LazyBoy2/web/src/channels.ts

27 lines
811 B
TypeScript
Raw Normal View History

2026-09-15 09:07:16 +00:00
export type Channel = {
id: string;
name: string;
memberIds: string[];
};
const KEY = "lazyboy.channels";
export function readChannels(): Channel[] {
try {
const parsed = JSON.parse(localStorage.getItem(KEY) || "[]") as unknown;
if (!Array.isArray(parsed)) return [];
return parsed.flatMap((row) => {
if (!row || typeof row !== "object") return [];
const item = row as Partial<Channel>;
if (typeof item.id !== "string" || typeof item.name !== "string" || !Array.isArray(item.memberIds)) return [];
return [{ id: item.id, name: item.name, memberIds: item.memberIds.filter((id) => typeof id === "string") }];
});
} catch {
return [];
}
}
export function writeChannels(channels: Channel[]): void {
localStorage.setItem(KEY, JSON.stringify(channels));
}