SPB Git

Repository Public

dashboard.ts 5.4 KB 98 lines typescript

1// SP Mail Next — centre de commande : disposition des widgets (module pur, sans dépendance) : ordre, visibilité, largeur, normalisation.23/** Widgets connus, dans l'ordre par défaut. Un widget ajouté plus tard se place à la fin des dispositions déjà enregistrées. */4export const WIDGET_IDS = ["attention", "inbox", "pending", "scheduled", "attachments", "contacts", "storage", "issues", "sync"] as const5export type WidgetId = (typeof WIDGET_IDS)[number]67export interface WidgetLayout {8  id: WidgetId9  hidden: boolean10  /** largeur double (2×) sur la grille de 12 colonnes */11  wide: boolean12}13export type DashboardLayout = WidgetLayout[]1415const DEFAULT_WIDE: readonly WidgetId[] = ["attention", "pending"]16const isWidgetId = (v: unknown): v is WidgetId => typeof v === "string" && (WIDGET_IDS as readonly string[]).includes(v)17const defaultOf = (id: WidgetId): WidgetLayout => ({ id, hidden: false, wide: DEFAULT_WIDE.includes(id) })1819export const defaultLayout = (): DashboardLayout => WIDGET_IDS.map(defaultOf)2021/**22 * Ne garde d'une valeur enregistrée (préférence synchronisée, donc potentiellement écrite par une autre version) que ce qui est23 * valide : identifiants connus, une seule fois, drapeaux booléens ; les widgets absents sont ajoutés à la fin avec leurs défauts.24 */25export function normalizeLayout(raw: unknown): DashboardLayout {26  const out: DashboardLayout = []27  const seen = new Set<WidgetId>()28  if (Array.isArray(raw)) {29    for (const item of raw.slice(0, 64)) {30      if (!item || typeof item !== "object") continue31      const r = item as Record<string, unknown>32      if (!isWidgetId(r.id) || seen.has(r.id)) continue33      seen.add(r.id)34      out.push({ id: r.id, hidden: r.hidden === true, wide: typeof r.wide === "boolean" ? r.wide : DEFAULT_WIDE.includes(r.id) })35    }36  }37  for (const id of WIDGET_IDS) if (!seen.has(id)) out.push(defaultOf(id))38  return out39}4041export const sameLayout = (a: DashboardLayout, b: DashboardLayout): boolean => a.length === b.length && a.every((w, i) => w.id === b[i].id && w.hidden === b[i].hidden && w.wide === b[i].wide)42export const isDefaultLayout = (l: DashboardLayout): boolean => sameLayout(l, defaultLayout())4344/** Widgets affichés, dans l'ordre ; `available` écarte ceux dont le serveur n'offre pas la fonction. */45export function visibleWidgets(layout: DashboardLayout, available: (id: WidgetId) => boolean = () => true): WidgetLayout[] {46  return layout.filter((w) => !w.hidden && available(w.id))47}48export function hiddenWidgets(layout: DashboardLayout, available: (id: WidgetId) => boolean = () => true): WidgetLayout[] {49  return layout.filter((w) => w.hidden && available(w.id))50}5152/**53 * Déplace un widget d'un cran parmi les widgets AFFICHÉS (les masqués et les indisponibles ne comptent pas : « monter » échange54 * toujours avec le voisin que l'on voit). Rend la même référence si rien ne bouge.55 */56export function moveWidget(layout: DashboardLayout, id: WidgetId, delta: -1 | 1, available: (id: WidgetId) => boolean = () => true): DashboardLayout {57  const shown = visibleWidgets(layout, available)58  const at = shown.findIndex((w) => w.id === id)59  const target = shown[at + delta]60  if (at < 0 || !target) return layout61  return moveWidgetBefore(layout, id, delta < 0 ? target.id : (shown[at + 2]?.id ?? null), target.id)62}6364/** Place `id` juste avant `beforeId` (null : après `afterId`, ou à la fin). Rend la même référence si l'ordre ne change pas. */65export function moveWidgetBefore(layout: DashboardLayout, id: WidgetId, beforeId: WidgetId | null, afterId: WidgetId | null = null): DashboardLayout {66  const item = layout.find((w) => w.id === id)67  if (!item || beforeId === id) return layout68  const rest = layout.filter((w) => w.id !== id)69  let at = beforeId ? rest.findIndex((w) => w.id === beforeId) : -170  if (at < 0) at = afterId ? rest.findIndex((w) => w.id === afterId) + 1 || rest.length : rest.length71  const next = [...rest.slice(0, at), item, ...rest.slice(at)]72  return next.every((w, i) => w.id === layout[i].id) ? layout : next73}7475/** Glisser-déposer : `id` prend la place de `overId` (devant s'il venait d'après, derrière s'il venait d'avant). */76export function dropWidgetOn(layout: DashboardLayout, id: WidgetId, overId: WidgetId): DashboardLayout {77  const from = layout.findIndex((w) => w.id === id)78  const to = layout.findIndex((w) => w.id === overId)79  if (from < 0 || to < 0 || from === to) return layout80  const next = layout.filter((w) => w.id !== id)81  next.splice(to, 0, layout[from])82  return next83}8485const patch = (layout: DashboardLayout, id: WidgetId, p: Partial<WidgetLayout>): DashboardLayout => {86  const cur = layout.find((w) => w.id === id)87  if (!cur || (Object.keys(p) as (keyof WidgetLayout)[]).every((k) => cur[k] === p[k])) return layout88  return layout.map((w) => (w.id === id ? { ...w, ...p } : w))89}90export const setWidgetHidden = (layout: DashboardLayout, id: WidgetId, hidden: boolean): DashboardLayout => patch(layout, id, { hidden })91export const setWidgetWide = (layout: DashboardLayout, id: WidgetId, wide: boolean): DashboardLayout => patch(layout, id, { wide })9293/** Rang (à partir de 1) d'un widget parmi les affichés, pour les annonces aux lecteurs d'écran. */94export function widgetRank(layout: DashboardLayout, id: WidgetId, available: (id: WidgetId) => boolean = () => true): { rank: number; of: number } {95  const shown = visibleWidgets(layout, available)96  return { rank: shown.findIndex((w) => w.id === id) + 1, of: shown.length }97}98
Files main
Menu

Theme