Fix state persistence when popup is closed

- Add 'dismissed' status to prevent dismissed PDFs from reappearing
- Persist PDFs discovered from current tab and recent downloads via background
- Add dismissPDF function that marks PDFs as dismissed instead of removing
- Dismissed PDFs are kept for 7 days for duplicate detection, then cleaned up
- Completed items (sent/canceled) can still be fully removed
- Add addPDF message handler to service worker for popup-discovered PDFs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-16 10:20:28 +01:00
parent 3a48d4f497
commit 468473f03b
3 changed files with 89 additions and 27 deletions

View File

@@ -11,6 +11,7 @@ const STORAGE_KEY = 'pdfQueue';
const MAX_ENTRIES = 50;
const SENT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
const FAILED_MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
const DISMISSED_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
export type PDFStatus =
| 'pending' // Not yet uploaded
@@ -20,7 +21,8 @@ export type PDFStatus =
| 'ordering' // Order in progress
| 'in_production' // PRODUCTION_QUEUE or PRINTING
| 'sent' // SENT - terminal
| 'canceled'; // CANCELED - terminal
| 'canceled' // CANCELED - terminal
| 'dismissed'; // User dismissed, don't show again
export interface PDFQueueEntry extends DetectedPDF {
status: PDFStatus;
@@ -70,10 +72,10 @@ export async function addPDF(pdf: DetectedPDF): Promise<PDFQueueEntry | null> {
// Check for duplicate by URL
const existing = state.entries.find(e => e.url === pdf.url);
if (existing) {
// Skip if already uploaded (in basket, production, or completed)
const uploadedStatuses: PDFStatus[] = ['in_basket', 'ordering', 'in_production', 'sent', 'canceled'];
if (uploadedStatuses.includes(existing.status)) {
console.log('[PDF Queue] PDF already uploaded, skipping:', pdf.filename);
// Skip if already processed (uploaded, dismissed, etc.)
const processedStatuses: PDFStatus[] = ['in_basket', 'ordering', 'in_production', 'sent', 'canceled', 'dismissed'];
if (processedStatuses.includes(existing.status)) {
console.log('[PDF Queue] PDF already processed, skipping:', pdf.filename, existing.status);
return null;
}
console.log('[PDF Queue] PDF already in queue:', pdf.filename);
@@ -160,7 +162,24 @@ export async function updatePDFStatus(
}
/**
* Remove a PDF from the queue
* Dismiss a PDF (mark as dismissed so it won't reappear)
*/
export async function dismissPDF(id: string): Promise<void> {
const state = await loadQueue();
const entry = state.entries.find(e => e.id === id);
if (!entry) {
console.warn('[PDF Queue] PDF not found for dismissal:', id);
return;
}
entry.status = 'dismissed';
await saveQueue(state);
console.log('[PDF Queue] Dismissed PDF:', id);
}
/**
* Remove a PDF from the queue (complete removal, used for cleanup)
*/
export async function removePDF(id: string): Promise<void> {
const state = await loadQueue();
@@ -177,11 +196,12 @@ export async function removePDF(id: string): Promise<void> {
}
/**
* Get all PDFs for display in popup (all non-terminal statuses + recent terminal)
* Get all PDFs for display in popup (excludes dismissed)
*/
export async function getAllPDFs(): Promise<PDFQueueEntry[]> {
const state = await loadQueue();
return state.entries;
// Filter out dismissed entries - they're kept for duplicate detection but not displayed
return state.entries.filter(e => e.status !== 'dismissed');
}
/**
@@ -254,6 +274,14 @@ export async function cleanupOldEntries(): Promise<void> {
}
}
// Remove dismissed entries older than 7 days
if (entry.status === 'dismissed') {
const age = now - entry.timestamp;
if (age > DISMISSED_MAX_AGE_MS) {
return false;
}
}
return true;
});