|
| 1 | +<script lang="ts"> |
| 2 | + import CopyButton from '#components/CopyButton.svelte'; |
| 3 | + import { fetchJobStatus, fetchShareLinks } from '#lib/api'; |
| 4 | + import { appDisplayName } from '#lib/appCatalog.svelte'; |
| 5 | + import Button from '#lib/components/ui/Button.svelte'; |
| 6 | + import Dialog from '#lib/components/ui/Dialog.svelte'; |
| 7 | + import { myDecryptsState, updateDecrypt, type TrackedDecrypt } from '#lib/decrypts.svelte'; |
| 8 | + import { fmtUntil } from '#lib/format'; |
| 9 | + import { notifyJobFinished } from '#lib/notifications'; |
| 10 | + import { playChime, vibrateCompletion } from '#lib/sound'; |
| 11 | + import { showToast, soundEnabledState } from '#lib/ui.svelte'; |
| 12 | +
|
| 13 | + interface CompletedDecrypt { |
| 14 | + label: string; |
| 15 | + url: string; |
| 16 | + expiresAt: number; |
| 17 | + } |
| 18 | +
|
| 19 | + let pollTimer: ReturnType<typeof setTimeout> | undefined; |
| 20 | + let completed = $state<CompletedDecrypt[]>([]); |
| 21 | + const current = $derived(completed[0]); |
| 22 | +
|
| 23 | + function decryptLabel(d: TrackedDecrypt): string { |
| 24 | + const name = appDisplayName(d.bundleId, d.trackName); |
| 25 | + return d.versionLabel ? `${name} (${d.versionLabel})` : name; |
| 26 | + } |
| 27 | +
|
| 28 | + async function presentCompletion(d: TrackedDecrypt): Promise<void> { |
| 29 | + const links = await fetchShareLinks(d.id); |
| 30 | + const link = links.links.find((entry) => entry.url); |
| 31 | + const label = decryptLabel(d); |
| 32 | +
|
| 33 | + if (!link?.url) { |
| 34 | + showToast(`${label} finished, but its share link is unavailable.`, 'error', { track: true }); |
| 35 | + notifyJobFinished('Decrypt finished', `${label} is ready to download.`); |
| 36 | + return; |
| 37 | + } |
| 38 | +
|
| 39 | + const url = link.url; |
| 40 | + completed = [...completed, { label, url, expiresAt: link.expiresAt }]; |
| 41 | + notifyJobFinished('Decrypt finished', `${label} is ready to download.`, url); |
| 42 | + showToast(`${label} is ready to download.`, 'success', { |
| 43 | + track: true, |
| 44 | + downloadUrl: url, |
| 45 | + action: { |
| 46 | + label: 'Download', |
| 47 | + onClick: () => window.location.assign(url), |
| 48 | + }, |
| 49 | + }); |
| 50 | + } |
| 51 | +
|
| 52 | + async function poll(): Promise<void> { |
| 53 | + clearTimeout(pollTimer); |
| 54 | + if (document.hidden) return; |
| 55 | + const pending = myDecryptsState.items.filter( |
| 56 | + (d) => d.status !== 'done' && d.status !== 'failed', |
| 57 | + ); |
| 58 | + if (pending.length === 0) return; |
| 59 | +
|
| 60 | + for (const d of pending) { |
| 61 | + try { |
| 62 | + const data = await fetchJobStatus(d.id); |
| 63 | + const finished = d.status !== data.status && (data.status === 'done' || data.status === 'failed'); |
| 64 | + updateDecrypt(d.id, { |
| 65 | + status: data.status, |
| 66 | + progress: data.progress, |
| 67 | + queue: data.queue, |
| 68 | + error: data.error, |
| 69 | + fileExpiresAt: data.fileExpiresAt, |
| 70 | + }); |
| 71 | + if (!finished) continue; |
| 72 | + if (soundEnabledState.value) { |
| 73 | + playChime(); |
| 74 | + vibrateCompletion(data.status === 'done'); |
| 75 | + } |
| 76 | + if (data.status === 'done') await presentCompletion(d); |
| 77 | + else { |
| 78 | + const label = decryptLabel(d); |
| 79 | + const message = `${label} failed: ${data.error ?? 'unknown error'}`; |
| 80 | + notifyJobFinished('Decrypt failed', message); |
| 81 | + showToast(message, 'error', { track: true }); |
| 82 | + } |
| 83 | + } catch {} |
| 84 | + } |
| 85 | +
|
| 86 | + pollTimer = setTimeout(poll, 2500); |
| 87 | + } |
| 88 | +
|
| 89 | + function onVisibilityChange(): void { |
| 90 | + if (!document.hidden) void poll(); |
| 91 | + } |
| 92 | +
|
| 93 | + $effect(() => { |
| 94 | + void poll(); |
| 95 | + document.addEventListener('visibilitychange', onVisibilityChange); |
| 96 | + return () => { |
| 97 | + clearTimeout(pollTimer); |
| 98 | + document.removeEventListener('visibilitychange', onVisibilityChange); |
| 99 | + }; |
| 100 | + }); |
| 101 | +
|
| 102 | + function onOpenChange(open: boolean): void { |
| 103 | + if (!open) completed = completed.slice(1); |
| 104 | + } |
| 105 | +</script> |
| 106 | + |
| 107 | +<Dialog open={Boolean(current)} {onOpenChange} class="max-w-md"> |
| 108 | + {#if current} |
| 109 | + <div class="mb-1 text-sm font-medium">Decrypt ready</div> |
| 110 | + <div class="mb-3 text-xs text-muted"> |
| 111 | + {current.label} has a registered share link with unlimited downloads. |
| 112 | + </div> |
| 113 | + <div class="bg-panel-muted mb-4 flex items-center gap-2 rounded-lg p-2"> |
| 114 | + <code class="min-w-0 flex-1 truncate" title={current.url}>{current.url}</code> |
| 115 | + <CopyButton text={current.url} label="Copy" /> |
| 116 | + </div> |
| 117 | + <div class="mb-4 text-xs text-muted">Expires {fmtUntil(current.expiresAt)}. It is available in Share links.</div> |
| 118 | + <div class="flex gap-2"> |
| 119 | + <a href={current.url} class="bg-accent text-accent-contrast hover:opacity-90 inline-flex h-8 flex-1 items-center justify-center rounded-md px-3 text-xs font-medium">Download</a> |
| 120 | + <Button variant="secondary" class="flex-1" onclick={() => onOpenChange(false)}>Close</Button> |
| 121 | + </div> |
| 122 | + {/if} |
| 123 | +</Dialog> |
0 commit comments