import { useNavigate } from '@remix-run/react'; import React, { useState } from 'react'; import { toast } from 'react-toastify'; import { db, deleteById, getAll } from '~/lib/persistence'; import { classNames } from '~/utils/classNames'; import styles from '~/components/settings/Settings.module.scss'; export default function ChatHistoryTab() { const navigate = useNavigate(); const [isDeleting, setIsDeleting] = useState(false); const downloadAsJson = (data: any, filename: string) => { const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; const handleDeleteAllChats = async () => { if (!db) { toast.error('Database is not available'); return; } try { setIsDeleting(true); const allChats = await getAll(db); // Delete all chats one by one await Promise.all(allChats.map((chat) => deleteById(db!, chat.id))); toast.success('All chats deleted successfully'); navigate('/', { replace: true }); } catch (error) { toast.error('Failed to delete chats'); console.error(error); } finally { setIsDeleting(false); } }; const handleExportAllChats = async () => { if (!db) { toast.error('Database is not available'); return; } try { const allChats = await getAll(db); const exportData = { chats: allChats, exportDate: new Date().toISOString(), }; downloadAsJson(exportData, `all-chats-${new Date().toISOString()}.json`); toast.success('Chats exported successfully'); } catch (error) { toast.error('Failed to export chats'); console.error(error); } }; return ( <>

Chat History

Danger Area

This action cannot be undone!

); }