The rain's column maths and fade colour move from MatrixRain into shell/rain.ts, and the request-once swarm name loader from useSwarmName into shell/swarm-name.ts. Both are plain functions with their input passed in, so tests cover them; the components keep the DOM and canvas work.
21 lines
716 B
TypeScript
21 lines
716 B
TypeScript
// useSwarmName — the swarm's configured display name, or `null` until it
|
|
// arrives, when none is configured, or when the request fails. One request per
|
|
// page load, shared by every caller; see `swarm-name.ts`.
|
|
import { useEffect, useState } from "preact/hooks";
|
|
import { createSwarmNameLoader } from "./swarm-name.js";
|
|
|
|
const loadSwarmName = createSwarmNameLoader((url) => fetch(url));
|
|
|
|
export function useSwarmName(): string | null {
|
|
const [name, setName] = useState<string | null>(null);
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
void loadSwarmName().then((n) => {
|
|
if (!cancelled && n !== null) setName(n);
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, []);
|
|
return name;
|
|
}
|