SPB Git

Repository Public

labels.setup.tsx 6.0 KB 89 lines tsx

1// SP Mail Next — inscription du module « labels » dans la coquille : section de la barre latérale, pastilles des lignes,2// pastilles du lecteur, sélecteur (barre de sélection, menu contextuel, touche « l »), palette « # », commandes.3import { lazy } from "react"4import { t } from "../../i18n"5import type { Setup } from "../../next/features"6import { allLabels, filterLabels, labelColor, labelsCollection, labelsOfKeywords, type Label } from "../../next/labels"7import { LabelUi, applyLabel, confirmDeleteLabel, labelHref, moveLabelBy, openLabelCreate, openLabelEdit, openLabelManage, openLabelPicker, openLabelPickerForCurrent, openLabelSearch } from "../../next/labelsUi"8import { operator } from "../../next/nav"9import { registerRowMenuItems, rowTargets } from "../../next/rowmenu"10import { S, toast } from "../../state/app"11import { registerOverlay, registerPaletteProvider, registerReaderAction, registerReaderPanel, registerRowLabels, registerSelectionAction, registerShortcut, registerSidebarAction, registerSidebarEntry } from "../../state/registry"12import { useStore } from "../../state/store"13import { registerCommand } from "../commands"14import { ensureRowMenu } from "./RowMenuHost"1516const LabelDialogs = lazy(() => import("./LabelDialogs"))17const LabelChips = lazy(() => import("./LabelChips"))18function LabelHost() {19  const open = useStore(LabelUi, (s) => !!(s.picker || s.edit || s.manage))20  return open ? <LabelDialogs /> : null21}2223function entryMenu(label: Label, index: number, count: number) {24  const offline = !S.get().online25  const say = (msg: string | null) => msg && toast({ kind: "info", text: msg })26  return [27    { label: t("nx.labels.rename"), icon: "edit" as const, disabled: offline, onSelect: () => openLabelEdit(label) },28    { label: t("nx.labels.moveUp"), icon: "arrowUp" as const, disabled: offline || index === 0, onSelect: () => void moveLabelBy(label, -1).then(say) },29    { label: t("nx.labels.moveDown"), icon: "arrowDown" as const, disabled: offline || index === count - 1, onSelect: () => void moveLabelBy(label, 1).then(say) },30    "sep" as const,31    { label: t("nx.labels.manage"), icon: "tag" as const, onSelect: openLabelManage },32    { label: t("nx.labels.delete"), icon: "trash" as const, danger: true, disabled: offline, onSelect: () => void confirmDeleteLabel(label) },33  ]34}3536export const setupLabels: Setup = () => {37  const undo: (() => void)[] = []38  // les entrées de la barre latérale et le fournisseur de pastilles suivent la collection (et la boîte affichée, pour les adresses)39  let dynamic: (() => void)[] = []40  let sig: string | null = null41  const sync = () => {42    const s = S.get()43    const labels = allLabels()44    const next = `${s.mb}|${s.route.name === "mail" ? s.route.mbIndex : ""}|${labels.map((l) => `${l.id}:${l.name}:${l.color}:${l.position}`).join(",")}`45    if (next === sig) return46    sig = next47    dynamic.splice(0).forEach((off) => off())48    labels.forEach((l, i) =>49      dynamic.push(50        registerSidebarEntry({51          id: `label:${l.id}`, slot: "labels", order: i, icon: "tag", color: labelColor(l.color), label: () => l.name, href: labelHref(l),52          active: (st) => st.route.name === "mail" && st.view === "search" && st.query === operator("label", l.name),53          onDropIds: (ids) => {54            const mb = S.get().mb55            if (mb) void applyLabel(l, true, rowTargets(mb, ids), true)56          },57          menu: () => entryMenu(l, i, labels.length),58        }),59      ),60    )61    // nouvelle fonction à chaque changement : les lignes déjà affichées se redessinent avec les libellés à jour62    dynamic.push(registerRowLabels((email) => labelsOfKeywords(email.keywords, labels).map((l) => ({ id: l.id, name: l.name, color: labelColor(l.color) }))))63  }64  sync()65  undo.push(labelsCollection.store.subscribe(sync), S.subscribe(sync), () => dynamic.splice(0).forEach((off) => off()))66  void labelsCollection.refresh()6768  undo.push(69    registerSidebarAction({ slot: "labels", icon: "plus", label: () => t("nx.labels.new"), run: () => openLabelCreate() }),70    registerOverlay({ id: "nx.labels", when: () => true, component: LabelHost }),71    registerReaderPanel({ id: "nx.labels", slot: "subject", component: LabelChips, order: 0 }),72    registerSelectionAction({73      id: "label", icon: "tag", label: () => t("nx.labels.action"), shortcut: "l", order: 10,74      run: (ids, mb) => void openLabelPicker(rowTargets(mb, ids)),75      runGroups: (groups) => void openLabelPicker({ groups, exact: true, unified: true }),76    }),77    registerReaderAction({ id: "label", icon: "tag", label: () => t("nx.labels.action"), order: 10, run: (ids) => void (S.get().mb && openLabelPicker(rowTargets(S.get().mb!, ids, true))) }),78    registerShortcut({ id: "label", keys: "l", label: () => t("nx.labels.actionKeys"), run: () => void openLabelPickerForCurrent(), when: (s) => s.route.name === "mail", order: 10 }),79    registerPaletteProvider("#", (query) => filterLabels(allLabels(), query).map((l) => ({ id: `label:${l.id}`, title: l.name, subtitle: t("nx.labels.search"), icon: "tag", run: () => openLabelSearch(l) }))),80    registerCommand(() => ({ id: "labels.apply", section: "actions", title: t("nx.labels.actionKeys"), icon: "tag", keys: "l", run: () => void openLabelPickerForCurrent(), when: (s) => s.route.name === "mail" && (s.selected.size > 0 || !!s.open || !!s.cursor || s.all), keywords: ["label", "libellé", "étiquette", "tag"] })),81    registerCommand(() => ({ id: "labels.new", section: "actions", title: t("nx.labels.new"), icon: "plus", run: () => openLabelCreate(), keywords: ["label", "libellé", "étiquette", "tag"] })),82    registerCommand(() => ({ id: "labels.manage", section: "settings", title: t("nx.labels.manage"), icon: "tag", run: openLabelManage, keywords: ["label", "libellé", "étiquette", "ordre"] })),83    registerRowMenuItems((targets) => [{ id: "label", icon: "tag", label: `${t("nx.labels.action")}…`, run: () => void openLabelPicker(targets) }], 10),84    ensureRowMenu(),85    () => LabelUi.set({ picker: null, edit: null, manage: false }),86  )87  return () => undo.splice(0).forEach((off) => off())88}89
Files main
Menu

Theme