5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 1 | import type { ImagePack, QuickEntry } from './types'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 2 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 3 | export type DisabledPackImages = Record<string, string[]>; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 4 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 5 | function isRecord(value: unknown): value is Record<string, unknown> { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 6 | return typeof value === 'object' && value !== null && !Array.isArray(value); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 7 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 8 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 9 | function setOwn<T>(record: Record<string, T>, key: string, value: T): void { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 10 | Object.defineProperty(record, key, { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 11 | configurable: true, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 12 | enumerable: true, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 13 | value, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 14 | writable: true |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 15 | }); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 16 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 17 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 18 | function hasOwn(record: object, key: string): boolean { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 19 | return Object.prototype.hasOwnProperty.call(record, key); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 20 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 21 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 22 | export function parseDisabledPackImages(raw: unknown): DisabledPackImages { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 23 | if (!isRecord(raw) || !isRecord(raw['disabled'])) return {}; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 24 | const disabled: DisabledPackImages = {}; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 25 | for (const [packId, rawShortcodes] of Object.entries(raw['disabled'])) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 26 | if (!Array.isArray(rawShortcodes)) continue; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 27 | const seen = new Set<string>(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 28 | const shortcodes = rawShortcodes.filter((value): value is string => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 29 | if (typeof value !== 'string' || value.length === 0 || seen.has(value)) return false; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 30 | seen.add(value); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 31 | return true; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 32 | }); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 33 | if (shortcodes.length > 0) setOwn(disabled, packId, shortcodes); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 34 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 35 | return disabled; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 36 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 37 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 38 | export function imageEnabled(disabled: DisabledPackImages, packId: string, shortcode: string): boolean { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 39 | return !hasOwn(disabled, packId) || !disabled[packId].includes(shortcode); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 40 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 41 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 42 | export function withImageEnabled( |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 43 | disabled: DisabledPackImages, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 44 | packId: string, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 45 | shortcode: string, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 46 | enabled: boolean |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 47 | ): DisabledPackImages { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 48 | const current = hasOwn(disabled, packId) ? disabled[packId] : []; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 49 | const nextShortcodes = enabled |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 50 | ? current.filter((item) => item !== shortcode) |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 51 | : current.includes(shortcode) ? [...current] : [...current, shortcode]; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 52 | if (nextShortcodes.length === current.length && nextShortcodes.every((item, index) => item === current[index])) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 53 | return disabled; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 54 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 55 | const next = { ...disabled }; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 56 | if (nextShortcodes.length === 0) delete next[packId]; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 57 | else setOwn(next, packId, nextShortcodes); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 58 | return next; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 59 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 60 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 61 | export function enabledPacks(packs: ImagePack[], disabled: DisabledPackImages): ImagePack[] { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 62 | return packs.map((pack) => ({ |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 63 | ...pack, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 64 | images: pack.images.filter((image) => imageEnabled(disabled, pack.id, image.shortcode)) |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 65 | })); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 66 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 67 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 68 | export function enabledQuickReactions( |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 69 | quick: QuickEntry[], |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 70 | packs: ImagePack[], |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 71 | disabled: DisabledPackImages |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 72 | ): QuickEntry[] { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 73 | const hidden = new Set<string>(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 74 | const enabled = new Set<string>(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 75 | for (const pack of packs) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 76 | for (const image of pack.images) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 77 | (imageEnabled(disabled, pack.id, image.shortcode) ? enabled : hidden).add(image.reaction_key); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 78 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 79 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 80 | return quick.filter((entry) => 'separator' in entry || !hidden.has(entry.key) || enabled.has(entry.key)); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 81 | } |