df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 1 | import { get, writable } from 'svelte/store'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 2 | import { getAccountData, setAccountData } from './api'; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 3 | import { normaliseQuick, parseQuickReactions } from './quick-reactions-model'; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 4 | import type { QuickEntry, QuickReaction } from './types'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 5 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 6 | const EVENT_TYPE = 'dev.malice.quick_reactions'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 7 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 8 | export const DEFAULT_QUICK_REACTIONS: QuickReaction[] = [ |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 9 | { key: '👍', shortcode: 'thumbsup' }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 10 | { key: '❤️', shortcode: 'heart' }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 11 | { key: '😂', shortcode: 'joy' }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 12 | { key: '🎉', shortcode: 'tada' }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 13 | { key: '😮', shortcode: 'open_mouth' }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 14 | { key: '😢', shortcode: 'cry' } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 15 | ]; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 16 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 17 | export const quickReactions = writable<QuickEntry[]>(DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 18 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 19 | export async function loadQuickReactions(): Promise<void> { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 20 | try { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 21 | quickReactions.set(parseQuickReactions(await getAccountData(EVENT_TYPE)) ?? DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 22 | } catch { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 23 | quickReactions.set(DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 24 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 25 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 26 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 27 | export async function saveQuickReactions(entries: QuickEntry[]): Promise<void> { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 28 | const reactions = normaliseQuick(entries); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 29 | const previous = get(quickReactions); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 30 | quickReactions.set(reactions); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 31 | try { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 32 | await setAccountData(EVENT_TYPE, { reactions }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 33 | } catch (e) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 34 | if (get(quickReactions) === reactions) quickReactions.set(previous); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 35 | throw e; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 36 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 37 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 38 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 39 | export function resetQuickReactions(): void { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 40 | quickReactions.set(DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 41 | } |