feat(mentions): client api types, store, snapshot + message hooks

This commit is contained in:
simoleo89
2026-05-31 21:56:35 +02:00
committed by simoleo89
parent 76ec66932b
commit afb8100300
10 changed files with 192 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
import { IMentionEntry } from '../../api/mentions';
let mentions: IMentionEntry[] = [];
const listeners = new Set<() => void>();
const emit = () => { for(const l of listeners) l(); };
export const subscribeMentions = (onChange: () => void): (() => void) =>
{
listeners.add(onChange);
return () => { listeners.delete(onChange); };
};
export const getMentionsSnapshot = (): ReadonlyArray<IMentionEntry> => mentions;
export const getUnreadCount = (): number => mentions.reduce((n, m) => n + (m.read ? 0 : 1), 0);
export const setMentions = (list: IMentionEntry[]): void =>
{
mentions = [...list].sort((a, b) => b.mentionId - a.mentionId);
emit();
};
export const addMention = (entry: IMentionEntry): void =>
{
if(mentions.some(m => m.mentionId === entry.mentionId && entry.mentionId !== 0)) return;
mentions = [entry, ...mentions];
emit();
};
export const markRead = (mentionId: number): void =>
{
mentions = mentions.map(m => m.mentionId === mentionId ? { ...m, read: true } : m);
emit();
};
export const markAllRead = (): void =>
{
mentions = mentions.map(m => m.read ? m : { ...m, read: true });
emit();
};
export const resetMentions = (): void => { mentions = []; emit(); };