File
Blob: src/client/lib/share-page-model.ts
| 1 | import type { ActivePageSnapshot, ActivePageState } from "@/client/lib/active-page-model"; |
| 2 | import type { PageAncestor, PageKind, SharePermission } from "@/shared/types"; |
| 3 | |
| 4 | export interface ShareRootPage { |
| 5 | id: string; |
| 6 | kind: PageKind; |
| 7 | title: string; |
| 8 | icon: string | null; |
| 9 | cover_url: string | null; |
| 10 | permission: SharePermission; |
| 11 | } |
| 12 | |
| 13 | export interface SharePagePresentation { |
| 14 | activePageId: string; |
| 15 | isRootActive: boolean; |
| 16 | isPageLoading: boolean; |
| 17 | isAncestorTrailLoading: boolean; |
| 18 | page: ActivePageSnapshot | null; |
| 19 | ancestors: PageAncestor[]; |
| 20 | displayTitle: string; |
| 21 | displayIcon: string | null; |
| 22 | displayCoverUrl: string | null; |
| 23 | isViewOnly: boolean; |
| 24 | } |
| 25 | |
| 26 | export function deriveSharePagePresentation( |
| 27 | rootPage: ShareRootPage, |
| 28 | activePageId: string, |
| 29 | activePageState: ActivePageState, |
| 30 | ): SharePagePresentation { |
| 31 | const isRootActive = activePageId === rootPage.id; |
| 32 | const readyState = |
| 33 | activePageState.kind === "ready" && activePageState.snapshot.id === activePageId ? activePageState : null; |
| 34 | |
| 35 | return { |
| 36 | activePageId, |
| 37 | isRootActive, |
| 38 | isPageLoading: !readyState && activePageState.kind !== "unavailable", |
| 39 | isAncestorTrailLoading: readyState?.ancestorsStatus === "loading", |
| 40 | page: readyState?.snapshot ?? null, |
| 41 | ancestors: readyState?.ancestors ?? [], |
| 42 | displayTitle: readyState?.snapshot.title ?? "", |
| 43 | displayIcon: readyState?.snapshot.icon ?? null, |
| 44 | displayCoverUrl: readyState?.snapshot.coverUrl ?? null, |
| 45 | isViewOnly: readyState ? readyState.access.mode === "view" : rootPage.permission === "view", |
| 46 | }; |
| 47 | } |