Files
faunosteo/apps/app/src/components/editor/DiscardButton.tsx
T
bchanot 99b669bf8f feat(editor): header history, save line and Abandonner les modifications
Header Annuler/Retablir on every tab (aria-disabled, commit on pointerdown,
refused blur commit skips the undo). Save line: unsaved count selector
(savedDistance), saveLineState forms, server save time via readSaveTime
(lastSave in the store, never the browser clock). Discard relabelled and
names the stored copy. Tool column pair removed, glyphs in EditorGlyph.

Rewritten or removed assertions (R-24):
- editor-route.test: Revision 3 / unsaved text / Annuler les modifications
  -> save-line testid, Abandonner label, copy sentence in the dialog
- editor-route-conflict, editor-route-recovery: Revision n and copy_cap/
  copy_quota text queries -> save-line testid
- editor-no-loss: discard button label
- editor-annotations-tab: tool column undo/redo test -> no pair left
  (behaviour moved to editor-history-buttons.test)
- e2e journey/annotations/recovery/structures/support saveDraft/lan:
  undo/redo and revision/unsaved text -> historyButton, saveLine helpers;
  structures toHaveCount(0) on old text -> exact Revision n line
- editor-shell clickability: header Annuler/Retablir added
2026-11-08 17:55:49 +01:00

72 lines
2.3 KiB
TypeScript

import { useEffect, useState } from 'react'
import Button from '@/components/ui/Button'
import { useEditorReadOnly } from '@/editor/editingContext'
import { loadFailureMessage } from '@/editor/messages'
import { copyExists } from '@/editor/state/recovery'
import { useEditorStore } from '@/editor/state/editorStore'
import { useT } from '@/i18n/useT'
import ConfirmDialog from './ConfirmDialog'
/**
* « Abandonner les modifications »: asks first, then the store reloads the server draft.
* The confirmation names the temporary local copy when one is stored at the moment it
* opens (a copy left by an earlier write counts, not only the last write's).
*/
export default function DiscardButton({
onFailure,
}: {
/** A failed reload after a discard, worded for the notice under the header. */
onFailure: (message: string | null) => void
}) {
const t = useT()
const unsaved = useEditorStore((s) => s.unsaved)
const saving = useEditorStore((s) => s.saving)
const readOnly = useEditorReadOnly()
const [asking, setAsking] = useState(false)
const [copyNamed, setCopyNamed] = useState(false)
// The editor turned read-only while asking: the confirmation closes without effect.
useEffect(() => {
if (readOnly) setAsking(false)
}, [readOnly])
const ask = () => {
const { userId, versionId } = useEditorStore.getState()
setCopyNamed(copyExists(userId ?? '', versionId ?? ''))
setAsking(true)
}
const discard = async () => {
setAsking(false)
const result = await useEditorStore
.getState()
.discardChanges({ confirmed: true })
onFailure(
!result.ok && result.reason === 'load'
? loadFailureMessage(t, result.failure)
: null,
)
}
const body = copyNamed
? `${t('editor.draft.discard_ask')} ${t('editor.draft.discard_copy')}`
: t('editor.draft.discard_ask')
return (
<>
<Button
type="button"
variant="secondary"
disabled={!unsaved || saving || readOnly}
onClick={ask}
>
{t('editor.draft.discard')}
</Button>
<ConfirmDialog
open={asking}
title={t('editor.draft.discard')}
body={body}
confirmLabel={t('editor.draft.discard_confirm')}
cancelLabel={t('common.cancel')}
onConfirm={() => void discard()}
onCancel={() => setAsking(false)}
/>
</>
)
}