Add scan page
This commit is contained in:
1 parent
c0ca256282
commit
c0bb12cee2
1 file changed
+106
@@ -0,0 +1,106 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { api, type Scan } from '../lib/api';
|
||||||
|
import { app, go, toast } from '../lib/state.svelte';
|
||||||
|
import { monthLabel } from '../lib/dates';
|
||||||
|
import StatusBadge from './StatusBadge.svelte';
|
||||||
|
import Review from './Review.svelte';
|
||||||
|
|
||||||
|
let { id }: { id: number } = $props();
|
||||||
|
let scan = $state<Scan | null>(null);
|
||||||
|
let error = $state('');
|
||||||
|
let confirmDiscard = $state(false);
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
try {
|
||||||
|
scan = await api<Scan>(`/scans/${id}`);
|
||||||
|
} catch (e: any) {
|
||||||
|
error = e.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
load();
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (scan?.status !== 'queued') return;
|
||||||
|
const t = setInterval(load, 3000);
|
||||||
|
return () => clearInterval(t);
|
||||||
|
});
|
||||||
|
|
||||||
|
async function retry() {
|
||||||
|
scan = await api<Scan>(`/scans/${id}/retry`, { method: 'POST' });
|
||||||
|
}
|
||||||
|
async function discard() {
|
||||||
|
await api(`/scans/${id}`, { method: 'DELETE' });
|
||||||
|
toast('Scan discarded');
|
||||||
|
go('#/');
|
||||||
|
}
|
||||||
|
|
||||||
|
const calendars = $derived.by(() => {
|
||||||
|
const src = app.household!.sources.find((s) => s.id === scan?.source_id);
|
||||||
|
const cals = app.household!.calendars.filter(
|
||||||
|
(c) => c.child_ids.some((k) => src?.child_ids.includes(k)) && (!c.source_ids.length || c.source_ids.includes(src!.id)),
|
||||||
|
);
|
||||||
|
return cals.sort((a, b) => Number(b.id === src?.default_calendar_id) - Number(a.id === src?.default_calendar_id));
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if error}
|
||||||
|
<p class="error">{error}</p>
|
||||||
|
{:else if !scan}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else}
|
||||||
|
<div class="row spread">
|
||||||
|
<h1>{scan.source_name} <span class="muted">· {monthLabel(scan.hint_month)}</span></h1>
|
||||||
|
<StatusBadge status={scan.status} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{#if scan.status === 'queued'}
|
||||||
|
<div class="card stack" aria-live="polite">
|
||||||
|
<p><span class="spinner" aria-hidden="true"></span> Reading the page. This usually takes under a minute; you can leave this screen.</p>
|
||||||
|
</div>
|
||||||
|
{:else if scan.status === 'failed'}
|
||||||
|
<div class="card stack">
|
||||||
|
<p class="error">Reading failed: {scan.error}</p>
|
||||||
|
<div class="row">
|
||||||
|
<button class="primary" onclick={retry}>Try again</button>
|
||||||
|
<button onclick={discard}>Discard</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{:else if scan.status === 'abandoned'}
|
||||||
|
<p class="card">This scan was discarded.</p>
|
||||||
|
{:else if scan.status === 'published'}
|
||||||
|
<div class="card stack">
|
||||||
|
<p>Published {scan.events.length} events{scan.menu_items.length ? ` and ${scan.menu_items.length} menu days` : ''}.</p>
|
||||||
|
{#if scan.layout_type === 'menu'}
|
||||||
|
<a class="button primary" href="#/lunch">Pick lunch days</a>
|
||||||
|
{/if}
|
||||||
|
{#each calendars as c (c.id)}
|
||||||
|
<div class="row">
|
||||||
|
<a class="button" href={`/api/calendars/${c.id}/export.ics`} download>Download new events for {c.name} (.ics)</a>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
<a href="#/calendar">See the calendar</a>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<Review bind:scan />
|
||||||
|
<div class="discard">
|
||||||
|
{#if confirmDiscard}
|
||||||
|
<button class="danger" onclick={discard}>Discard this scan</button>
|
||||||
|
<button class="quiet" onclick={() => (confirmDiscard = false)}>Keep it</button>
|
||||||
|
{:else}
|
||||||
|
<button class="quiet" onclick={() => (confirmDiscard = true)}>Discard scan…</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
h1 { margin-bottom: 8px; }
|
||||||
|
.discard { margin-top: 32px; display: flex; gap: 8px; }
|
||||||
|
.spinner {
|
||||||
|
display: inline-block; width: 16px; height: 16px; margin-right: 6px; vertical-align: -2px;
|
||||||
|
border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
|
||||||
|
animation: spin 0.8s linear infinite;
|
||||||
|
}
|
||||||
|
@keyframes spin { to { transform: rotate(360deg); } }
|
||||||
|
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user