import { useState, useRef } from 'react'; import { motion } from 'framer-motion'; import { toast } from 'react-toastify'; import { DialogRoot, DialogClose, Dialog, DialogTitle } from '~/components/ui/Dialog'; import { db, getAll, deleteById } from '~/lib/persistence'; export default function DataTab() { const [isDownloadingTemplate, setIsDownloadingTemplate] = useState(false); const [isImportingKeys, setIsImportingKeys] = useState(false); const [isResetting, setIsResetting] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [showResetInlineConfirm, setShowResetInlineConfirm] = useState(false); const [showDeleteInlineConfirm, setShowDeleteInlineConfirm] = useState(false); const fileInputRef = useRef(null); const apiKeyFileInputRef = useRef(null); const handleExportAllChats = async () => { try { if (!db) { throw new Error('Database not initialized'); } // Get all chats from IndexedDB const allChats = await getAll(db); const exportData = { chats: allChats, exportDate: new Date().toISOString(), }; // Download as JSON const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `bolt-chats-${new Date().toISOString()}.json`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); toast.success('Chats exported successfully'); } catch (error) { console.error('Export error:', error); toast.error('Failed to export chats'); } }; const handleExportSettings = () => { try { const settings = { userProfile: localStorage.getItem('bolt_user_profile'), settings: localStorage.getItem('bolt_settings'), exportDate: new Date().toISOString(), }; const blob = new Blob([JSON.stringify(settings, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `bolt-settings-${new Date().toISOString()}.json`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); toast.success('Settings exported successfully'); } catch (error) { console.error('Export error:', error); toast.error('Failed to export settings'); } }; const handleImportSettings = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (!file) { return; } try { const content = await file.text(); const settings = JSON.parse(content); if (settings.userProfile) { localStorage.setItem('bolt_user_profile', settings.userProfile); } if (settings.settings) { localStorage.setItem('bolt_settings', settings.settings); } window.location.reload(); // Reload to apply settings toast.success('Settings imported successfully'); } catch (error) { console.error('Import error:', error); toast.error('Failed to import settings'); } }; const handleImportAPIKeys = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (!file) { return; } setIsImportingKeys(true); try { const content = await file.text(); const keys = JSON.parse(content); // Validate and save each key Object.entries(keys).forEach(([key, value]) => { if (typeof value !== 'string') { throw new Error(`Invalid value for key: ${key}`); } localStorage.setItem(`bolt_${key.toLowerCase()}`, value); }); toast.success('API keys imported successfully'); } catch (error) { console.error('Error importing API keys:', error); toast.error('Failed to import API keys'); } finally { setIsImportingKeys(false); if (apiKeyFileInputRef.current) { apiKeyFileInputRef.current.value = ''; } } }; const handleDownloadTemplate = () => { setIsDownloadingTemplate(true); try { const template = { Anthropic_API_KEY: '', OpenAI_API_KEY: '', Google_API_KEY: '', Groq_API_KEY: '', HuggingFace_API_KEY: '', OpenRouter_API_KEY: '', Deepseek_API_KEY: '', Mistral_API_KEY: '', OpenAILike_API_KEY: '', Together_API_KEY: '', xAI_API_KEY: '', Perplexity_API_KEY: '', Cohere_API_KEY: '', AzureOpenAI_API_KEY: '', OPENAI_LIKE_API_BASE_URL: '', LMSTUDIO_API_BASE_URL: '', OLLAMA_API_BASE_URL: '', TOGETHER_API_BASE_URL: '', }; const blob = new Blob([JSON.stringify(template, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'bolt-api-keys-template.json'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); toast.success('Template downloaded successfully'); } catch (error) { console.error('Error downloading template:', error); toast.error('Failed to download template'); } finally { setIsDownloadingTemplate(false); } }; const handleResetSettings = async () => { setIsResetting(true); try { // Clear all stored settings from localStorage localStorage.removeItem('bolt_user_profile'); localStorage.removeItem('bolt_settings'); localStorage.removeItem('bolt_chat_history'); // Clear all data from IndexedDB if (!db) { throw new Error('Database not initialized'); } // Get all chats and delete them const chats = await getAll(db as IDBDatabase); const deletePromises = chats.map((chat) => deleteById(db as IDBDatabase, chat.id)); await Promise.all(deletePromises); // Close the dialog first setShowResetInlineConfirm(false); // Then reload and show success message window.location.reload(); toast.success('Settings reset successfully'); } catch (error) { console.error('Reset error:', error); setShowResetInlineConfirm(false); toast.error('Failed to reset settings'); } finally { setIsResetting(false); } }; const handleDeleteAllChats = async () => { setIsDeleting(true); try { // Clear chat history from localStorage localStorage.removeItem('bolt_chat_history'); // Clear chats from IndexedDB if (!db) { throw new Error('Database not initialized'); } // Get all chats and delete them one by one const chats = await getAll(db as IDBDatabase); const deletePromises = chats.map((chat) => deleteById(db as IDBDatabase, chat.id)); await Promise.all(deletePromises); // Close the dialog first setShowDeleteInlineConfirm(false); // Then show the success message toast.success('Chat history deleted successfully'); } catch (error) { console.error('Delete error:', error); setShowDeleteInlineConfirm(false); toast.error('Failed to delete chat history'); } finally { setIsDeleting(false); } }; return (
{/* Reset Settings Dialog */}
Reset All Settings?

This will reset all your settings to their default values. This action cannot be undone.

{isResetting ? (
) : (
)} Reset Settings
{/* Delete Confirmation Dialog */}
Delete All Chats?

This will permanently delete all your chat history. This action cannot be undone.

{isDeleting ? (
) : (
)} Delete All
{/* Chat History Section */}

Chat History

Export or delete all your chat history.

Export All Chats setShowDeleteInlineConfirm(true)} >
Delete All Chats
{/* Settings Backup Section */}

Settings Backup

Export your settings to a JSON file or import settings from a previously exported file.

Export Settings fileInputRef.current?.click()} >
Import Settings setShowResetInlineConfirm(true)} >
Reset Settings
{/* API Keys Management Section */}

API Keys Management

Import API keys from a JSON file or download a template to fill in your keys.

{isDownloadingTemplate ? (
) : (
)} Download Template apiKeyFileInputRef.current?.click()} disabled={isImportingKeys} > {isImportingKeys ? (
) : (
)} Import API Keys
); }