feat(frontend): add admin card to generate thumbnails with polling
Fourth card on /admin/system mirrors the mass-import pattern: - POST /api/admin/generate-thumbnails to trigger - 2000 ms polling on /api/admin/thumbnail-status while RUNNING - processed / skipped / failed counters in the DONE message - standalone pollInterval so import and thumbnail polling don't interfere with each other Paraglide keys added in de/en/es, mirroring admin_system_import_*. Refs #307 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -14,8 +14,20 @@ type ImportStatus = {
|
||||
startedAt: string | null;
|
||||
};
|
||||
|
||||
type ThumbnailStatus = {
|
||||
state: 'IDLE' | 'RUNNING' | 'DONE' | 'FAILED';
|
||||
message: string;
|
||||
total: number;
|
||||
processed: number;
|
||||
skipped: number;
|
||||
failed: number;
|
||||
startedAt: string | null;
|
||||
};
|
||||
|
||||
let importStatus: ImportStatus | null = $state(null);
|
||||
let thumbnailStatus: ThumbnailStatus | null = $state(null);
|
||||
let pollInterval: ReturnType<typeof setInterval> | null = null;
|
||||
let thumbnailPollInterval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
function startPolling() {
|
||||
if (pollInterval) return;
|
||||
@@ -29,6 +41,18 @@ function stopPolling() {
|
||||
}
|
||||
}
|
||||
|
||||
function startThumbnailPolling() {
|
||||
if (thumbnailPollInterval) return;
|
||||
thumbnailPollInterval = setInterval(fetchThumbnailStatus, 2000);
|
||||
}
|
||||
|
||||
function stopThumbnailPolling() {
|
||||
if (thumbnailPollInterval) {
|
||||
clearInterval(thumbnailPollInterval);
|
||||
thumbnailPollInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchImportStatus() {
|
||||
const res = await fetch('/api/admin/import-status');
|
||||
if (res.ok) {
|
||||
@@ -51,11 +75,37 @@ async function triggerImport() {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchThumbnailStatus() {
|
||||
const res = await fetch('/api/admin/thumbnail-status');
|
||||
if (res.ok) {
|
||||
thumbnailStatus = await res.json();
|
||||
if (thumbnailStatus!.state === 'RUNNING') {
|
||||
startThumbnailPolling();
|
||||
} else {
|
||||
stopThumbnailPolling();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function triggerThumbnails() {
|
||||
const res = await fetch('/api/admin/generate-thumbnails', { method: 'POST' });
|
||||
if (res.ok) {
|
||||
thumbnailStatus = await res.json();
|
||||
if (thumbnailStatus!.state === 'RUNNING') {
|
||||
startThumbnailPolling();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
fetchImportStatus();
|
||||
fetchThumbnailStatus();
|
||||
});
|
||||
|
||||
onDestroy(() => stopPolling());
|
||||
onDestroy(() => {
|
||||
stopPolling();
|
||||
stopThumbnailPolling();
|
||||
});
|
||||
|
||||
async function backfillVersions() {
|
||||
backfillLoading = true;
|
||||
@@ -168,5 +218,62 @@ async function backfillFileHashes() {
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<!-- Thumbnail backfill -->
|
||||
<div class="rounded-sm border border-line bg-surface p-6 shadow-sm">
|
||||
<h2 class="mb-1 font-sans text-sm font-bold text-ink">
|
||||
{m.admin_system_thumbnails_heading()}
|
||||
</h2>
|
||||
<p class="mb-4 text-sm text-ink-2">{m.admin_system_thumbnails_description()}</p>
|
||||
|
||||
{#if thumbnailStatus?.state === 'RUNNING'}
|
||||
<p class="text-sm text-ink-2">
|
||||
{m.admin_system_thumbnails_status_running()}
|
||||
{#if thumbnailStatus.total > 0}
|
||||
<span class="text-ink-3">
|
||||
({thumbnailStatus.processed + thumbnailStatus.skipped + thumbnailStatus.failed} /
|
||||
{thumbnailStatus.total})
|
||||
</span>
|
||||
{/if}
|
||||
</p>
|
||||
{:else if thumbnailStatus?.state === 'DONE'}
|
||||
<p
|
||||
data-testid="thumbnails-status-done"
|
||||
class="mb-4 rounded-sm border border-green-200 bg-green-50 p-3 text-sm text-green-700"
|
||||
>
|
||||
{m.admin_system_thumbnails_status_done({
|
||||
processed: thumbnailStatus.processed,
|
||||
skipped: thumbnailStatus.skipped,
|
||||
failed: thumbnailStatus.failed
|
||||
})}
|
||||
</p>
|
||||
<button
|
||||
data-thumbnails-trigger
|
||||
onclick={triggerThumbnails}
|
||||
class="rounded-sm bg-primary px-5 py-2 font-sans text-xs font-bold tracking-widest text-primary-fg uppercase transition-opacity hover:opacity-80"
|
||||
>
|
||||
{m.admin_system_thumbnails_btn_retry()}
|
||||
</button>
|
||||
{:else if thumbnailStatus?.state === 'FAILED'}
|
||||
<p class="mb-4 rounded-sm border border-red-200 bg-red-50 p-3 text-sm text-red-700">
|
||||
{m.admin_system_thumbnails_status_failed({ message: thumbnailStatus.message })}
|
||||
</p>
|
||||
<button
|
||||
data-thumbnails-trigger
|
||||
onclick={triggerThumbnails}
|
||||
class="rounded-sm bg-primary px-5 py-2 font-sans text-xs font-bold tracking-widest text-primary-fg uppercase transition-opacity hover:opacity-80"
|
||||
>
|
||||
{m.admin_system_thumbnails_btn_retry()}
|
||||
</button>
|
||||
{:else}
|
||||
<button
|
||||
data-thumbnails-trigger
|
||||
onclick={triggerThumbnails}
|
||||
class="rounded-sm bg-primary px-5 py-2 font-sans text-xs font-bold tracking-widest text-primary-fg uppercase transition-opacity hover:opacity-80"
|
||||
>
|
||||
{m.admin_system_thumbnails_btn_start()}
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user