Download web/src/home/DatabaseTile.tsx from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/home/DatabaseTile.tsx
- Command line
-
hf download hf://spaces/fsanyoto/loopable/web/src/home/DatabaseTile.tsx
-
curl -L -o DatabaseTile.tsx https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/home/DatabaseTile.tsx
6.65 kB
| 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> | |
| ); | |
| } | |