atagen

malice

public
Star0
Watch0
5059ed753f8be331e027bc2b84dfbf30b240252e
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/quick-reactions.ts

df3c0f2b3da4atagen2026-10-22 13:07 UTC1
import { get, writable } from 'svelte/store';
544a170ead2catagen2026-10-20 11:47 UTC2
import { getAccountData, setAccountData } from './api';
5059ed753f8batagen2026-10-27 07:20 UTC3
import { normaliseQuick, parseQuickReactions } from './quick-reactions-model';
5059ed753f8batagen2026-10-27 07:20 UTC4
import type { QuickEntry, QuickReaction } from './types';
544a170ead2catagen2026-10-20 11:47 UTC5
544a170ead2catagen2026-10-20 11:47 UTC6
const EVENT_TYPE = 'dev.malice.quick_reactions';
544a170ead2catagen2026-10-20 11:47 UTC7
544a170ead2catagen2026-10-20 11:47 UTC8
export const DEFAULT_QUICK_REACTIONS: QuickReaction[] = [
544a170ead2catagen2026-10-20 11:47 UTC9
  { key: '👍', shortcode: 'thumbsup' },
544a170ead2catagen2026-10-20 11:47 UTC10
  { key: '❤️', shortcode: 'heart' },
544a170ead2catagen2026-10-20 11:47 UTC11
  { key: '😂', shortcode: 'joy' },
544a170ead2catagen2026-10-20 11:47 UTC12
  { key: '🎉', shortcode: 'tada' },
544a170ead2catagen2026-10-20 11:47 UTC13
  { key: '😮', shortcode: 'open_mouth' },
544a170ead2catagen2026-10-20 11:47 UTC14
  { key: '😢', shortcode: 'cry' }
544a170ead2catagen2026-10-20 11:47 UTC15
];
544a170ead2catagen2026-10-20 11:47 UTC16
5059ed753f8batagen2026-10-27 07:20 UTC17
export const quickReactions = writable<QuickEntry[]>(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC18
544a170ead2catagen2026-10-20 11:47 UTC19
export async function loadQuickReactions(): Promise<void> {
544a170ead2catagen2026-10-20 11:47 UTC20
  try {
5059ed753f8batagen2026-10-27 07:20 UTC21
    quickReactions.set(parseQuickReactions(await getAccountData(EVENT_TYPE)) ?? DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC22
  } catch {
544a170ead2catagen2026-10-20 11:47 UTC23
    quickReactions.set(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC24
  }
544a170ead2catagen2026-10-20 11:47 UTC25
}
544a170ead2catagen2026-10-20 11:47 UTC26
5059ed753f8batagen2026-10-27 07:20 UTC27
export async function saveQuickReactions(entries: QuickEntry[]): Promise<void> {
5059ed753f8batagen2026-10-27 07:20 UTC28
  const reactions = normaliseQuick(entries);
df3c0f2b3da4atagen2026-10-22 13:07 UTC29
  const previous = get(quickReactions);
544a170ead2catagen2026-10-20 11:47 UTC30
  quickReactions.set(reactions);
df3c0f2b3da4atagen2026-10-22 13:07 UTC31
  try {
df3c0f2b3da4atagen2026-10-22 13:07 UTC32
    await setAccountData(EVENT_TYPE, { reactions });
df3c0f2b3da4atagen2026-10-22 13:07 UTC33
  } catch (e) {
df3c0f2b3da4atagen2026-10-22 13:07 UTC34
    if (get(quickReactions) === reactions) quickReactions.set(previous);
df3c0f2b3da4atagen2026-10-22 13:07 UTC35
    throw e;
df3c0f2b3da4atagen2026-10-22 13:07 UTC36
  }
544a170ead2catagen2026-10-20 11:47 UTC37
}
544a170ead2catagen2026-10-20 11:47 UTC38
544a170ead2catagen2026-10-20 11:47 UTC39
export function resetQuickReactions(): void {
544a170ead2catagen2026-10-20 11:47 UTC40
  quickReactions.set(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC41
}