feat(#9): export/import состояния — backup в JSON и восстановление
This commit is contained in:
@@ -143,6 +143,21 @@ export const ru: Dict = {
|
||||
'settings.section.appearance': 'Внешний вид',
|
||||
'settings.section.language': 'Язык',
|
||||
'settings.section.updates': 'Обновления',
|
||||
'settings.section.data': 'Данные',
|
||||
'settings.data.export.label': 'Экспортировать всё',
|
||||
'settings.data.export.hint':
|
||||
'Сохрани резервную копию упражнений, истории, челленджей и настроек в JSON-файл.',
|
||||
'settings.data.export.btn': 'Сохранить',
|
||||
'settings.data.export.ok': 'Сохранено в {path}',
|
||||
'settings.data.export.err': 'Не удалось сохранить',
|
||||
'settings.data.import.label': 'Восстановить из файла',
|
||||
'settings.data.import.hint':
|
||||
'Загрузить ранее сохранённую копию. Текущие данные будут перезаписаны.',
|
||||
'settings.data.import.btn': 'Восстановить',
|
||||
'settings.data.import.confirm':
|
||||
'Все текущие упражнения, история и настройки будут заменены содержимым файла. Продолжить?',
|
||||
'settings.data.import.ok': 'Восстановлено',
|
||||
'settings.data.import.err': 'Файл не подошёл — это не наша резервная копия?',
|
||||
'settings.notification_mode.label': 'Режим уведомления',
|
||||
'settings.notification_mode.hint': 'Как должно выглядеть напоминание',
|
||||
'settings.notification_mode.modal': 'Окно поверх всех',
|
||||
@@ -390,6 +405,21 @@ export const en: Dict = {
|
||||
'settings.section.appearance': 'Appearance',
|
||||
'settings.section.language': 'Language',
|
||||
'settings.section.updates': 'Updates',
|
||||
'settings.section.data': 'Data',
|
||||
'settings.data.export.label': 'Export everything',
|
||||
'settings.data.export.hint':
|
||||
'Save a backup of exercises, history, challenges and settings to a JSON file.',
|
||||
'settings.data.export.btn': 'Save',
|
||||
'settings.data.export.ok': 'Saved to {path}',
|
||||
'settings.data.export.err': 'Could not save',
|
||||
'settings.data.import.label': 'Restore from file',
|
||||
'settings.data.import.hint':
|
||||
'Load a previously saved backup. Current data will be overwritten.',
|
||||
'settings.data.import.btn': 'Restore',
|
||||
'settings.data.import.confirm':
|
||||
'All current exercises, history and settings will be replaced with the file contents. Continue?',
|
||||
'settings.data.import.ok': 'Restored',
|
||||
'settings.data.import.err': "Couldn't read the file — not our backup?",
|
||||
'settings.notification_mode.label': 'Notification mode',
|
||||
'settings.notification_mode.hint': 'How a reminder appears',
|
||||
'settings.notification_mode.modal': 'Window on top',
|
||||
|
||||
@@ -158,11 +158,102 @@ export default function SettingsPage(): JSX.Element {
|
||||
|
||||
<SectionHeader title={t('settings.section.updates')} />
|
||||
<UpdaterCard />
|
||||
|
||||
<div className="mt-6">
|
||||
<SectionHeader title={t('settings.section.data')} />
|
||||
<DataCard />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DataCard(): JSX.Element {
|
||||
const { t } = useT()
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [toast, setToast] = useState<string | null>(null)
|
||||
|
||||
// Простое toast'-сообщение в карточке; через 4 сек чистится.
|
||||
useEffect(() => {
|
||||
if (!toast) return
|
||||
const id = setTimeout(() => setToast(null), 4000)
|
||||
return () => clearTimeout(id)
|
||||
}, [toast])
|
||||
|
||||
async function onExport(): Promise<void> {
|
||||
setBusy(true)
|
||||
try {
|
||||
const r = await window.api.exportState()
|
||||
if (r.ok && r.path) {
|
||||
setToast(t('settings.data.export.ok', { path: r.path }))
|
||||
} else if (!r.ok) {
|
||||
setToast(t('settings.data.export.err'))
|
||||
}
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
async function onImport(): Promise<void> {
|
||||
// eslint-disable-next-line no-alert -- modal-confirm для destructive action
|
||||
if (!window.confirm(t('settings.data.import.confirm'))) return
|
||||
setBusy(true)
|
||||
try {
|
||||
const r = await window.api.importState()
|
||||
if (r.ok) setToast(t('settings.data.import.ok'))
|
||||
else if ('error' in r && r.error !== undefined) {
|
||||
setToast(t('settings.data.import.err'))
|
||||
}
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<Row>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-[15px] font-semibold leading-tight">
|
||||
{t('settings.data.export.label')}
|
||||
</div>
|
||||
<div className="text-[13px] text-text/65 mt-1 leading-snug">
|
||||
{t('settings.data.export.hint')}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={onExport}
|
||||
disabled={busy}
|
||||
className="h-9 px-4 rounded-xl bg-surface-2 hover:bg-hairline/25 text-[14px] font-semibold transition-colors disabled:opacity-50"
|
||||
>
|
||||
{t('settings.data.export.btn')}
|
||||
</button>
|
||||
</Row>
|
||||
<Row last>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-[15px] font-semibold leading-tight">
|
||||
{t('settings.data.import.label')}
|
||||
</div>
|
||||
<div className="text-[13px] text-text/65 mt-1 leading-snug">
|
||||
{t('settings.data.import.hint')}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={onImport}
|
||||
disabled={busy}
|
||||
className="h-9 px-4 rounded-xl bg-surface-2 hover:bg-hairline/25 text-[14px] font-semibold transition-colors disabled:opacity-50"
|
||||
>
|
||||
{t('settings.data.import.btn')}
|
||||
</button>
|
||||
</Row>
|
||||
{toast && (
|
||||
<div className="px-4 py-2.5 text-[13px] text-text/75 bg-accent/8 truncate font-medium">
|
||||
{toast}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function ToggleRow({
|
||||
label,
|
||||
hint,
|
||||
|
||||
Reference in New Issue
Block a user