"use client"; import { useState } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@kandev/ui/card"; import { Button } from "@kandev/ui/button"; import { Spinner } from "@kandev/ui/spinner"; import { IconDatabase, IconBolt, IconInfoCircle, IconRefresh, IconTrash, } from "@tabler/icons-react"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { useDatabaseStats } from "@/hooks/domains/system/use-database-stats"; import { optimizeDatabase, vacuumDatabase } from "@/lib/api/domains/system-api"; import { formatBytes } from "@/lib/utils/format-bytes"; import { useActionFeedback, type ActionFeedbackState } from "@/hooks/use-action-feedback"; import { ActionButtonContent } from "./action-button-content"; import { JobProgressIndicator } from "./job-progress-indicator"; import { FactoryResetDialog } from "./factory-reset-dialog"; function formatTimestamp(iso: string | null | undefined): string { if (!iso) return "Never"; const d = new Date(iso); if (Number.isNaN(d.getTime())) return iso; return d.toLocaleString(); } const WAL_HELP = "Write-Ahead Log. SQLite writes new changes to this companion file first and merges them into the main database over time. A non-zero WAL size is normal - it temporarily grows under load and shrinks back during checkpoints. Running vacuum will reset it."; type Row = { label: string; value: string; testid: string; info?: string }; function StatRow({ label, value, testid, info }: Row) { return (
{label} {info && ( {info} )} {value}
); } type DBStats = { path: string; size_bytes: number; wal_size_bytes: number; schema_version: string; last_backup_at: string | null; }; function StatsTable({ database }: { database: DBStats }) { return (
); } // Plain-language descriptions of each maintenance operation. Surfaced as a // short paragraph next to the button + an on-hover tooltip so users without // database background know what each action does and when to use it. const VACUUM_HELP = "Reclaims unused space inside the database file. The file can grow over time as you delete tasks or sessions; vacuum compacts it back down. Safe to run anytime - it does not change any data. Use it once in a while if the database size looks large."; const OPTIMIZE_HELP = "Asks the database to refresh its internal indexes so common queries stay fast. Run it after deleting a lot of data or if the app feels sluggish. Quick and safe - no data is changed."; const FACTORY_RESET_HELP = "Wipes ALL Kandev data: tasks, sessions, settings, repositories, and worktrees. There is no undo. A backup is taken first, but restoring it requires using the Backups page after the reset. Only use this if you want to start over from scratch."; function OperationRow({ testid, label, description, button, }: { testid: string; label: string; description: string; button: React.ReactNode; }) { return (

{label}

{description}

{button}
); } function MaintenanceButtons({ vacuumState, optimizeState, onVacuum, onOptimize, onResetOpen, }: { vacuumState: ActionFeedbackState; optimizeState: ActionFeedbackState; onVacuum: () => void; onOptimize: () => void; onResetOpen: () => void; }) { return (
{VACUUM_HELP} } /> {OPTIMIZE_HELP} } /> {FACTORY_RESET_HELP} } />
); } export function DatabaseStatsCard() { const { database, isLoading, error, reload } = useDatabaseStats(); const vacuum = useActionFeedback(); const optimize = useActionFeedback(); const [resetOpen, setResetOpen] = useState(false); const onVacuum = () => void vacuum.run(async () => { await vacuumDatabase(); await reload(); }); const onOptimize = () => void optimize.run(async () => { await optimizeDatabase(); }); return ( Database {error && (

{error}

)} {!database && isLoading && (
Loading database stats...
)} {database && } setResetOpen(true)} />
); }