27 lines
811 B
TypeScript
27 lines
811 B
TypeScript
|
|
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));
|
||
|
|
}
|