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'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 3 | import { MAX_QUICK_REACTIONS } from './types'; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 4 | import type { 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 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 17 | export const quickReactions = writable<QuickReaction[]>(DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 18 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 19 | function parse(raw: unknown): QuickReaction[] | null { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 20 | if (typeof raw !== 'object' || raw === null) return null; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 21 | const list = (raw as Record<string, unknown>)['reactions']; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 22 | if (!Array.isArray(list)) return null; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 23 | const seen = new Set<string>(); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 24 | const out: QuickReaction[] = []; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 25 | for (const item of list) { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 26 | if (typeof item !== 'object' || item === null) continue; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 27 | const { key, shortcode } = item as Record<string, unknown>; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 28 | if (typeof key !== 'string' || !key || seen.has(key)) continue; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 29 | seen.add(key); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 30 | out.push({ key, shortcode: typeof shortcode === 'string' ? shortcode : null }); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 31 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 32 | return out.slice(0, MAX_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 33 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 34 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 35 | export async function loadQuickReactions(): Promise<void> { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 36 | try { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 37 | quickReactions.set(parse(await getAccountData(EVENT_TYPE)) ?? DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 38 | } catch { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 39 | quickReactions.set(DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 40 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 41 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 42 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 43 | export async function saveQuickReactions(list: QuickReaction[]): Promise<void> { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 44 | const reactions = list.slice(0, MAX_QUICK_REACTIONS); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 45 | const previous = get(quickReactions); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 46 | quickReactions.set(reactions); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 47 | try { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 48 | await setAccountData(EVENT_TYPE, { reactions }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 49 | } catch (e) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 50 | if (get(quickReactions) === reactions) quickReactions.set(previous); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 51 | throw e; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 52 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 53 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 54 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 55 | export function resetQuickReactions(): void { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 56 | quickReactions.set(DEFAULT_QUICK_REACTIONS); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 57 | } |