I want to implement the ability to delete all chats in my AI assistant…
- User
User: User: I want to implement the ability to delete all chats in my AI assistant interface.
function Sidebar() { const { chats, currentChatId, setCurrentChatId, addChat, deleteChat, updateChat, } = useContext(ChatContext);
const { theme, toggleTheme } = useContext(ThemeContext);
const [isResetting, setIsResetting] = useState(false); const [error, setError] = useState(null);
const handleNewChat = async () => { setIsResetting(true); setError(null);
try { const response = await fetch('http://localhost:8000/api/reset', { method: 'POST', headers: { 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error('Failed to reset chat.'); } const data = await response.json(); console.log(data.status); // Optional: Handle the response as needed addChat('New Chat'); setCurrentChatId(null); // Optionally set to the new chat's ID if returned } catch (err) { console.error('Error resetting chat:', err); setError('Unable to reset the chat. Please try again.'); } finally { setIsResetting(false); }};
const handleGoHome = () => { setCurrentChatId(null); };
return ( {/* Chatbot Name "Echo" at the top */} Echo
<div className="flex justify-between items-center mb-4"> <h2 className="text-xl font-bold text-gray-800 dark:text-gray-200">Past Chats</h2> <button onClick={handleNewChat} className={`p-2 bg-blue-500 text-white rounded hover:bg-blue-600 flex items-center justify-center ${ isResetting ? 'opacity-50 cursor-not-allowed' : '' }`} title="Start New Chat" disabled={isResetting} > {isResetting ? 'Resetting...' : <PlusIcon className="h-5 w-5" />} </button> </div> {error && ( <div className="mb-4 text-red-500 text-sm"> {error} </div> )} <ul className="flex-1 overflow-y-auto space-y-2"> {chats.length === 0 && ( <li className="text-gray-500 dark:text-gray-400">No past chats.</li> )} {chats.map((chat) => ( <ChatItem key={chat.id} chat={chat} /> ))} </ul> {/* Dark Mode Toggle at the bottom */} <div className="mt-4"> <DarkModeToggle theme={theme} toggleTheme={toggleTheme} /> </div> </div>); }
export default Sidebar;
export const ChatContext = createContext();
export const ChatProvider = ({ children }) => { const [chats, setChats] = useState(() => { const saved = localStorage.getItem('chats'); return saved ? JSON.parse(saved) : []; });
const [currentChatId, setCurrentChatId] = useState(() => { const saved = localStorage.getItem('currentChatId'); return saved ? JSON.parse(saved) : null; });
useEffect(() => { localStorage.setItem('chats', JSON.stringify(chats)); }, [chats]);
useEffect(() => { localStorage.setItem('currentChatId', JSON.stringify(currentChatId)); }, [currentChatId]);
- Assistant