interstellar_ai/app/components/InputFrontend.tsx

56 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-24 16:42:36 +02:00
import React, { useState, ForwardedRef, useEffect } from 'react';
2024-09-26 09:28:18 +02:00
import "../styles/variables.css"
2024-09-18 14:52:04 +02:00
interface InputProps {
message: string;
2024-09-24 16:42:36 +02:00
onSendClick: (message: string, override: boolean) => void;
2024-09-18 14:52:04 +02:00
onMicClick: () => void;
2024-09-26 08:57:28 +02:00
inputDisabled: boolean;
2024-09-30 12:45:19 +02:00
isRecording: boolean
2024-09-18 14:52:04 +02:00
}
const InputFrontend = React.forwardRef<HTMLDivElement, InputProps>(
2024-09-30 12:45:19 +02:00
({ message, onSendClick, onMicClick, inputDisabled, isRecording }, ref: ForwardedRef<HTMLDivElement>) => {
2024-09-18 14:52:04 +02:00
const [inputValue, setInputValue] = useState('');
2024-09-24 16:42:36 +02:00
2024-09-30 12:45:19 +02:00
useEffect(() => {
setInputValue(message);
}, [message]);
2024-09-18 14:52:04 +02:00
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
2024-09-19 16:52:16 +02:00
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
2024-09-24 13:50:46 +02:00
if (!inputDisabled) {
if (event.key === 'Enter') {
2024-09-24 16:42:36 +02:00
onSendClick(inputValue, false); // Call the function passed via props
2024-09-24 13:50:46 +02:00
setInputValue(''); // Optionally clear input after submission
event.preventDefault(); // Prevent default action (e.g., form submission)
}
2024-09-19 16:52:16 +02:00
}
};
2024-09-18 14:52:04 +02:00
return (
2024-09-19 16:52:16 +02:00
<div className="input" id="inputForm" ref={ref}>
2024-09-18 14:52:04 +02:00
<input
type="text"
name="user_message"
placeholder="Type your message here..."
value={inputValue}
onChange={handleInputChange}
2024-09-19 16:52:16 +02:00
onKeyDown={handleKeyDown}
2024-09-18 14:52:04 +02:00
/>
2024-09-24 16:42:36 +02:00
<button type="button" onClick={() => onSendClick(inputValue, false)} disabled={inputDisabled ? true : false}>
2024-09-18 14:52:04 +02:00
<img src="/img/send.svg" alt="send" />
</button>
2024-09-30 12:45:19 +02:00
<button className={`microphone-button ${isRecording ? "red" : "var(--input-button-color)"}`} type="button" onClick={onMicClick}>
2024-09-18 14:52:04 +02:00
<img src="/img/microphone.svg" alt="microphone" />
</button>
</div>
);
}
);
export default InputFrontend;