SkateSpots/src/lib/localMutes.ts

45 lines
1.2 KiB
TypeScript

/**
* Device-local optimistic mutes.
*
* Publishing a NIP-51 kind-10000 mute list is a signed action, and the relay
* may not hand it back immediately. Keeping the just-muted pubkeys here means
* the mute takes effect on this device at once and survives a reload until the
* published list is read back. The relay list remains the source of truth; this
* is only an overlay (mirrors `localModeration` / `localDeletions`).
*/
const STORAGE_KEY = 'skatespots:local-mutes-v1';
function read(): string[] {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return [];
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? (parsed as string[]) : [];
} catch {
return [];
}
}
function write(list: string[]): void {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify([...new Set(list)]));
} catch {
/* storage full / unavailable — best-effort */
}
}
export function getLocalMutes(): Set<string> {
return new Set(read());
}
export function addLocalMute(pubkey: string): void {
if (!pubkey) return;
const list = read();
list.push(pubkey);
write(list);
}
export function removeLocalMute(pubkey: string): void {
write(read().filter((pk) => pk !== pubkey));
}