mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-20 15:36:18 +00:00
feat(mentions): client api types, store, snapshot + message hooks
This commit is contained in:
@@ -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(); };
|
||||
Reference in New Issue
Block a user