File
Blob: src/client/lib/affordance/sidebar.ts
| 1 | import { getPageStructureEntitlements, type ResolvedWorkspaceRole } from "@/shared/entitlements"; |
| 2 | import { |
| 3 | ENABLED_ACTION, |
| 4 | HIDDEN_ACTION, |
| 5 | disabledAction, |
| 6 | OFFLINE_ACTION_REASON, |
| 7 | type UiActionState, |
| 8 | } from "@/client/lib/affordance/action-state"; |
| 9 | import { resolveArchiveAffordance } from "@/client/lib/affordance/archive"; |
| 10 | |
| 11 | export interface SidebarBaseAffordance { |
| 12 | createPage: UiActionState; |
| 13 | } |
| 14 | |
| 15 | export interface SidebarRowAffordance { |
| 16 | createSubpage: UiActionState; |
| 17 | movePage: UiActionState; |
| 18 | archivePage: UiActionState; |
| 19 | } |
| 20 | |
| 21 | export function deriveSidebarBaseAffordance(input: { |
| 22 | workspaceRole: ResolvedWorkspaceRole; |
| 23 | online: boolean; |
| 24 | }): SidebarBaseAffordance { |
| 25 | const { workspaceRole, online } = input; |
| 26 | const entitlements = getPageStructureEntitlements(workspaceRole, false); |
| 27 | |
| 28 | return { |
| 29 | createPage: entitlements.createPage |
| 30 | ? online |
| 31 | ? ENABLED_ACTION |
| 32 | : disabledAction(OFFLINE_ACTION_REASON) |
| 33 | : HIDDEN_ACTION, |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | export function deriveSidebarRowAffordance(input: { |
| 38 | workspaceRole: ResolvedWorkspaceRole; |
| 39 | ownsPage: boolean; |
| 40 | online: boolean; |
| 41 | }): SidebarRowAffordance { |
| 42 | const { workspaceRole, ownsPage, online } = input; |
| 43 | const entitlements = getPageStructureEntitlements(workspaceRole, ownsPage); |
| 44 | |
| 45 | return { |
| 46 | createSubpage: entitlements.createPage |
| 47 | ? online |
| 48 | ? ENABLED_ACTION |
| 49 | : disabledAction(OFFLINE_ACTION_REASON) |
| 50 | : HIDDEN_ACTION, |
| 51 | movePage: entitlements.movePage ? (online ? ENABLED_ACTION : disabledAction(OFFLINE_ACTION_REASON)) : HIDDEN_ACTION, |
| 52 | archivePage: resolveArchiveAffordance({ |
| 53 | archiveAnyPage: entitlements.archiveAnyPage, |
| 54 | archiveOwnPage: entitlements.archiveOwnPage, |
| 55 | ownsPage, |
| 56 | online, |
| 57 | }), |
| 58 | }; |
| 59 | } |