ultisuite-client/lib/label-edits.ts
R3D347HR4Y c87670e90f
Some checks failed
E2E / Playwright e2e (push) Has been cancelled
feat(api): offline-first mail sync w/ TanStack Query
Move mail, compose, contacts, and accounts off mocks onto REST + WS.
Add client, auth store, IDB-backed query cache, offline queue, and
sync bar; hybrid Zustand for UI-only state. Settings still local until
backend has preferences API.
2026-05-23 00:04:28 +02:00

58 lines
1.7 KiB
TypeScript

import type { Email } from "@/lib/email-data"
export type LabelEditState = {
additions: Record<string, string[]>
removals: Record<string, string[]>
}
export function effectiveLabels(
email: Email | undefined,
additions: Record<string, string[]>,
removals: Record<string, string[]>
): string[] {
if (!email) return []
const id = email.id
let labels = [...(email.labels ?? [])]
for (const l of additions[id] ?? []) {
if (!labels.some((x) => x.toLowerCase() === l.toLowerCase())) {
labels.push(l)
}
}
for (const r of removals[id] ?? []) {
labels = labels.filter((x) => x.toLowerCase() !== r.toLowerCase())
}
return labels
}
export function mergeEmailLabelEdits(
email: Email,
edits: LabelEditState
): Email {
const adds = edits.additions[email.id] ?? []
const rems = edits.removals[email.id] ?? []
if (!adds.length && !rems.length) return email
const labels = effectiveLabels(email, edits.additions, edits.removals)
return { ...email, labels }
}
/** Marquage local « non-spam » : retire le flag spam, enlève le libellé spam et assure la boîte de réception. */
export function mergeEmailNotSpam(
email: Email,
notSpamEmailIds: readonly string[]
): Email {
const set = new Set(notSpamEmailIds)
if (!set.has(email.id)) return email
const ls = [...(email.labels ?? [])]
const noSpam = ls.filter((l) => l.toLowerCase() !== "spam")
const hasInbox = noSpam.some((l) => l.toLowerCase() === "inbox")
const labels = hasInbox ? noSpam : [...noSpam, "inbox"]
return { ...email, spam: false, labels }
}
export function applyLabelEditsToEmails(
emails: Email[],
edits: LabelEditState
): Email[] {
return emails.map((e) => mergeEmailLabelEdits(e, edits))
}