mirror of
https://github.com/block/buzz.git
synced 2026-11-29 04:32:08 +00:00
Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
/**
|
|
* Per-pubkey localStorage record store for channels manually marked unread via
|
|
* right-click → "mark unread". Keyed by channelId (not thread-root). Persisted
|
|
* so the sidebar badge survives reload and the rail observer can read it for
|
|
* inactive workspaces.
|
|
*
|
|
* Each entry stores the channel's own NIP-RS read marker (unix seconds) at the
|
|
* moment mark-unread was invoked, or null if no marker existed yet. The rail
|
|
* observer gates the forced-unread OR on this baseline: if the observed synced
|
|
* read marker has since advanced past markerAtWhenForced, the cross-device read
|
|
* wins and the dot is not lit.
|
|
*
|
|
* On identity change, the in-memory map is swapped to the current pubkey's
|
|
* persisted data. Old pubkey data is NOT wiped from localStorage.
|
|
*
|
|
* NOT synced to the relay — NIP-RS markers are monotonic and cannot represent
|
|
* a retrograde "unread" state. localStorage is best-effort (per-device).
|
|
*/
|
|
|
|
/** channelId → NIP-RS read marker (unix seconds) at force-time, or null */
|
|
export type ForcedUnreadMap = Record<string, number | null>;
|
|
|
|
const STORAGE_PREFIX = "buzz-forced-unread.v1";
|
|
const storageKey = (pubkey: string) => `${STORAGE_PREFIX}:${pubkey}`;
|
|
|
|
export const forcedUnreadStore = {
|
|
read(pubkey: string): ForcedUnreadMap {
|
|
try {
|
|
const raw = window.localStorage.getItem(storageKey(pubkey));
|
|
if (!raw) return {};
|
|
const parsed: unknown = JSON.parse(raw);
|
|
if (
|
|
typeof parsed !== "object" ||
|
|
parsed === null ||
|
|
Array.isArray(parsed)
|
|
)
|
|
return {};
|
|
const result: ForcedUnreadMap = {};
|
|
for (const [k, v] of Object.entries(parsed)) {
|
|
if (typeof k === "string" && (v === null || typeof v === "number")) {
|
|
result[k] = v as number | null;
|
|
}
|
|
}
|
|
return result;
|
|
} catch {
|
|
return {};
|
|
}
|
|
},
|
|
write(pubkey: string, map: ForcedUnreadMap): void {
|
|
try {
|
|
window.localStorage.setItem(storageKey(pubkey), JSON.stringify(map));
|
|
} catch {
|
|
// Ignore storage errors (private browsing, quota exceeded).
|
|
}
|
|
},
|
|
};
|