ultisuite-client/lib/mail-folder-display.ts
2026-05-16 20:30:50 +02:00

128 lines
3.5 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"
/** @deprecated Utiliser `getMailNavFolderLabel(inboxTab, folderIdToLabel)` ou `inboxTabDisplayLabel`. */
export const INBOX_CATEGORY_TAB_LABELS: Record<string, string> = {
primary: "Principale",
}
/** 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 type MailNavBreadcrumbItem = {
label: string
visitKey: string
}
export function getMailNavFolderBreadcrumbItems(
folderId: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>,
inboxCategory?: { tabId: string; label: string } | null
): MailNavBreadcrumbItem[] {
if (folderId === "inbox") {
const base = getMailNavFolderLabel(folderId, folderIdToLabel)
if (inboxCategory && inboxCategory.label !== "Principale") {
return [
{ label: base, visitKey: mailNavVisitKey("inbox") },
{
label: inboxCategory.label,
visitKey: mailNavVisitKey("inbox", inboxCategory.tabId),
},
]
}
return [{ label: base, visitKey: mailNavVisitKey(folderId) }]
}
const path = findFolderPath(folderTree, folderId)
if (path?.length) {
return path.map((n) => ({ label: n.label, visitKey: n.id }))
}
return [
{
label: getMailNavFolderLabel(folderId, folderIdToLabel),
visitKey: folderId,
},
]
}
export function getMailNavFolderBreadcrumbSegments(
folderId: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>,
inboxCategoryLabel?: string | null,
inboxTabId?: string | null
): string[] {
const inboxCategory =
folderId === "inbox" &&
inboxTabId &&
inboxCategoryLabel &&
inboxCategoryLabel !== "Principale"
? { tabId: inboxTabId, label: inboxCategoryLabel }
: null
return getMailNavFolderBreadcrumbItems(
folderId,
folderTree,
folderIdToLabel,
inboxCategory
).map((i) => i.label)
}
export function breadcrumbItemsForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>
): MailNavBreadcrumbItem[] {
const { folderId, inboxTab } = parseMailNavVisitKey(key)
const inboxCategory =
folderId === "inbox" && inboxTab && inboxTab !== "primary"
? {
tabId: inboxTab,
label: getMailNavFolderLabel(inboxTab, folderIdToLabel),
}
: null
return getMailNavFolderBreadcrumbItems(
folderId,
folderTree,
folderIdToLabel,
inboxCategory
)
}
export function breadcrumbSegmentsForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>
): string[] {
return breadcrumbItemsForVisitKey(key, folderTree, folderIdToLabel).map(
(i) => i.label
)
}
export function breadcrumbForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>
): string {
return breadcrumbSegmentsForVisitKey(key, folderTree, folderIdToLabel).join(
" · "
)
}