132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 1 | <script lang="ts"> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 2 | import { runCommand } from './api'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 3 | import type { CloneSource } from './pack-clone'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 4 | import { cloneRequest, editableChoices, freePackStateKey, packLabel } from './packs'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 5 | import type { PackTarget } from './transport'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 6 | import type { ImagePacks, PackUsage, RoomSummary } from './types'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 7 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 8 | export let source: CloneSource; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 9 | export let packs: ImagePacks; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 10 | export let rooms: RoomSummary[]; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 11 | export let onClose: () => void; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 12 | export let onError: (message: string) => void; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 13 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 14 | const roomName = (id: string) => rooms.find((room) => room.room_id === id)?.name ?? id; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 15 | let destinationId = ''; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 16 | let roomId = ''; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 17 | let name = source.kind === 'sticker' ? 'Sticker pack' : packLabel(source.pack, roomName); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 18 | let asSticker = true; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 19 | let asEmoji = true; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 20 | let busy = false; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 21 | let error = ''; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 22 | let createdTarget: { roomId: string; name: string; target: PackTarget } | null = null; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 23 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 24 | $: choices = editableChoices(packs, roomName).filter((choice) => |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 25 | source.kind !== 'pack' || choice.pack?.id !== source.pack.id); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 26 | $: locations = rooms.filter((room) => room.can_edit_packs && room.membership === 'Joined') |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 27 | .sort((a, b) => Number(b.space) - Number(a.space) || a.name.localeCompare(b.name)); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 28 | $: if (!destinationId) destinationId = choices[0]?.pack?.id ?? (choices.length ? 'user' : 'new'); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 29 | $: if (!roomId && locations.length) roomId = locations[0].room_id; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 30 | $: creating = destinationId === 'new'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 31 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 32 | function key(event: KeyboardEvent): void { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 33 | if (event.key !== 'Escape') return; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 34 | event.stopImmediatePropagation(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 35 | if (!busy) onClose(); |
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 | async function copy(): Promise<void> { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 39 | if (busy) return; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 40 | const usage: PackUsage[] = [...(asSticker ? ['Sticker' as const] : []), ...(asEmoji ? ['Emoticon' as const] : [])]; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 41 | if (!usage.length) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 42 | error = 'Choose stickers, emoji, or both'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 43 | return; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 44 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 45 | busy = true; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 46 | error = ''; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 47 | try { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 48 | const choice = choices.find((item) => (item.pack?.id ?? 'user') === destinationId); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 49 | let target = choice?.target; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 50 | const displayName = name.trim(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 51 | if (creating) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 52 | if (!roomId || !displayName) throw new Error('Choose a space or room and give the pack a name'); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 53 | if (!createdTarget || createdTarget.roomId !== roomId || createdTarget.name !== displayName) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 54 | const stateKey = freePackStateKey(packs, roomId, displayName); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 55 | target = { kind: 'room', room_id: roomId, state_key: stateKey }; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 56 | await runCommand({ op: 'create_room_pack', room_id: roomId, state_key: stateKey, display_name: displayName }); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 57 | createdTarget = { roomId, name: displayName, target }; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 58 | } else target = createdTarget.target; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 59 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 60 | if (!target) throw new Error('Choose a destination pack'); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 61 | const result = await runCommand(cloneRequest(source, target, usage, creating ? displayName : null, choice?.pack ?? null)) as { skipped?: string[] }; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 62 | if (result?.skipped?.length) onError(`Could not copy ${result.skipped.join(', ')}`); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 63 | onClose(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 64 | } catch (e) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 65 | error = e instanceof Error ? e.message : 'Could not copy the images'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 66 | } finally { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 67 | busy = false; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 68 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 69 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 70 | </script> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 71 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 72 | <svelte:window on:keydown|capture={key} /> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 73 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 74 | <div class="modal clone" role="dialog" aria-modal="true" aria-label={source.kind === 'pack' ? 'Clone pack' : 'Clone image'}> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 75 | <h2>{source.kind === 'pack' ? 'Clone pack' : 'Clone image'}</h2> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 76 | <form on:submit|preventDefault={() => void copy()}> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 77 | <fieldset disabled={busy}> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 78 | <label class="field"> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 79 | <span>Destination pack</span> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 80 | <select bind:value={destinationId}> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 81 | {#each choices as choice} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 82 | <option value={choice.pack?.id ?? 'user'}>{choice.label}</option> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 83 | {/each} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 84 | {#if locations.length}<option value="new">New pack in a space or room</option>{/if} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 85 | </select> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 86 | </label> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 87 | {#if creating && locations.length} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 88 | <label class="field"> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 89 | <span>Space or room</span> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 90 | <select bind:value={roomId}> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 91 | {#each locations as room (room.room_id)} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 92 | <option value={room.room_id}>{room.space ? 'Space' : 'Room'} · {room.name}</option> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 93 | {/each} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 94 | </select> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 95 | </label> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 96 | <label class="field"><span>New pack name</span><input type="text" bind:value={name} required /></label> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 97 | {:else if choices.length === 0} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 98 | <p>No writable destination packs are available. Creating shared packs requires permission in a joined space or room.</p> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 99 | {:else} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 100 | <p>Copies are appended to the selected pack. Existing images stay in place and duplicate names get a numbered suffix.</p> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 101 | {/if} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 102 | <div class="uses" role="group" aria-label="Use copied images as"> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 103 | <label><input type="checkbox" bind:checked={asSticker} /> Stickers</label> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 104 | <label><input type="checkbox" bind:checked={asEmoji} /> Emoji and reactions</label> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 105 | </div> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 106 | {#if error}<p class="error" role="alert">{error}</p>{/if} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 107 | <div class="actions"> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 108 | <button type="button" class="ghost-btn" on:click={onClose}>Cancel</button> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 109 | <button type="submit" class="primary-btn" disabled={(!choices.length && !locations.length) || (!asSticker && !asEmoji)}>{busy ? 'Copying…' : 'Copy'}</button> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 110 | </div> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 111 | </fieldset> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 112 | </form> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 113 | </div> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 114 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 115 | <style> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 116 | .clone { width: min(460px, 100%); } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 117 | h2 { margin: 0 0 16px; font-size: 18px; } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 118 | fieldset { display: flex; flex-direction: column; gap: 14px; border: 0; padding: 0; margin: 0; min-width: 0; } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 119 | .field { display: flex; flex-direction: column; gap: 6px; } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 120 | input[type='text'], select { width: 100%; padding: 8px; background: var(--bg-soft); border: 1px solid var(--border); border-radius: var(--r-sm); color: var(--text); } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 121 | p { margin: 0; font-size: 13px; color: var(--muted); } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 122 | .uses, .actions { display: flex; flex-wrap: wrap; gap: 14px; } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 123 | .uses label { display: flex; align-items: center; gap: 6px; font-size: 13px; } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 124 | .actions { justify-content: flex-end; } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 125 | .error { color: var(--danger); } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 126 | </style> |