import clsx from "clsx"; import { memo, useCallback, useEffect, useMemo, useState, type CSSProperties } from "react"; import "./rds-comp-ai-typing-section.scss"; import RdsButton from "../../raaghu-elements/rds-button/rds-button"; import RdsAutocomplete from "../../raaghu-elements/rds-autocomplete/rds-autocomplete"; import AutoAwesomeOutlinedIcon from "@mui/icons-material/AutoAwesomeOutlined"; import RdsCompAiAttachment, { Comment as AttachmentComment, } from "../../raaghu-components/rds-comp-ai-attachment/rds-comp-ai-attachment"; import RdsCompAiIcon, { registerMaterialIcons } from "../rds-comp-ai-icon/rds-comp-ai-icon"; export interface RdsCompAiTypingSectionProps { colorVariant?: string; placeholderText?: string; iconName: string; onSend?: (inputText: string, image?: string) => void; onAddComment?: (comment: AttachmentComment) => void; previewImage?: string; type?: string; autoCompleteMaxWidth?: string; } registerMaterialIcons({ enhance: AutoAwesomeOutlinedIcon, }); const MOBILE_BREAKPOINT = 768; const RdsCompAiTypingSectionComponent = ({ placeholderText, onSend, previewImage, onAddComment, autoCompleteMaxWidth, }: RdsCompAiTypingSectionProps) => { const [inputText, setInputText] = useState(""); const [isMobile, setIsMobile] = useState(() => typeof window !== 'undefined' && window.innerWidth <= MOBILE_BREAKPOINT); const [enhancedImage, setEnhancedImage] = useState(null); useEffect(() => { const handleResize = () => { setIsMobile(window.innerWidth <= MOBILE_BREAKPOINT); }; window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); const handleMicClick = useCallback(() => { if (!("webkitSpeechRecognition" in window)) return; const SpeechRecognitionCtor = window.webkitSpeechRecognition; if (!SpeechRecognitionCtor) return; const recognition = new SpeechRecognitionCtor(); recognition.continuous = false; recognition.interimResults = false; recognition.lang = "en-US"; recognition.onresult = (event: SpeechRecognitionEvent) => { const transcript = event.results[0][0].transcript; setInputText(transcript); }; recognition.start(); }, []); const handleSent = useCallback(() => { onSend?.(inputText, enhancedImage || previewImage); setInputText(""); setEnhancedImage(null); }, [enhancedImage, inputText, onSend, previewImage]); const handleFileSelect = useCallback((file: File) => { const reader = new FileReader(); reader.onloadend = () => { setEnhancedImage(reader.result as string); setInputText(""); }; reader.readAsDataURL(file); }, []); const handleAddComment = useCallback( (comment: AttachmentComment) => { onAddComment?.(comment); }, [onAddComment] ); const handleFigmaSubmit = useCallback( (value: string) => { onAddComment?.({ image: value } as AttachmentComment); }, [onAddComment] ); const autocompleteStyle = useMemo( (): CSSProperties | undefined => autoCompleteMaxWidth ? ({ "--ai-typing-autocomplete-max-width": autoCompleteMaxWidth } as CSSProperties) : undefined, [autoCompleteMaxWidth] ); return (