atagen

malice

public
Star0
Watch0
5e9271c9b8f197f3df3835ba5f01a4428aac18c8
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';
544a170ead2catagen2026-10-20 11:47 UTC3
import { MAX_QUICK_REACTIONS } from './types';
544a170ead2catagen2026-10-20 11:47 UTC4
import type { 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
544a170ead2catagen2026-10-20 11:47 UTC17
export const quickReactions = writable<QuickReaction[]>(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC18
544a170ead2catagen2026-10-20 11:47 UTC19
function parse(raw: unknown): QuickReaction[] | null {
544a170ead2catagen2026-10-20 11:47 UTC20
  if (typeof raw !== 'object' || raw === null) return null;
544a170ead2catagen2026-10-20 11:47 UTC21
  const list = (raw as Record<string, unknown>)['reactions'];
544a170ead2catagen2026-10-20 11:47 UTC22
  if (!Array.isArray(list)) return null;
544a170ead2catagen2026-10-20 11:47 UTC23
  const seen = new Set<string>();
544a170ead2catagen2026-10-20 11:47 UTC24
  const out: QuickReaction[] = [];
544a170ead2catagen2026-10-20 11:47 UTC25
  for (const item of list) {
544a170ead2catagen2026-10-20 11:47 UTC26
    if (typeof item !== 'object' || item === null) continue;
544a170ead2catagen2026-10-20 11:47 UTC27
    const { key, shortcode } = item as Record<string, unknown>;
544a170ead2catagen2026-10-20 11:47 UTC28
    if (typeof key !== 'string' || !key || seen.has(key)) continue;
544a170ead2catagen2026-10-20 11:47 UTC29
    seen.add(key);
544a170ead2catagen2026-10-20 11:47 UTC30
    out.push({ key, shortcode: typeof shortcode === 'string' ? shortcode : null });
544a170ead2catagen2026-10-20 11:47 UTC31
  }
544a170ead2catagen2026-10-20 11:47 UTC32
  return out.slice(0, MAX_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC33
}
544a170ead2catagen2026-10-20 11:47 UTC34
544a170ead2catagen2026-10-20 11:47 UTC35
export async function loadQuickReactions(): Promise<void> {
544a170ead2catagen2026-10-20 11:47 UTC36
  try {
544a170ead2catagen2026-10-20 11:47 UTC37
    quickReactions.set(parse(await getAccountData(EVENT_TYPE)) ?? DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC38
  } catch {
544a170ead2catagen2026-10-20 11:47 UTC39
    quickReactions.set(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC40
  }
544a170ead2catagen2026-10-20 11:47 UTC41
}
544a170ead2catagen2026-10-20 11:47 UTC42
544a170ead2catagen2026-10-20 11:47 UTC43
export async function saveQuickReactions(list: QuickReaction[]): Promise<void> {
544a170ead2catagen2026-10-20 11:47 UTC44
  const reactions = list.slice(0, MAX_QUICK_REACTIONS);
df3c0f2b3da4atagen2026-10-22 13:07 UTC45
  const previous = get(quickReactions);
544a170ead2catagen2026-10-20 11:47 UTC46
  quickReactions.set(reactions);
df3c0f2b3da4atagen2026-10-22 13:07 UTC47
  try {
df3c0f2b3da4atagen2026-10-22 13:07 UTC48
    await setAccountData(EVENT_TYPE, { reactions });
df3c0f2b3da4atagen2026-10-22 13:07 UTC49
  } catch (e) {
df3c0f2b3da4atagen2026-10-22 13:07 UTC50
    if (get(quickReactions) === reactions) quickReactions.set(previous);
df3c0f2b3da4atagen2026-10-22 13:07 UTC51
    throw e;
df3c0f2b3da4atagen2026-10-22 13:07 UTC52
  }
544a170ead2catagen2026-10-20 11:47 UTC53
}
544a170ead2catagen2026-10-20 11:47 UTC54
544a170ead2catagen2026-10-20 11:47 UTC55
export function resetQuickReactions(): void {
544a170ead2catagen2026-10-20 11:47 UTC56
  quickReactions.set(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC57
}