| 1 | import type { PackTarget } from './transport'; |
| 2 | import type { ImagePack, ImagePacks, PackImage, PackUsage } from './types'; |
| 3 | |
| 4 | export interface PackChoice { |
| 5 | label: string; |
| 6 | target: PackTarget; |
| 7 | pack: ImagePack | null; |
| 8 | } |
| 9 | |
| 10 | export function packTarget(pack: ImagePack): PackTarget { |
| 11 | if (pack.source === 'User') return { kind: 'user' }; |
| 12 | return { kind: 'room', room_id: pack.source.Room.room_id, state_key: pack.source.Room.state_key }; |
| 13 | } |
| 14 | |
| 15 | export function packLabel(pack: ImagePack, roomName: (roomId: string) => string): string { |
| 16 | if (pack.display_name) return pack.display_name; |
| 17 | if (pack.source === 'User') return 'Your pack'; |
| 18 | return roomName(pack.source.Room.room_id); |
| 19 | } |
| 20 | |
| 21 | export function allPacks(packs: ImagePacks): ImagePack[] { |
| 22 | const seen = new Set<string>(); |
| 23 | const out: ImagePack[] = []; |
| 24 | for (const pack of [...packs.global, ...Object.values(packs.rooms).flat()]) { |
| 25 | if (seen.has(pack.id)) continue; |
| 26 | seen.add(pack.id); |
| 27 | out.push(pack); |
| 28 | } |
| 29 | return out; |
| 30 | } |
| 31 | |
| 32 | /** Packs you can write to, with your own pack first even before it exists. */ |
| 33 | export function editableChoices(packs: ImagePacks, roomName: (roomId: string) => string): PackChoice[] { |
| 34 | const all = allPacks(packs); |
| 35 | const user = all.find((p) => p.source === 'User') ?? null; |
| 36 | const choices: PackChoice[] = [{ label: user?.display_name ?? 'Your pack', target: { kind: 'user' }, pack: user }]; |
| 37 | for (const pack of all) { |
| 38 | if (pack.source === 'User' || !pack.editable) continue; |
| 39 | choices.push({ label: packLabel(pack, roomName), target: packTarget(pack), pack }); |
| 40 | } |
| 41 | return choices; |
| 42 | } |
| 43 | |
| 44 | export function hasUsage(image: PackImage, usage: PackUsage): boolean { |
| 45 | return image.usage.length === 0 || image.usage.includes(usage); |
| 46 | } |
| 47 | |
| 48 | export function stickersOf(pack: ImagePack): PackImage[] { |
| 49 | return pack.images.filter((image) => hasUsage(image, 'Sticker')); |
| 50 | } |
| 51 | |
| 52 | /** A shortcode from a file name, unique within `taken`. */ |
| 53 | export function shortcodeFor(filename: string, taken: Set<string>): string { |
| 54 | const stem = filename.replace(/\.[^.]+$/, ''); |
| 55 | const base = |
| 56 | stem |
| 57 | .toLowerCase() |
| 58 | .replace(/[^a-z0-9_-]+/g, '_') |
| 59 | .replace(/^_+|_+$/g, '') |
| 60 | .slice(0, 64) || 'image'; |
| 61 | let candidate = base; |
| 62 | for (let n = 2; taken.has(candidate); n += 1) candidate = `${base}_${n}`; |
| 63 | taken.add(candidate); |
| 64 | return candidate; |
| 65 | } |
| 66 | |
| 67 | export function validShortcode(code: string): boolean { |
| 68 | return /^[^\s:]{1,100}$/.test(code); |
| 69 | } |
| 70 | |
| 71 | export function packSlug(name: string): string { |
| 72 | return ( |
| 73 | name |
| 74 | .toLowerCase() |
| 75 | .replace(/[^a-z0-9]+/g, '-') |
| 76 | .replace(/^-+|-+$/g, '') |
| 77 | .slice(0, 48) || 'pack' |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | export interface CloneDestination { |
| 82 | label: string; |
| 83 | target: PackTarget; |
| 84 | display_name: string | null; |
| 85 | } |
| 86 | |
| 87 | /** Where a whole pack can be cloned to: merged into your pack, or as a new pack in a room you can edit. */ |
| 88 | export function clonePackDestinations( |
| 89 | pack: ImagePack, |
| 90 | rooms: { room_id: string; name: string }[], |
| 91 | roomName: (roomId: string) => string |
| 92 | ): CloneDestination[] { |
| 93 | const name = packLabel(pack, roomName); |
| 94 | return [ |
| 95 | { label: 'Merge into your pack', target: { kind: 'user' }, display_name: null }, |
| 96 | ...rooms.map((room) => ({ |
| 97 | label: `New pack in ${room.name}`, |
| 98 | target: { kind: 'room', room_id: room.room_id, state_key: packSlug(name) } as PackTarget, |
| 99 | display_name: name |
| 100 | })) |
| 101 | ]; |
| 102 | } |