import { useState } from 'react' import { Card } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { FloppyDisk, Trash, PencilSimple, CheckCircle, Clock } from '@phosphor-icons/react' import { useAppDispatch, useAppSelector } from '@/store' import { saveFile, deleteFile } from '@/store/slices/filesSlice' import { toast } from 'sonner' export function PersistenceExample() { const dispatch = useAppDispatch() const files = useAppSelector((state) => state.files.files) const [fileName, setFileName] = useState('') const [fileContent, setFileContent] = useState('') const [editingId, setEditingId] = useState(null) const handleSave = async () => { if (!fileName.trim()) { toast.error('File name is required') return } const fileItem = { id: editingId || `file-${Date.now()}`, name: fileName, content: fileContent, language: 'javascript', path: `/src/${fileName}`, updatedAt: Date.now(), } try { await dispatch(saveFile(fileItem)).unwrap() toast.success(`File "${fileName}" saved automatically!`, { description: 'Synced to IndexedDB and Flask API', }) setFileName('') setFileContent('') setEditingId(null) } catch (error: any) { toast.error('Failed to save file', { description: error, }) } } const handleEdit = (file: any) => { setEditingId(file.id) setFileName(file.name) setFileContent(file.content) } const handleDelete = async (fileId: string, name: string) => { try { await dispatch(deleteFile(fileId)).unwrap() toast.success(`File "${name}" deleted`, { description: 'Automatically synced to storage', }) } catch (error: any) { toast.error('Failed to delete file', { description: error, }) } } const handleCancel = () => { setFileName('') setFileContent('') setEditingId(null) } return (

Persistence Middleware Example

Demonstrates automatic persistence of Redux state to IndexedDB and Flask API

{editingId ? 'Edit File' : 'Create File'}

{editingId && ( Editing )}
setFileName(e.target.value)} placeholder="example.js" />