import { Stethoscope } from 'lucide-react' import { useState } from 'react' import { Button } from '../shared/Button' import { useTranslation } from '../../i18n' import { runDoctorRepair, type DoctorRepairResult } from '../../lib/doctorRepair' import { useUIStore } from '../../stores/uiStore' type DoctorPanelProps = { compact?: boolean } export function DoctorPanel({ compact = false }: DoctorPanelProps) { const t = useTranslation() const addToast = useUIStore((s) => s.addToast) const [isRunning, setIsRunning] = useState(false) const [result, setResult] = useState(null) const handleRunDoctor = async () => { setIsRunning(true) try { const nextResult = await runDoctorRepair() setResult(nextResult) addToast({ type: nextResult.local.failedKeys.length === 0 ? 'success' : 'warning', message: getDoctorToastMessage(t, nextResult), }) } catch (error) { addToast({ type: 'error', message: error instanceof Error ? error.message : t('settings.diagnostics.doctorFailed'), }) } finally { setIsRunning(false) } } const statusText = result ? getDoctorStatusMessage(t, result) : null return (
{t('settings.diagnostics.doctorTitle')}

{t('settings.diagnostics.doctorDescription')}

{t('settings.diagnostics.doctorProtectedData')}

{t('settings.diagnostics.doctorSafeKeys')}
{statusText ? (
{statusText}
) : null}
) } function getDoctorToastMessage( t: ReturnType, result: DoctorRepairResult, ): string { if (result.local.failedKeys.length > 0) { return t('settings.diagnostics.doctorPartial', { count: String(result.local.failedKeys.length) }) } return t('settings.diagnostics.doctorCompleted') } function getDoctorStatusMessage( t: ReturnType, result: DoctorRepairResult, ): string { const clearedCount = result.local.removedKeys.length const base = t('settings.diagnostics.doctorResultLocal', { count: String(clearedCount) }) if (result.local.failedKeys.length > 0) { return `${base} ${t('settings.diagnostics.doctorResultFailedKeys', { count: String(result.local.failedKeys.length) })}` } if (result.server) { return `${base} ${t('settings.diagnostics.doctorServerRan')}` } if (result.serverError) { return `${base} ${t('settings.diagnostics.doctorServerUnavailable')}` } return base }