ultisuite-client/lib/mail-folder-display.ts

147 lines
3.8 KiB
TypeScript

import type { FolderTreeNode } from "@/lib/sidebar-nav-maps"
import { findFolderPath } from "@/lib/sidebar-nav-folder-ids"
import {
defaultNavLabelRowsSnapshot,
getMailNavFolderLabel,
inboxTabDisplayLabel,
type LabelRowItem,
} from "@/lib/sidebar-nav-data"
/** @deprecated Utiliser `getMailNavFolderLabel(inboxTab, folderIdToLabel)` ou `inboxTabDisplayLabel`. */
export const INBOX_CATEGORY_TAB_LABELS: Record<string, string> = {
primary: "Principale",
all: "Tous les messages",
}
/** 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>,
labelRows: readonly LabelRowItem[] = defaultNavLabelRowsSnapshot
): MailNavBreadcrumbItem[] {
const { folderId, inboxTab } = parseMailNavVisitKey(key)
const inboxCategory =
folderId === "inbox" && inboxTab && inboxTab !== "primary"
? {
tabId: inboxTab,
label: inboxTabDisplayLabel(
inboxTab,
labelRows,
folderIdToLabel ?? {}
),
}
: null
return getMailNavFolderBreadcrumbItems(
folderId,
folderTree,
folderIdToLabel,
inboxCategory
)
}
export function breadcrumbSegmentsForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>,
labelRows?: readonly LabelRowItem[]
): string[] {
return breadcrumbItemsForVisitKey(
key,
folderTree,
folderIdToLabel,
labelRows
).map((i) => i.label)
}
export function breadcrumbForVisitKey(
key: string,
folderTree: FolderTreeNode[],
folderIdToLabel?: Record<string, string>,
labelRows?: readonly LabelRowItem[]
): string {
return breadcrumbSegmentsForVisitKey(
key,
folderTree,
folderIdToLabel,
labelRows
).join(" · ")
}