import { useEffect, useRef, useState } from "react"; import type { MouseEvent } from "react"; import { API_V1, CREDENTIALS } from "../apiContract"; import { DatabaseMark, SHARE_SHAPE } from "../customer-grid/iconShapes"; import { StarButton } from "../starred/StarredPage"; import { NO_SHARE_MARKS, parseShareMarks, shareMarkText } from "./homeModel"; import type { RecentTile, ShareMark, ShareMarks } from "./homeModel"; import TruncatedName from "./TruncatedName"; import IconTile from "../ui/IconTile"; import "../styles/sdlc79/shared-mark.css"; // S79-T61: the last answer seeds the next mount so a revisit paints its icons with the tiles. // It expires fast, and a sign-out clears it, so another account never inherits it. const SHARE_MARKS_SEED_MS = 15000; /** How long a module home holds its loading state for marks, once its own tiles are ready. */ export const SHARE_MARKS_WAIT_MS = 300; const SIGNED_OUT_EVENT = "aios:signed-out"; let shareMarksSeed: { marks: ShareMarks; at: number } | null = null; let shareMarksFlight: Promise | null = null; let shareMarksAccount = 0; function freshSeed(): ShareMarks | null { return shareMarksSeed && Date.now() - shareMarksSeed.at < SHARE_MARKS_SEED_MS ? shareMarksSeed.marks : null; } // Shell.tsx::signOut emits the event. A request still in flight answers no marks to anybody. function forgetShareMarks(): void { shareMarksAccount += 1; shareMarksSeed = null; shareMarksFlight = null; } if (typeof window !== "undefined") { window.addEventListener(SIGNED_OUT_EVENT, forgetShareMarks); } function loadShareMarks(): Promise { const account = shareMarksAccount; shareMarksFlight ??= fetch(`${API_V1}/share/marks`, { credentials: CREDENTIALS }) .then(async (response) => response.ok ? parseShareMarks(await response.json()) : NO_SHARE_MARKS) .catch(() => NO_SHARE_MARKS) .then((marks) => { if (account !== shareMarksAccount) return NO_SHARE_MARKS; // A failed read replaces the seed too: no icon, never a stale one. shareMarksSeed = { marks, at: Date.now() }; shareMarksFlight = null; return marks; }); return shareMarksFlight; } export interface ShareMarksState { marks: ShareMarks; /** True while the home should keep its loading state: tiles ready, marks not, bound not reached. */ hold: boolean; /** True when the marks landed after the tiles painted, so each icon fades in. */ fade: boolean; /** True until the marks answer (or fail). Tiles painted meanwhile keep an empty icon box. */ pending: boolean; } /** One request answers every tile's shared icon on a module home (R54). `ready`: its tiles are. */ export function useShareMarks(ready = true): ShareMarksState { const [state, setState] = useState(() => { const seed = freshSeed(); return { marks: seed ?? NO_SHARE_MARKS, settled: seed !== null, fade: false }; }); const [expired, setExpired] = useState(false); const painted = useRef(false); const hold = ready && !state.settled && !expired; useEffect(() => { if (ready && !hold) painted.current = true; }); useEffect(() => { let live = true; void loadShareMarks().then((marks) => { if (live) setState((now) => ({ marks, settled: true, fade: !now.settled && painted.current })); }); return () => { live = false; }; }, []); useEffect(() => { if (!ready || state.settled || expired) return; const timer = setTimeout(() => setExpired(true), SHARE_MARKS_WAIT_MS); return () => clearTimeout(timer); }, [ready, state.settled, expired]); return { marks: state.marks, hold, fade: state.fade, pending: !state.settled }; } // A tap on the icon shows its tooltip on a touch screen; it must not also open the tile. const holdTile = (event: MouseEvent) => { event.preventDefault(); event.stopPropagation(); }; /** * R54: one shared icon for both directions, at the end of the tile, left of its right-hand control. * While marks are `pending` every tile keeps an empty box, so a late icon fills reserved space. * Once they settle an unshared tile renders nothing: the box collapses and only widens the name. */ export function SharedMark({ mark, fade = false, pending = false }: { mark?: ShareMark | null; fade?: boolean; pending?: boolean; }) { if (!mark) return pending ?