atagen

malice

public
Star0
Watch0
9090eb8cdf5f6e853fd8a08b68b8d5fcc31b062a
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 type { QuickReaction } from './types';
544a170ead2catagen2026-10-20 11:47 UTC4
544a170ead2catagen2026-10-20 11:47 UTC5
const EVENT_TYPE = 'dev.malice.quick_reactions';
544a170ead2catagen2026-10-20 11:47 UTC6
544a170ead2catagen2026-10-20 11:47 UTC7
export const DEFAULT_QUICK_REACTIONS: QuickReaction[] = [
544a170ead2catagen2026-10-20 11:47 UTC8
  { key: '👍', shortcode: 'thumbsup' },
544a170ead2catagen2026-10-20 11:47 UTC9
  { key: '❤️', shortcode: 'heart' },
544a170ead2catagen2026-10-20 11:47 UTC10
  { key: '😂', shortcode: 'joy' },
544a170ead2catagen2026-10-20 11:47 UTC11
  { key: '🎉', shortcode: 'tada' },
544a170ead2catagen2026-10-20 11:47 UTC12
  { key: '😮', shortcode: 'open_mouth' },
544a170ead2catagen2026-10-20 11:47 UTC13
  { key: '😢', shortcode: 'cry' }
544a170ead2catagen2026-10-20 11:47 UTC14
];
544a170ead2catagen2026-10-20 11:47 UTC15
544a170ead2catagen2026-10-20 11:47 UTC16
export const quickReactions = writable<QuickReaction[]>(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC17
544a170ead2catagen2026-10-20 11:47 UTC18
function parse(raw: unknown): QuickReaction[] | null {
544a170ead2catagen2026-10-20 11:47 UTC19
  if (typeof raw !== 'object' || raw === null) return null;
544a170ead2catagen2026-10-20 11:47 UTC20
  const list = (raw as Record<string, unknown>)['reactions'];
544a170ead2catagen2026-10-20 11:47 UTC21
  if (!Array.isArray(list)) return null;
544a170ead2catagen2026-10-20 11:47 UTC22
  const seen = new Set<string>();
544a170ead2catagen2026-10-20 11:47 UTC23
  const out: QuickReaction[] = [];
544a170ead2catagen2026-10-20 11:47 UTC24
  for (const item of list) {
544a170ead2catagen2026-10-20 11:47 UTC25
    if (typeof item !== 'object' || item === null) continue;
544a170ead2catagen2026-10-20 11:47 UTC26
    const { key, shortcode } = item as Record<string, unknown>;
544a170ead2catagen2026-10-20 11:47 UTC27
    if (typeof key !== 'string' || !key || seen.has(key)) continue;
544a170ead2catagen2026-10-20 11:47 UTC28
    seen.add(key);
544a170ead2catagen2026-10-20 11:47 UTC29
    out.push({ key, shortcode: typeof shortcode === 'string' ? shortcode : null });
544a170ead2catagen2026-10-20 11:47 UTC30
  }
8c81366a752fatagen2026-10-26 03:56 UTC31
  return out;
544a170ead2catagen2026-10-20 11:47 UTC32
}
544a170ead2catagen2026-10-20 11:47 UTC33
544a170ead2catagen2026-10-20 11:47 UTC34
export async function loadQuickReactions(): Promise<void> {
544a170ead2catagen2026-10-20 11:47 UTC35
  try {
544a170ead2catagen2026-10-20 11:47 UTC36
    quickReactions.set(parse(await getAccountData(EVENT_TYPE)) ?? DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC37
  } catch {
544a170ead2catagen2026-10-20 11:47 UTC38
    quickReactions.set(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC39
  }
544a170ead2catagen2026-10-20 11:47 UTC40
}
544a170ead2catagen2026-10-20 11:47 UTC41
8c81366a752fatagen2026-10-26 03:56 UTC42
export async function saveQuickReactions(reactions: QuickReaction[]): Promise<void> {
df3c0f2b3da4atagen2026-10-22 13:07 UTC43
  const previous = get(quickReactions);
544a170ead2catagen2026-10-20 11:47 UTC44
  quickReactions.set(reactions);
df3c0f2b3da4atagen2026-10-22 13:07 UTC45
  try {
df3c0f2b3da4atagen2026-10-22 13:07 UTC46
    await setAccountData(EVENT_TYPE, { reactions });
df3c0f2b3da4atagen2026-10-22 13:07 UTC47
  } catch (e) {
df3c0f2b3da4atagen2026-10-22 13:07 UTC48
    if (get(quickReactions) === reactions) quickReactions.set(previous);
df3c0f2b3da4atagen2026-10-22 13:07 UTC49
    throw e;
df3c0f2b3da4atagen2026-10-22 13:07 UTC50
  }
544a170ead2catagen2026-10-20 11:47 UTC51
}
544a170ead2catagen2026-10-20 11:47 UTC52
544a170ead2catagen2026-10-20 11:47 UTC53
export function resetQuickReactions(): void {
544a170ead2catagen2026-10-20 11:47 UTC54
  quickReactions.set(DEFAULT_QUICK_REACTIONS);
544a170ead2catagen2026-10-20 11:47 UTC55
}