Skip to content
File

Blob: src/client/lib/page-archive.ts

typescript40 lines
1import { DEFAULT_PAGE_TITLE } from "@/shared/constants";
2import type { PageTreeIndex } from "@/client/lib/page-tree-model";
3 
4export function getActiveDescendantIds(index: Pick<PageTreeIndex, "childrenByParent">, pageId: string): string[] {
5 const result: string[] = [];
6 const stack = [...(index.childrenByParent.get(pageId) ?? [])].reverse();
7 while (stack.length > 0) {
8 const page = stack.pop()!;
9 result.push(page.id);
10 const children = index.childrenByParent.get(page.id);
11 if (children) {
12 for (let i = children.length - 1; i >= 0; i -= 1) {
13 stack.push(children[i]);
14 }
15 }
16 }
17 return result;
18}
19 
20export function getArchivePageConfirmMessage(title: string | null | undefined, descendantCount: number): string {
21 const pageTitle = title || DEFAULT_PAGE_TITLE;
22 
23 if (descendantCount === 1) {
24 return `"${pageTitle}" and 1 subpage will be archived.`;
25 }
26 
27 if (descendantCount > 1) {
28 return `"${pageTitle}" and ${descendantCount} subpages will be archived.`;
29 }
30 
31 return `"${pageTitle}" will be archived.`;
32}
33 
34export function shouldNavigateAwayAfterArchive(
35 currentPageId: string | null | undefined,
36 archivedPageIds: readonly string[],
37): boolean {
38 return !!currentPageId && archivedPageIds.includes(currentPageId);
39}