mirror of
https://github.com/block/buzz.git
synced 2026-11-03 16:01:35 +00:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import * as React from "react";
|
|
|
|
/**
|
|
* User preference for showing a timestamp under each activity row — polished
|
|
* transcript rows and raw JSON-RPC cards alike. Persisted in localStorage and
|
|
* shared across every transcript surface, same as the animation preference.
|
|
* This is a device-level UI preference, not community-scoped data, so it is
|
|
* intentionally not reset on community switch. Defaults off to keep the feed
|
|
* compact.
|
|
*/
|
|
const STORAGE_KEY = "buzz:show-transcript-timestamps";
|
|
|
|
const listeners = new Set<() => void>();
|
|
|
|
let timestampsEnabled = readStoredPreference();
|
|
|
|
function readStoredPreference(): boolean {
|
|
if (typeof window === "undefined") {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
return window.localStorage.getItem(STORAGE_KEY) === "1";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function subscribe(listener: () => void): () => void {
|
|
listeners.add(listener);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
};
|
|
}
|
|
|
|
function getSnapshot(): boolean {
|
|
return timestampsEnabled;
|
|
}
|
|
|
|
function getServerSnapshot(): boolean {
|
|
return false;
|
|
}
|
|
|
|
/** Update the preference and notify all subscribed components. */
|
|
export function setTranscriptTimestampsEnabled(enabled: boolean): void {
|
|
timestampsEnabled = enabled;
|
|
|
|
try {
|
|
window.localStorage.setItem(STORAGE_KEY, enabled ? "1" : "0");
|
|
} catch {
|
|
// Persistence is best-effort; the in-memory value still applies.
|
|
}
|
|
|
|
for (const listener of listeners) {
|
|
listener();
|
|
}
|
|
}
|
|
|
|
/** Whether polished activity rows should show a timestamp footer. */
|
|
export function useTranscriptTimestampsEnabled(): boolean {
|
|
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
}
|