93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
import type {
|
|
OfficialSheetAdmin,
|
|
PlatformRole,
|
|
SheetAdminRound,
|
|
SheetFailureKind,
|
|
} from '@faunosteo/supabase'
|
|
|
|
/**
|
|
* Why the draft is edited or only read (plan « Modes, permissions, layout »). A display
|
|
* rule: the server refuses every write it does not allow, this only decides what to show.
|
|
*/
|
|
export type EditorMode =
|
|
| 'edit'
|
|
| 'narrow'
|
|
| 'rights'
|
|
| 'frozen'
|
|
| 'historical'
|
|
| 'inactive'
|
|
|
|
/** Editing needs a viewport at least this wide (L15). */
|
|
export const MIN_EDIT_WIDTH = 1024
|
|
|
|
type ModeSheet = Pick<
|
|
OfficialSheetAdmin,
|
|
'status' | 'historical' | 'rounds' | 'draft_version'
|
|
>
|
|
|
|
/** Round states that freeze the draft on the server (`draft.frozen`). */
|
|
const FREEZING_ROUNDS: ReadonlySet<SheetAdminRound['status']> = new Set([
|
|
'verifying',
|
|
'open',
|
|
'approved',
|
|
])
|
|
|
|
/** The round that freezes the draft being edited, if any. */
|
|
export function freezingRound(sheet: ModeSheet): SheetAdminRound | null {
|
|
const draftId = sheet.draft_version?.version_id
|
|
const found = sheet.rounds.find(
|
|
(round) =>
|
|
round.version_id === draftId && FREEZING_ROUNDS.has(round.status),
|
|
)
|
|
return found ?? null
|
|
}
|
|
|
|
/** `grant_active` needs the editor capability AND the admin or owner role. */
|
|
export function hasEditRight(
|
|
editorGrant: boolean,
|
|
roles: readonly PlatformRole[],
|
|
): boolean {
|
|
return editorGrant && (roles.includes('admin') || roles.includes('owner'))
|
|
}
|
|
|
|
/**
|
|
* The reason the draft cannot be edited, strongest first: the account's own rights, then
|
|
* the sheet (archived, historical), the freezing round, last the screen width.
|
|
*/
|
|
export function editorMode(input: {
|
|
hasRight: boolean
|
|
sheet: ModeSheet
|
|
wide: boolean
|
|
}): EditorMode {
|
|
const { sheet } = input
|
|
if (!input.hasRight) return 'rights'
|
|
if (sheet.status !== 'active') return 'inactive'
|
|
if (sheet.historical) return 'historical'
|
|
if (freezingRound(sheet)) return 'frozen'
|
|
return input.wide ? 'edit' : 'narrow'
|
|
}
|
|
|
|
/**
|
|
* A refused save names the state the page missed: the matching read-only mode replaces
|
|
* the edit mode (the page never retries or sends again by itself).
|
|
*/
|
|
export function modeAfterSave(
|
|
mode: EditorMode,
|
|
failure: SheetFailureKind | null,
|
|
): EditorMode {
|
|
if (mode !== 'edit' || failure === null) return mode
|
|
if (failure === 'draft.frozen') return 'frozen'
|
|
if (failure === 'sheet.inactive') return 'inactive'
|
|
return failure.startsWith('rights.') ? 'rights' : mode
|
|
}
|
|
|
|
/** Whether the sheet's draft can be edited at a wide enough screen: « Ouvrir » or « Consulter ». */
|
|
export function draftEditable(hasRight: boolean, sheet: ModeSheet): boolean {
|
|
return editorMode({ hasRight, sheet, wide: true }) === 'edit'
|
|
}
|
|
|
|
/** The store may use a recovery copy where the account could save (also at a narrow screen). */
|
|
export function storeCanEdit(mode: EditorMode): boolean {
|
|
return mode === 'edit' || mode === 'narrow'
|
|
}
|