import React, { useState, useRef, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { useOrg } from '../contexts/OrgContext'; import Sidebar from '../components/figma/Sidebar'; interface Message { id: string; role: 'user' | 'assistant'; content: string; timestamp: Date; } interface ChatState { messages: Message[]; isLoading: boolean; showEmployeeMenu: boolean; mentionQuery: string; selectedEmployees: string[]; hasUploadedFiles: boolean; uploadedFiles: Array<{ name: string; type: string; size: number; }>; } const ChatNew: React.FC = () => { const { user } = useAuth(); const { employees, orgId } = useOrg(); const navigate = useNavigate(); const inputRef = useRef(null); const fileInputRef = useRef(null); const [state, setState] = useState({ messages: [], isLoading: false, showEmployeeMenu: false, mentionQuery: '', selectedEmployees: [], hasUploadedFiles: false, uploadedFiles: [] }); const [currentInput, setCurrentInput] = useState(''); const [selectedCategory, setSelectedCategory] = useState('Accountability'); useEffect(() => { if (!user) { navigate('/login'); } }, [user, navigate]); const questionStarters = [ "How can the company serve them better?", "How can the company serve them better?", "How can the company serve them better?", "How can the company serve them better?" ]; const categories = ['Accountability', 'Employee Growth', 'Customer Focus', 'Teamwork']; const handleSendMessage = async () => { if (!currentInput.trim() && state.uploadedFiles.length === 0) return; const newMessage: Message = { id: Date.now().toString(), role: 'user', content: currentInput.trim(), timestamp: new Date() }; setState(prev => ({ ...prev, messages: [...prev.messages, newMessage], isLoading: true })); setCurrentInput(''); // Simulate AI response setTimeout(() => { const aiResponse: Message = { id: (Date.now() + 1).toString(), role: 'assistant', content: "Based on the information provided and the company data, here are my insights and recommendations...", timestamp: new Date() }; setState(prev => ({ ...prev, messages: [...prev.messages, aiResponse], isLoading: false })); }, 2000); }; const handleInputChange = (e: React.ChangeEvent) => { const value = e.target.value; setCurrentInput(value); // Check for @ mentions const atIndex = value.lastIndexOf('@'); if (atIndex !== -1 && atIndex === value.length - 1) { setState(prev => ({ ...prev, showEmployeeMenu: true, mentionQuery: '' })); } else if (atIndex !== -1 && value.length > atIndex + 1) { const query = value.substring(atIndex + 1); setState(prev => ({ ...prev, showEmployeeMenu: true, mentionQuery: query })); } else { setState(prev => ({ ...prev, showEmployeeMenu: false, mentionQuery: '' })); } }; const handleEmployeeSelect = (employeeName: string) => { const atIndex = currentInput.lastIndexOf('@'); if (atIndex !== -1) { const newInput = currentInput.substring(0, atIndex) + '@' + employeeName + ' '; setCurrentInput(newInput); } setState(prev => ({ ...prev, showEmployeeMenu: false, mentionQuery: '' })); }; const handleFileUpload = (e: React.ChangeEvent) => { const files = Array.from(e.target.files || []); if (files.length > 0) { const uploadedFiles = files.map(file => ({ name: file.name, type: file.type, size: file.size })); setState(prev => ({ ...prev, hasUploadedFiles: true, uploadedFiles: [...prev.uploadedFiles, ...uploadedFiles] })); } }; const removeFile = (index: number) => { setState(prev => ({ ...prev, uploadedFiles: prev.uploadedFiles.filter((_, i) => i !== index), hasUploadedFiles: prev.uploadedFiles.length > 1 })); }; const handleQuestionClick = (question: string) => { setCurrentInput(question); }; const renderEmployeeMenu = () => { if (!state.showEmployeeMenu) return null; const filteredEmployees = employees.filter(emp => emp.name.toLowerCase().includes(state.mentionQuery.toLowerCase()) ); return (
{filteredEmployees.slice(0, 3).map((employee, index) => (
handleEmployeeSelect(employee.name)} className={`self-stretch px-3 py-2 rounded-xl flex flex-col justify-start items-start gap-2.5 overflow-hidden cursor-pointer hover:bg-Text-Gray-100 ${index === 2 ? 'bg-Text-Gray-100' : '' }`} >
{employee.name}
))}
); }; const renderUploadedFiles = () => { if (state.uploadedFiles.length === 0) return null; return (
{state.uploadedFiles.map((file, index) => (
{file.name}
removeFile(index)} className="cursor-pointer">
))}
); }; const renderChatInterface = () => { if (state.messages.length === 0) { return (
What would you like to understand?
{categories.map((category) => (
setSelectedCategory(category)} className={`px-3 py-1.5 rounded-lg shadow-[0px_1px_2px_0px_rgba(16,24,40,0.05)] shadow-[inset_0px_-2px_0px_0px_rgba(10,13,18,0.05)] shadow-[inset_0px_0px_0px_1px_rgba(10,13,18,0.18)] flex justify-center items-center gap-1 overflow-hidden cursor-pointer ${selectedCategory === category ? 'bg-white' : '' }`} >
{category}
))}
{questionStarters.map((question, index) => (
handleQuestionClick(question)} className="flex-1 h-48 px-3 py-4 bg-Main-BG-Gray-50 rounded-2xl inline-flex flex-col justify-between items-start overflow-hidden cursor-pointer hover:bg-Main-BG-Gray-100" >
{question}
))}
{renderChatInput()}
); } return (
{state.messages.map((message) => (
{message.content}
))} {state.isLoading && (
)}
{renderChatInput()}
); }; const renderChatInput = () => { return (
{renderUploadedFiles()}
{currentInput || "Ask anything, use @ to tag staff and ask questions."}
fileInputRef.current?.click()} className="cursor-pointer">
fileInputRef.current?.click()} className="cursor-pointer">
0 ? 'bg-Main-BG-Gray-800' : 'bg-Text-Gray-300' }`} >
{renderEmployeeMenu()}