Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Command Center (MCC)8 * File : apps/web/src/features/maintenance/parts.tsx9 * Module : features10 * Repository : https://git.spboucher.ai/maclustr-command-center11 * Project Website : https://clustr.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-1814 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142"use client";43import Link from "next/link";44import type { Schemas } from "@/api/client";45import { useI18n } from "@/i18n";46import { Icon, StateDot, cx, maintenanceTone, type IconName, type Tone } from "@/ui";47import type { DaySegment, Occurrence } from "./calendar";4849type Category = Schemas["MaintenanceCategory"];50type Status = Schemas["MaintenanceStatus"];5152/** Category → tone (existing state tokens) and icon. The category is always written out as text as well. */53export const CATEGORY_TONE: Record<Category, Tone> = { planned: "info", update: "maintenance", hardware: "warning", inspection: "stale", emergency: "critical" };54export const CATEGORY_ICON: Record<Category, IconName> = { planned: "calendar", update: "refresh", hardware: "wrench", inspection: "eye", emergency: "bolt" };55export const STATUS_ICON: Record<Status, IconName> = { scheduled: "clock", in_progress: "running", completed: "ok", cancelled: "offline" };5657export const occurrenceHref = (o: Occurrence): string => `/maintenance/${o.window.id}?occurrence=${encodeURIComponent(o.occurrence_start)}`;5859/** Status of one occurrence: the window status, refined by `active` for the occurrence being applied right now. */60export function occurrenceStatus(o: Occurrence): Status {61 if (o.active) return "in_progress";62 return o.window.status === "in_progress" ? "scheduled" : o.window.status;63}6465export function CategoryBadge({ category }: { category: Category }) {66 const { t } = useI18n();67 return (68 <span className={cx("mcc-badge", `tone-${CATEGORY_TONE[category]}`)} data-category={category}>69 <Icon name={CATEGORY_ICON[category]} size={13} />70 {t(`states.category.${category}`)}71 </span>72 );73}7475export function StatusDot({ status, subtle }: { status: Status; subtle?: boolean }) {76 const { t } = useI18n();77 return <StateDot tone={maintenanceTone(status)} icon={STATUS_ICON[status]} label={t(`states.maintenance.${status}`)} subtle={subtle ?? false} />;78}7980export function CategoryLegend() {81 const { t } = useI18n();82 const cats: Category[] = ["planned", "update", "hardware", "inspection", "emergency"];83 return (84 <ul className="mt-legend" aria-label={t("maintenance.calendar.legend")}>85 {cats.map((c) => <li key={c}><CategoryBadge category={c} /></li>)}86 <li className="mt-legend-item"><Icon name="restart" size={13} />{t("maintenance.calendar.recurring")}</li>87 <li className="mt-legend-item"><Icon name="note" size={13} />{t("maintenance.calendar.hasNotes")}</li>88 </ul>89 );90}9192/** Accessible sentence describing an occurrence (chips are compact: the full text lives in the label). */93export function useOccurrenceLabel(zone: string): (o: Occurrence) => string {94 const { t, tn, fmt } = useI18n();95 return (o) =>96 [97 o.window.title,98 t(`states.category.${o.window.category}`),99 t(`states.maintenance.${occurrenceStatus(o)}`),100 `${fmt.dateTime(o.occurrence_start, { timeZone: zone })} → ${fmt.dateTime(o.occurrence_end, { timeZone: zone })}`,101 tn("maintenance.calendar.nodeCount", o.window.scope.node_ids.length),102 o.window.recurrence !== "none" ? t(`states.recurrence.${o.window.recurrence}`) : "",103 o.note_count > 0 ? tn("maintenance.calendar.noteCount", o.note_count) : "",104 ].filter(Boolean).join(", ");105}106107/** Compact occurrence link used in the month grid and the week columns. */108export function OccurrenceChip({ seg, zone, label, tabbable, showTime = true }: { seg: DaySegment; zone: string; label: string; tabbable: boolean; showTime?: boolean }) {109 const { fmt } = useI18n();110 const o = seg.occ;111 const status = occurrenceStatus(o);112 return (113 <Link114 href={occurrenceHref(o)}115 className={cx("mt-chip", `tone-${CATEGORY_TONE[o.window.category]}`, status === "cancelled" && "cancelled", seg.continuesBefore && "cont-before", seg.continuesAfter && "cont-after")}116 title={label}117 tabIndex={tabbable ? 0 : -1}118 data-testid={`mt-occurrence-${o.window.id}`}119 data-status={status}120 data-category={o.window.category}121 >122 {/* the full description is the accessible name; the compact visible form is hidden from it so both never disagree */}123 <span className="sr-only">{label}</span>124 <Icon name={status === "scheduled" ? CATEGORY_ICON[o.window.category] : STATUS_ICON[status]} size={12} />125 {showTime && !seg.continuesBefore ? <span className="num mt-chip-time" aria-hidden="true">{fmt.time(seg.startMs, { seconds: false, timeZone: zone })}</span> : null}126 <span className="mt-chip-title" aria-hidden="true">{o.window.title}</span>127 {o.window.recurrence !== "none" ? <Icon name="restart" size={11} /> : null}128 {o.note_count > 0 ? <span className="mt-chip-notes num" aria-hidden="true"><Icon name="note" size={11} />{o.note_count}</span> : null}129 </Link>130 );131}132