ultisuite-client/lib/mail-folder-display.ts
2026-05-15 17:40:17 +02:00

83 lines
2.2 KiB
TypeScript

import type { FolderTreeNode } from "@/lib/sidebar-nav-maps"
import { findFolderPath } from "@/lib/sidebar-nav-folder-ids"
import { getMailNavFolderLabel } from "@/lib/sidebar-nav-data"
export const INBOX_CATEGORY_TAB_LABELS: Record<string, string> = {
primary: "Principale",
promotions: "Promotions",
social: "Réseaux sociaux",
updates: "Notifications",
forums: "Forums",
}
/** Clé stable pour historique navigation (dossier + onglet boîte de réception). */
export function mailNavVisitKey(folderId: string, inboxTab?: string): string {
if (folderId === "inbox" && inboxTab && inboxTab !== "primary") {
return `inbox::${inboxTab}`
}
return folderId
}
export function parseMailNavVisitKey(key: string): {
folderId: string
inboxTab?: string
} {
if (key.startsWith("inbox::")) {
return { folderId: "inbox", inboxTab: key.slice("inbox::".length) }
}
return { folderId: key }
}
export function getMailNavFolderBreadcrumbSegments(
folderId: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>,
inboxCategoryLabel?: string | null
): string[] {
if (folderId === "inbox") {
const base = getMailNavFolderLabel(folderId, folderIdToLabel)
if (
inboxCategoryLabel &&
inboxCategoryLabel !== INBOX_CATEGORY_TAB_LABELS.primary
) {
return [base, inboxCategoryLabel]
}
return [base]
}
const path = findFolderPath(folderTree, folderId)
if (path?.length) {
return path.map((n) => n.label)
}
return [getMailNavFolderLabel(folderId, folderIdToLabel)]
}
export function breadcrumbSegmentsForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>
): string[] {
const { folderId, inboxTab } = parseMailNavVisitKey(key)
const cat =
folderId === "inbox" && inboxTab
? INBOX_CATEGORY_TAB_LABELS[inboxTab] ?? inboxTab
: null
return getMailNavFolderBreadcrumbSegments(
folderId,
folderTree,
folderIdToLabel,
cat
)
}
export function breadcrumbForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>
): string {
return breadcrumbSegmentsForVisitKey(key, folderTree, folderIdToLabel).join(
" · "
)
}