2024-06-27 17:34:48 -04:00
|
|
|
import { SquareSlash } from 'lucide-react';
|
|
|
|
|
import { Constants } from 'librechat-data-provider';
|
|
|
|
|
import { useState, useEffect } from 'react';
|
2025-02-05 17:37:17 +01:00
|
|
|
import { Input } from '~/components/ui';
|
2024-06-27 17:34:48 -04:00
|
|
|
import { useLocalize } from '~/hooks';
|
|
|
|
|
|
|
|
|
|
const Command = ({
|
|
|
|
|
initialValue,
|
|
|
|
|
onValueChange,
|
|
|
|
|
disabled,
|
|
|
|
|
tabIndex,
|
|
|
|
|
}: {
|
|
|
|
|
initialValue?: string;
|
|
|
|
|
onValueChange?: (value: string) => void;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
tabIndex?: number;
|
|
|
|
|
}) => {
|
|
|
|
|
const localize = useLocalize();
|
|
|
|
|
const [command, setCommand] = useState(initialValue || '');
|
|
|
|
|
const [charCount, setCharCount] = useState(initialValue?.length || 0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setCommand(initialValue || '');
|
|
|
|
|
setCharCount(initialValue?.length || 0);
|
|
|
|
|
}, [initialValue]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setCharCount(command.length);
|
|
|
|
|
}, [command]);
|
|
|
|
|
|
|
|
|
|
const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
|
|
|
let newValue = e.target.value.toLowerCase();
|
|
|
|
|
|
|
|
|
|
newValue = newValue.replace(/\s/g, '-').replace(/[^a-z0-9-]/g, '');
|
|
|
|
|
|
|
|
|
|
if (newValue.length <= Constants.COMMANDS_MAX_LENGTH) {
|
|
|
|
|
setCommand(newValue);
|
|
|
|
|
onValueChange?.(newValue);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-18 05:52:05 -04:00
|
|
|
if (disabled === true && !command) {
|
2024-06-27 17:34:48 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2025-02-05 17:37:17 +01:00
|
|
|
<div className="rounded-xl border border-border-light">
|
|
|
|
|
<h3 className="flex h-10 items-center gap-1 pl-4 text-sm text-text-secondary">
|
2024-06-27 17:34:48 -04:00
|
|
|
<SquareSlash className="icon-sm" />
|
2025-02-05 17:37:17 +01:00
|
|
|
<Input
|
2024-06-27 17:34:48 -04:00
|
|
|
type="text"
|
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
placeholder={localize('com_ui_command_placeholder')}
|
|
|
|
|
value={command}
|
|
|
|
|
onChange={handleInputChange}
|
2025-02-05 17:37:17 +01:00
|
|
|
className="border-none"
|
2024-06-27 17:34:48 -04:00
|
|
|
/>
|
2024-08-18 05:52:05 -04:00
|
|
|
{disabled !== true && (
|
2025-02-05 17:37:17 +01:00
|
|
|
<span className="mr-4 w-10 text-xs text-text-secondary md:text-sm">{`${charCount}/${Constants.COMMANDS_MAX_LENGTH}`}</span>
|
2024-06-27 17:34:48 -04:00
|
|
|
)}
|
|
|
|
|
</h3>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Command;
|