54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import type { PreviewState, SheetView } from '@faunosteo/core-wasm/types'
|
|
import type { CatalogueFailure } from '@/catalogue/failure'
|
|
import { rejectionReason } from './drawingReason'
|
|
import { loadFailureMessage } from './messages'
|
|
|
|
/** The state of the drawing a view names itself (not the one it inherits). */
|
|
export type DrawingStatus =
|
|
| { kind: 'none' }
|
|
| { kind: 'ok' }
|
|
| { kind: 'missing'; detail: null }
|
|
| { kind: 'rejected'; detail: string | null }
|
|
| { kind: 'failed'; failure: CatalogueFailure }
|
|
|
|
export function drawingStatusOf(
|
|
view: SheetView,
|
|
state: PreviewState,
|
|
failures: Readonly<Record<string, CatalogueFailure>>,
|
|
): DrawingStatus {
|
|
const assetId = view.svg_asset_id
|
|
if (assetId === null) return { kind: 'none' }
|
|
const held = state.drawings.find((drawing) => drawing.asset_id === assetId)
|
|
if (held?.state === 'ok') return { kind: 'ok' }
|
|
if (held?.state === 'rejected')
|
|
return { kind: 'rejected', detail: held.detail }
|
|
// A drawing the handle refused at load is `missing` for the core: the failure says why.
|
|
const failure = failures[assetId]
|
|
return failure
|
|
? { kind: 'failed', failure }
|
|
: { kind: 'missing', detail: null }
|
|
}
|
|
|
|
type Translate = (key: string) => string
|
|
|
|
export function drawingStatusText(t: Translate, status: DrawingStatus): string {
|
|
if (status.kind === 'failed') return failedText(t, status.failure)
|
|
if (status.kind === 'rejected') return rejectedText(t, status.detail)
|
|
return t(`editor.views.drawing.${status.kind}`)
|
|
}
|
|
|
|
function failedText(t: Translate, failure: CatalogueFailure): string {
|
|
return failure === 'drawing_hash_mismatch'
|
|
? t('editor.views.drawing.hash_mismatch')
|
|
: loadFailureMessage(t, failure)
|
|
}
|
|
|
|
function rejectedText(t: Translate, detail: string | null): string {
|
|
const reason = rejectionReason(detail)
|
|
if (reason === 'not_canonical') return t('editor.views.drawing.not_canonical')
|
|
return t('editor.views.drawing.rejected_reason').replace(
|
|
'{reason}',
|
|
t(`editor.upload.reason.${reason}`),
|
|
)
|
|
}
|