mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
Some checks failed
CI/CD Pipeline / backend-lint (push) Failing after 31s
CI/CD Pipeline / backend-build (push) Has been skipped
CI/CD Pipeline / frontend-lint (push) Failing after 1m37s
CI/CD Pipeline / frontend-build (push) Has been skipped
CI/CD Pipeline / e2e-test (push) Has been skipped
- Replace all 'any' types with proper TypeScript interfaces - Fix React hooks setState in useEffect issues with lazy initialization - Remove unused variables and imports across all files - Fix React Compiler memoization dependency issues - Add comprehensive i18n translation keys for admin interfaces - Apply consistent prettier formatting throughout codebase - Clean up unused bulk editing functionality - Improve type safety and code quality across frontend Files changed: 39 - ImpactMetrics.tsx: Fixed any types and interfaces - AdminVerificationQueuePage.tsx: Added i18n keys, removed unused vars - LocalizationUIPage.tsx: Fixed memoization, added translations - LocalizationDataPage.tsx: Added type safety and translations - And 35+ other files with various lint fixes
112 lines
3.2 KiB
TypeScript
112 lines
3.2 KiB
TypeScript
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
import { fileToDataUrl } from '@/lib/utils.ts';
|
|
import { useChat } from '@/hooks/useChat.ts';
|
|
import { useSpeechRecognition } from '@/hooks/useSpeechRecognition.ts';
|
|
|
|
export const useChatbot = () => {
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const [inputValue, setInputValue] = useState('');
|
|
const [attachedImage, setAttachedImage] = useState<{ file: File; previewUrl: string } | null>(
|
|
null
|
|
);
|
|
|
|
const { messages, isLoading, sendMessage, clearChat } = useChat();
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
const { isListening, transcript, startListening, stopListening, isSupported } =
|
|
useSpeechRecognition();
|
|
|
|
// Update input value when speech recognition provides transcript
|
|
const lastTranscriptRef = useRef<string>('');
|
|
|
|
useLayoutEffect(() => {
|
|
if (isListening && transcript && transcript !== lastTranscriptRef.current) {
|
|
lastTranscriptRef.current = transcript;
|
|
// Update input value in layout effect for speech recognition
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
setInputValue(transcript);
|
|
}
|
|
}, [transcript, isListening]);
|
|
|
|
const toggleChat = useCallback(() => {
|
|
setIsOpen((prev) => !prev);
|
|
}, []);
|
|
|
|
const handleSendMessage = useCallback(() => {
|
|
if (inputValue.trim() || attachedImage) {
|
|
sendMessage({ text: inputValue, imageUrl: attachedImage?.previewUrl });
|
|
setInputValue('');
|
|
setAttachedImage(null);
|
|
}
|
|
}, [sendMessage, inputValue, attachedImage]);
|
|
|
|
const handleClearChat = useCallback(() => {
|
|
// A confirmation could be added here in a real app
|
|
clearChat();
|
|
}, [clearChat]);
|
|
|
|
const handleFileChange = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const file = e.target.files?.[0];
|
|
if (file && file.type.startsWith('image/')) {
|
|
try {
|
|
const previewUrl = await fileToDataUrl(file);
|
|
setAttachedImage({ file, previewUrl });
|
|
} catch (error) {
|
|
console.error('Error creating image preview:', error);
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
inputRef.current?.focus();
|
|
}
|
|
}, [messages, isOpen]);
|
|
|
|
const showSuggestions = messages.length === 1 && messages[0].id === 'init';
|
|
|
|
return useMemo(
|
|
() => ({
|
|
isOpen,
|
|
toggleChat,
|
|
messages,
|
|
isLoading,
|
|
handleSendMessage,
|
|
handleClearChat,
|
|
messagesEndRef,
|
|
inputRef,
|
|
showSuggestions,
|
|
isListening,
|
|
transcript,
|
|
startListening,
|
|
stopListening,
|
|
isSpeechSupported: isSupported,
|
|
inputValue,
|
|
setInputValue,
|
|
attachedImage,
|
|
setAttachedImage,
|
|
fileInputRef,
|
|
handleFileChange,
|
|
}),
|
|
[
|
|
isOpen,
|
|
messages,
|
|
isLoading,
|
|
handleSendMessage,
|
|
handleClearChat,
|
|
showSuggestions,
|
|
isListening,
|
|
transcript,
|
|
startListening,
|
|
stopListening,
|
|
isSupported,
|
|
inputValue,
|
|
attachedImage,
|
|
handleFileChange,
|
|
toggleChat,
|
|
]
|
|
);
|
|
};
|