import { Button } from '@/components/ui/button'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from '@/components/ui/tooltip'; import { useEncryptionKey } from '@/contexts/encryption-key-context'; import { type Account, type Bank } from '@/types/account'; import { type AutomationRule } from '@/types/automation-rule'; import { type Category } from '@/types/category'; import { Upload } from 'lucide-react'; import { useState } from 'react'; import { toast } from 'sonner'; import { ImportTransactionsDrawer } from './import-transactions-drawer'; interface ImportData { accounts: Account[]; categories: Category[]; banks: Bank[]; automationRules: AutomationRule[]; } export function ImportTransactionsButton() { const { isKeySet } = useEncryptionKey(); const [drawerOpen, setDrawerOpen] = useState(false); const [importData, setImportData] = useState(null); const [loading, setLoading] = useState(false); const handleOpenDrawer = async () => { if (!isKeySet) { toast.error( 'Please unlock your encryption key to import transactions', ); return; } // Fetch data on-demand when drawer opens setLoading(true); try { const response = await fetch('/api/import/data'); if (!response.ok) { throw new Error('Failed to load import data'); } const data = await response.json(); setImportData(data); setDrawerOpen(true); } catch (error) { toast.error('Failed to load import data'); console.error(error); } finally { setLoading(false); } }; return ( <> {!isKeySet ? `Unlock encryption to import transactions` : `Import transactions from CSV/Excel`} {importData && ( )} ); }