Files
paseo/packages/app/src/utils/relative-time-ticker.ts
T
Mohamed Boudra ab8151d33f feat(app): sidebar display preferences, a shared menu engine, and a colour pass
Sidebar polish round.

Menus. `dropdown-menu` and `context-menu` were two copies of one engine that
differed only in how the trigger fires — identical from `computePosition` down —
and neither could nest. Both are now thin wrappers over
`components/ui/menu/`, which owns anchoring, a pure page-stack navigation model,
and one presentation fork: flyout on pointer, pushed pages in a sheet on compact.
All 31 call sites are unchanged. Items got a rounded inset fill, a visible
separator, and `selected` (a check) split from `active` (a fill).

Sidebar display preferences. The flat wall of four sections becomes one row per
decision with its value, and per-feature toggles over what a workspace row shows:
host, pull request, checks, scripts. Each is independent — a ticked toggle that
draws nothing is a lie. `display-preferences/` hides that grouping lives in
zustand while title source and row items are app settings.

Timestamps. "Last activity" now ticks itself. The label rounds to the minute and
never shows seconds; past a week it becomes a date. The timer lives in the leaf
`<Text>`, at most one per resolution tier shared by every row, and a tick only
re-renders when the string actually changed — so a memoized row that legitimately
never re-renders still stays current.

Colour. Full status tier regenerated at 90% of gamut max (was 85), same rule as
the muted tier, lightness and hue untouched. File icons are pulled toward neutral
in OKLab behind `getFileIconSvg` — material-icon-theme picks its palette for a
tree where the icon is the only colour on the row, and ours is not. New/deleted
files use the forge square-plus/square-minus glyphs at muted weight instead of
tinted pills. Project icons round a little more; uploaded icons never do.

The project status badge is a knockout again: it fills with the colour of the row
behind it, so the ring reads as a gap in the icon rather than a white halo.

File rows. The always-visible kebab is gone from the file explorer and diff tree
in favour of right-click, which also reclaims the reserved action slot.
2026-08-03 18:58:06 +02:00

97 lines
3.4 KiB
TypeScript

import type { RelativeTimeResolution } from "./time";
/**
* The shared clock behind relative timestamps.
*
* A sidebar row that nothing has happened to should never re-render — that is what the
* memoization is for — so a "2m" label sitting inside one cannot wait to be re-rendered by
* something else. It needs its own clock. The naive version of that is one timer per row, which
* turns a list of fifty workspaces into fifty timers firing at fifty different moments.
*
* Instead there is at most one timer per resolution, shared by every subscriber at that
* resolution, and a tier with no subscribers has no timer at all. A sidebar full of week-old
* workspaces runs no timers whatsoever, because those labels are `static` and never subscribe.
*/
export type TickResolution = Exclude<RelativeTimeResolution, "static">;
/**
* How often each tier wakes. The label only changes once per unit, so the interval sets how
* stale it can be, not how often it moves: an hour-resolution label is at most half an hour
* behind, which is invisible on a value that reads "3h" for sixty minutes either way.
*/
const INTERVAL_MS: Record<TickResolution, number> = {
minute: 60_000,
hour: 30 * 60_000,
day: 60 * 60_000,
};
interface Tier {
listeners: Set<() => void>;
handle: ReturnType<typeof setTimeout> | null;
interval: ReturnType<typeof setInterval> | null;
}
function createTier(): Tier {
return { listeners: new Set(), handle: null, interval: null };
}
const tiers: Record<TickResolution, Tier> = {
minute: createTier(),
hour: createTier(),
day: createTier(),
};
function notify(tier: Tier): void {
// Iterated live rather than copied: a listener that unsubscribes on its own tick — which is
// exactly what happens when a label ages into a slower tier — deletes from this set mid-loop,
// and Set iteration is defined under deletion. An entry removed before it is reached is simply
// not called, which is what unsubscribing asked for.
for (const listener of tier.listeners) {
listener();
}
}
function start(resolution: TickResolution): void {
const tier = tiers[resolution];
if (tier.handle !== null || tier.interval !== null) return;
// Aligned to the wall clock so every row flips together rather than each on the anniversary of
// whenever it happened to mount.
const period = INTERVAL_MS[resolution];
const delay = period - (Date.now() % period);
tier.handle = setTimeout(() => {
tier.handle = null;
notify(tier);
tier.interval = setInterval(() => notify(tier), period);
}, delay);
}
function stop(resolution: TickResolution): void {
const tier = tiers[resolution];
if (tier.handle !== null) clearTimeout(tier.handle);
if (tier.interval !== null) clearInterval(tier.interval);
tier.handle = null;
tier.interval = null;
}
/** Subscribes to a tier's tick. The tier's timer runs only while it has at least one listener. */
export function subscribeToRelativeTimeTick(
resolution: TickResolution,
listener: () => void,
): () => void {
const tier = tiers[resolution];
tier.listeners.add(listener);
if (tier.listeners.size === 1) start(resolution);
return () => {
tier.listeners.delete(listener);
if (tier.listeners.size === 0) stop(resolution);
};
}
/** Test seam: how many tiers currently hold a live timer. */
export function activeRelativeTimeTickerCount(): number {
return Object.values(tiers).filter((tier) => tier.handle !== null || tier.interval !== null)
.length;
}