Skip to content
File

Blob: src/client/components/editor/lib/page-mention/view-state.ts

typescript55 lines
1import { DEFAULT_PAGE_TITLE } from "@/shared/constants";
2import type { MentionEntry } from "@/client/components/page-mention/resolver";
3 
4export type PageMentionViewState =
5 | {
6 kind: "pending";
7 interactive: false;
8 label: "Pending...";
9 ariaLabel: "Pending page mention";
10 }
11 | {
12 kind: "restricted";
13 interactive: false;
14 label: "Restricted";
15 ariaLabel: "Restricted page mention";
16 }
17 | {
18 kind: "accessible";
19 interactive: true;
20 label: string;
21 ariaLabel: string;
22 icon: string | null;
23 showFallbackIcon: boolean;
24 };
25 
26export function getPageMentionViewState(entry: MentionEntry): PageMentionViewState {
27 if (entry.status !== "resolved") {
28 return {
29 kind: "pending",
30 interactive: false,
31 label: "Pending...",
32 ariaLabel: "Pending page mention",
33 };
34 }
35 
36 if (!entry.accessible) {
37 return {
38 kind: "restricted",
39 interactive: false,
40 label: "Restricted",
41 ariaLabel: "Restricted page mention",
42 };
43 }
44 
45 const label = entry.title ?? DEFAULT_PAGE_TITLE;
46 return {
47 kind: "accessible",
48 interactive: true,
49 label,
50 ariaLabel: label,
51 icon: entry.icon,
52 showFallbackIcon: !entry.icon,
53 };
54}