5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 1 | import type { QuickEntry, QuickReaction, QuickSeparator } from './types'; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 2 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 3 | export function isSeparator(entry: QuickEntry): entry is QuickSeparator { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 4 | return 'separator' in entry; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 5 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 6 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 7 | export function normaliseQuick(entries: readonly QuickEntry[]): QuickEntry[] { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 8 | const seen = new Set<string>(); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 9 | const out: QuickEntry[] = []; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 10 | for (const entry of entries) { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 11 | if (isSeparator(entry)) { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 12 | const last = out[out.length - 1]; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 13 | if (last && !isSeparator(last)) out.push(entry); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 14 | continue; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 15 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 16 | if (seen.has(entry.key)) continue; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 17 | seen.add(entry.key); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 18 | out.push(entry); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 19 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 20 | const last = out[out.length - 1]; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 21 | if (last && isSeparator(last)) out.pop(); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 22 | return out; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 23 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 24 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 25 | export function parseQuickReactions(raw: unknown): QuickEntry[] | null { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 26 | if (typeof raw !== 'object' || raw === null) return null; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 27 | const list = (raw as Record<string, unknown>)['reactions']; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 28 | if (!Array.isArray(list)) return null; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 29 | const entries: QuickEntry[] = []; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 30 | for (const item of list) { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 31 | if (typeof item !== 'object' || item === null) continue; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 32 | const { key, shortcode, separator } = item as Record<string, unknown>; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 33 | if (separator === true) { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 34 | entries.push({ separator: true }); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 35 | continue; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 36 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 37 | if (typeof key !== 'string' || !key) continue; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 38 | entries.push({ key, shortcode: typeof shortcode === 'string' ? shortcode : null }); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 39 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 40 | return normaliseQuick(entries); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 41 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 42 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 43 | export function quickGroups(entries: readonly QuickEntry[]): QuickReaction[][] { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 44 | const groups: QuickReaction[][] = []; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 45 | let group: QuickReaction[] = []; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 46 | for (const entry of entries) { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 47 | if (!isSeparator(entry)) { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 48 | group.push(entry); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 49 | continue; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 50 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 51 | if (group.length > 0) groups.push(group); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 52 | group = []; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 53 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 54 | if (group.length > 0) groups.push(group); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 55 | return groups; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 56 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 57 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 58 | export function quickColumns(groups: readonly QuickReaction[][], perRow: number): number { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 59 | return groups.reduce((cols, group) => Math.max(cols, Math.min(group.length, perRow)), 0); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 60 | } |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 61 | |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 62 | export function sameQuick(a: readonly QuickEntry[], b: readonly QuickEntry[]): boolean { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 63 | return ( |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 64 | a.length === b.length && |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 65 | a.every((entry, i) => { |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 66 | const other = b[i]; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 67 | return isSeparator(entry) ? isSeparator(other) : !isSeparator(other) && entry.key === other.key; |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 68 | }) |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 69 | ); |
5059ed753f8b | atagen | 2026-10-27 07:20 UTC | 70 | } |