File size: 6,646 Bytes
d598032 4b54047 d598032 868b75a 7f72224 d598032 4b54047 1ad1eea 4b54047 d598032 4b54047 1ad1eea 4b54047 d598032 4b54047 86d34c4 4b54047 868b75a 1ad1eea 4b54047 d598032 4b54047 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 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<ShareMarks> | 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<ShareMarks> {
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<HTMLElement>) => {
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 ? <span className="home-shared-mark is-empty" aria-hidden="true" /> : null;
const text = shareMarkText(mark);
return (
<span className={"home-shared-mark" + (fade ? " is-arriving" : "")} role="img" aria-label={text} data-tip={text}
onClick={holdTile}>
<svg width={14} height={14} viewBox="0 0 16 16" aria-hidden="true" focusable="false">
{SHARE_SHAPE.map((s) => s.fill
? <path key={s.d} d={s.d} fill="currentColor" />
: <path key={s.d} d={s.d} fill="none" stroke="currentColor" strokeWidth={1.35}
strokeLinecap="round" strokeLinejoin="round" />)}
</svg>
</span>
);
}
/** Home and Data share this exact card in both grid and list layouts. */
export default function DatabaseTile({
tile,
folder = "",
known,
starred,
busy,
onToggle,
shared = null,
fade = false,
pending = false,
}: {
tile: RecentTile;
/** The folder path, shown only while searching. */
folder?: string;
known: boolean;
starred: boolean;
busy: boolean;
onToggle: () => void;
/** This account's share mark for the database, or none. */
shared?: ShareMark | null;
/** The mark landed after the tiles painted. */
fade?: boolean;
/** The marks have not answered yet. */
pending?: boolean;
}) {
return (
// Keep the wrapper while stars load so the grid cell never changes geometry.
<div className="st-tilewrap">
<a
className="home-tile"
href={tile.href}
{...(tile.external ? { target: "_blank", rel: "noreferrer" } : {})}
>
{/* `cg-db-mark` keeps the grid sheet's purple folder fill off this hollow mark, as in the rail. */}
<span className="cg-db-mark">
<IconTile icon={tile.icon} surface="home">
<DatabaseMark size={16} />
</IconTile>
</span>
<span className="home-tile-text">
<TruncatedName>{tile.label}</TruncatedName>
{/* No opened or edited line (owner item 23). Search alone adds the folder path. */}
{folder ? <span className="home-tile-ago">{folder}</span> : null}
</span>
<SharedMark mark={shared} fade={fade} pending={pending} />
</a>
{known ? (
<StarButton on={starred} label={tile.label} busy={busy} onToggle={onToggle} />
) : null}
</div>
);
}
|