Files
faunosteo/apps/app/src/components/editor/CopyIssueNotice.tsx
T

116 lines
3.9 KiB
TypeScript

import { useState } from 'react'
import { Link } from 'react-router-dom'
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import { formatParisTime } from '@/editor/format'
import { copySheetId } from '@/editor/otherCopies'
import { useEditorStore } from '@/editor/state/editorStore'
import type { StoredCopyInfo } from '@/editor/state/recovery'
import type { Locale } from '@/i18n/types'
import { useLocaleStore } from '@/state/localeStore'
import { useT } from '@/i18n/useT'
import ConfirmDialog from './ConfirmDialog'
type Translate = ReturnType<typeof useT>
/** « <sheet_name> · révision R · <heure> », each part only when the copy tells it. */
function copyLine(t: Translate, locale: Locale, copy: StoredCopyInfo): string {
return [
copy.sheetName ?? t('editor.draft.copy.unreadable'),
copy.revision === null
? null
: t('editor.draft.copy.revision').replace(
'{revision}',
String(copy.revision),
),
copy.writtenAt === null ? null : formatParisTime(copy.writtenAt, locale),
]
.filter(Boolean)
.join(' · ')
}
/**
* The write failed (C28: cap of two other copies, or a full storage): the account's other
* copies are listed, each openable and abandonable (confirmed); abandoning writes the
* current copy again at once. Nothing is evicted by itself.
*/
export default function CopyIssueNotice() {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const issue = useEditorStore((s) => s.copyIssue)
const copies = useEditorStore((s) => s.otherCopies)
const userId = useEditorStore((s) => s.userId)
const [asking, setAsking] = useState<StoredCopyInfo | null>(null)
if (issue === null) return null
const abandon = () => {
if (asking)
useEditorStore
.getState()
.abandonOtherCopy(asking.versionId, { confirmed: true })
setAsking(null)
}
return (
<>
<Notice tone="error">
<p>{t(`editor.draft.copy_issue.${issue}`)}</p>
<ul className="mt-2 space-y-2">
{copies.map((copy) => {
const sheetId = userId ? copySheetId(userId, copy.versionId) : null
return (
<li
key={copy.versionId}
className="flex flex-wrap items-center gap-2"
>
<span>{copyLine(t, locale, copy)}</span>
{sheetId && (
<Link
to={`/app/admin/sheets/${sheetId}/draft`}
className="text-sm underline"
>
{t('editor.draft.copy.open')}
</Link>
)}
<Button
type="button"
size="sm"
variant="secondary"
onClick={() => setAsking(copy)}
>
{t('editor.draft.copy.abandon')}
</Button>
</li>
)
})}
</ul>
{issue === 'quota' && (
<Button
type="button"
size="sm"
variant="secondary"
className="mt-2"
onClick={() => useEditorStore.getState().retryCopyWrite()}
>
{t('common.retry')}
</Button>
)}
</Notice>
<ConfirmDialog
open={asking !== null}
title={t('editor.draft.copy.abandon')}
body={
asking?.revision != null && asking.writtenAt !== null
? t('editor.draft.recovery.ignore_body')
.replace('{revision}', String(asking.revision))
.replace('{time}', formatParisTime(asking.writtenAt, locale))
: t('editor.draft.recovery.ignore_unreadable_body')
}
confirmLabel={t('editor.draft.recovery.ignore_confirm')}
cancelLabel={t('common.cancel')}
onConfirm={abandon}
onCancel={() => setAsking(null)}
/>
</>
)
}