diff --git a/apps/web/src/lib/components/products/ProductProcessingActions.svelte b/apps/web/src/lib/components/products/ProductProcessingActions.svelte index 6317f1c..6dc17e3 100644 --- a/apps/web/src/lib/components/products/ProductProcessingActions.svelte +++ b/apps/web/src/lib/components/products/ProductProcessingActions.svelte @@ -2,14 +2,14 @@ import { Check, CheckSquare, ChevronDown, FileUp, RotateCcw, X } from "@lucide/svelte"; import { Button } from "$lib/components/ui"; import { buttonClasses } from "$lib/components/ui/button-variants"; - import ConfirmationDialog from "$lib/components/categories/formula/ConfirmationDialog.svelte"; import { cn } from "$lib/utils"; import { i18n } from "$lib/i18n"; - import { PROCESSING_OPTIONS, type ProcessingType, type ProductRow } from "./types"; - import { CONTENT_LANGUAGES } from "$lib/content-languages"; + import { PROCESSING_OPTIONS, type ProcessingType, type ProductRow } from "./types"; + import { CONTENT_LANGUAGES } from "$lib/content-languages"; let { selectedCount, + initialOpen = false, selectedProducts = [], products = [], activeTab = "processed", @@ -17,8 +17,8 @@ inline = false, hideClear = false, primaryLanguage = "en", - contentLanguages = ["en"], - canUseAI = true, + contentLanguages = ["en"], + canUseAI = true, canProcessAITitles = canUseAI, canProcessAIDescriptions = canUseAI, onClear, @@ -28,6 +28,7 @@ onResetToUnprocessed }: { selectedCount: number; + initialOpen?: boolean; selectedProducts?: Array; products?: ProductRow[]; activeTab?: string; @@ -41,8 +42,8 @@ canProcessAIDescriptions?: boolean; onClear: () => void; primaryLanguage?: string; - contentLanguages?: string[]; - onProcess: (types: ProcessingType[], language: string) => void; + contentLanguages?: string[]; + onProcess: (types: ProcessingType[], language: string) => Promise | void; onExport: () => void; onMarkAsProcessed?: () => void; onResetToUnprocessed?: () => void; @@ -55,26 +56,21 @@ return !opt?.requiresAI || canUseAI; } - let open = $state(false); - let confirmProcessOpen = $state(false); - let pendingTypes = $state([]); - let pendingCredits = $state(null); - let pendingCount = $state(0); + let open = $state(initialOpen); + let submitting = $state(false); let selectedTypes = $state(["title", "description", "seo_meta"]); - let selectedLanguage = $state(""); - const languageOptions = $derived([primaryLanguage, ...contentLanguages].filter( - (lang, index, all) => all.indexOf(lang) === index - )); - const targetLanguage = $derived(languageOptions.includes(selectedLanguage) ? selectedLanguage : primaryLanguage); - let pendingLanguage = $state(""); + let selectedLanguage = $state(""); + const languageOptions = $derived([primaryLanguage, ...contentLanguages].filter( + (lang, index, all) => all.indexOf(lang) === index + )); + const targetLanguage = $derived(languageOptions.includes(selectedLanguage) ? selectedLanguage : primaryLanguage); - const selectedAILocked = $derived(selectedTypes.some( - (t) => PROCESSING_OPTIONS.find((o) => o.type === t)?.requiresAI && !aiAllowedForType(t) - )); + const selectedAILocked = $derived(selectedTypes.some( + (t) => PROCESSING_OPTIONS.find((o) => o.type === t)?.requiresAI && !aiAllowedForType(t) + )); - // A single product in a single language costs 1 credit, no matter how many - // of the three outputs are selected. Show only the total charge. - const totalCredits = $derived(selectedTypes.length === 0 ? 0 : selectedCount); + // A single product in one language costs 1 credit regardless of outputs. + const totalCredits = $derived(selectedTypes.length === 0 ? 0 : selectedCount); function toggleType(type: ProcessingType) { if (!aiAllowedForType(type) && PROCESSING_OPTIONS.find((o) => o.type === type)?.requiresAI) return; @@ -90,49 +86,21 @@ selectedTypes = all.every((t) => selectedTypes.includes(t)) ? [] : all; } - function processSelected() { - if (selectedTypes.length === 0) return; - if (selectedAILocked) return; - pendingTypes = [...selectedTypes]; - pendingLanguage = targetLanguage; - pendingCredits = totalCredits; - pendingCount = selectedCount; + // Start immediately. The parent owns the API request, success toast and + // selection reset. Never clear selection before a job was accepted. + async function processSelected() { + if (selectedTypes.length === 0 || selectedAILocked || isProcessing || submitting) return; + const outputs = [...selectedTypes]; + const language = targetLanguage; + submitting = true; open = false; - confirmProcessOpen = true; - } - - function cancelProcessConfirm() { - confirmProcessOpen = false; - pendingTypes = []; - pendingLanguage = ""; - pendingCredits = null; - pendingCount = 0; - } - - function confirmProcess() { - if (pendingTypes.length === 0) { - cancelProcessConfirm(); - return; + try { + await onProcess(outputs, language); + } finally { + submitting = false; } - const types = pendingTypes; - const language = pendingLanguage; - pendingTypes = []; - pendingLanguage = ""; - pendingCredits = null; - pendingCount = 0; - confirmProcessOpen = false; - selectedTypes = [...processProductTypes]; - onProcess(types, language); } - const processConfirmDescription = $derived( - pendingCredits === null - ? i18n.t("processing.actions.confirmWithoutEstimate", { count: pendingCount }) - : i18n.t("processing.actions.confirmBody", { - count: pendingCount, - credits: String(pendingCredits) - }) - );
@@ -160,15 +128,13 @@ {/if} - {#if activeTab !== "processed"} -
@@ -292,7 +258,6 @@ > {/if} - {/if} {#if activeTab === "needs_review" && onMarkAsProcessed} {/if} - - diff --git a/apps/web/src/lib/components/products/per-product-plan.test.ts b/apps/web/src/lib/components/products/per-product-plan.test.ts index 0eec3d4..f4c4842 100644 --- a/apps/web/src/lib/components/products/per-product-plan.test.ts +++ b/apps/web/src/lib/components/products/per-product-plan.test.ts @@ -6,14 +6,10 @@ import test from 'node:test'; const actions = readFileSync(fileURLToPath(new URL('./ProductProcessingActions.svelte', import.meta.url)), 'utf8'); const plan = readFileSync(fileURLToPath(new URL('../../../routes/admin/billing/+page.svelte', import.meta.url)), 'utf8'); -test('per-product plan hides individual output choices', () => { - assert.match(actions, /processingMode === "product"/); - assert.match(actions, /\{#if processingMode !== "outputs"\}[\s\S]*?\{:else\}[\s\S]*?\{#each PROCESSING_OPTIONS as option\}/); - assert.match(actions, /pendingTypes = processingMode !== "outputs" \? \[\.\.\.processProductTypes\]/); - assert.match(actions, /effectiveCosts\.product/); - assert.match(actions, /processingMode !== "outputs"/); - assert.match(actions, /pricingLoading/); - assert.match(actions, /processing.actions.confirmWithoutEstimate/); +test('catalog menu charges one credit per selected product without per-output prices', () => { + assert.match(actions, /selectedTypes\.length === 0 \? 0 : selectedCount/); + assert.match(actions, /"Total credits"/); + assert.doesNotMatch(actions, /effectiveCosts|pricingLoading|creditPriceMap/); }); test('plan popup opts in explicitly to the one-price-per-product mode', () => { diff --git a/apps/web/src/lib/components/products/processing-start.test.ts b/apps/web/src/lib/components/products/processing-start.test.ts new file mode 100644 index 0000000..fc314e6 --- /dev/null +++ b/apps/web/src/lib/components/products/processing-start.test.ts @@ -0,0 +1,40 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; +import test from 'node:test'; + +const actions = readFileSync(fileURLToPath(new URL('./ProductProcessingActions.svelte', import.meta.url)), 'utf8'); +const page = readFileSync(fileURLToPath(new URL('../../../routes/products/+page.svelte', import.meta.url)), 'utf8'); + +test('start button sends selected outputs and language without a second confirmation', () => { + assert.match(actions, /async function processSelected\(\)/); + assert.match(actions, /const outputs = \[\.\.\.selectedTypes\]/); + assert.match(actions, /const language = targetLanguage/); + assert.match(actions, /await onProcess\(outputs, language\)/); + assert.match(actions, /onclick=\{\(\) => void processSelected\(\)\}/); + assert.doesNotMatch(actions, /processProductTypes|confirmProcessOpen| { + assert.match(actions, /isProcessing \|\| submitting\) return/); + assert.match(actions, /submitting = true/); + assert.match(actions, /finally \{\s*submitting = false;/); + assert.match(page, /onProcess=\{\(types, language\) => startProcessing\(types, language\)\}/); +}); + +test('all shown products are selected, with the processing menu opened', () => { + assert.match(page, /function selectShownForProcessing\(\) \{[\s\S]*?selectableProductIds\(processableOnPage\)/); + assert.match(page, /openProcessingAfterSelect = true;\s*selectedIds = ids;/); + assert.match(page, /initialOpen=\{openProcessingAfterSelect\}/); + assert.match(page, /Process all shown \(\{processableOnPage\.length\}\)/); +}); + +test('a successful accepted processing job triggers notification and clears selection', () => { + const start = page.split('async function startProcessing(')[1]?.split('\n\tfunction requestMarkAsProcessed', 1)[0] ?? ''; + assert.match(start, /await api<[\s\S]*?\("\/api\/processing\/jobs",\s*\{/); + assert.match(start, /processing_types: types/); + assert.match(start, /notifySuccess\(i18n\.t\("toast\.products\.jobStarted"\)/); + assert.match(start, /notifySuccess[\s\S]*?selectedIds = \[\];/); + assert.match(start, /catch \(err\) \{[\s\S]*?notifyApiError/); + assert.doesNotMatch(start.split('catch (err)')[1] ?? '', /selectedIds = \[\]/); +}); diff --git a/apps/web/src/routes/products/+page.svelte b/apps/web/src/routes/products/+page.svelte index a97382e..1342bd0 100644 --- a/apps/web/src/routes/products/+page.svelte +++ b/apps/web/src/routes/products/+page.svelte @@ -152,6 +152,7 @@ let productsFetchGen = 0; let selectedIds = $state>([]); + let openProcessingAfterSelect = $state(false); let editOpen = $state(false); let showKeyboardTip = $state(false); let editing = $state(null); @@ -599,13 +600,24 @@ } function toggleSelect(id: string | number, selected: boolean) { + openProcessingAfterSelect = false; selectedIds = setProductSelected(selectedIds, id, selected); } function toggleSelectAll() { + openProcessingAfterSelect = false; selectedIds = toggleSelectAllIds(selectedIds, selectableProductIds(products)); } + // Process exactly the currently displayed, eligible products. This deliberately + // does not enqueue unseen pages or products already processing. + function selectShownForProcessing() { + const ids = selectableProductIds(processableOnPage); + if (ids.length === 0) return; + openProcessingAfterSelect = true; + selectedIds = ids; + } + function selectedRawIds(): string[] { return selectedRawProductIds(products, selectedIds, kind); } @@ -655,7 +667,7 @@ raw_product_ids: rawIds, processing_type: processingType, processing_types: types, - language + language } }); const queued = job.total_products_queued ?? job.total_products ?? rawIds.length; @@ -685,6 +697,7 @@ duration: 12000 }); selectedIds = []; + openProcessingAfterSelect = false; void loadProducts({ soft: true }); } catch (err) { error = notifyApiError(err, i18n.t("toast.products.startJobFailed"), { @@ -1149,6 +1162,9 @@ const selectableOnPage = $derived( products.filter((p) => p.processing_status !== "processing") ); + const processableOnPage = $derived( + selectableOnPage.filter((p) => kind === "raw" || Boolean(p.raw_product_id)) + ); const allPageSelected = $derived( selectableOnPage.length > 0 && selectableOnPage.every((p) => selectedIds.some((id) => String(id) === String(p.id))) @@ -1345,6 +1361,13 @@ /> + {#if selectedIds.length === 0 && processableOnPage.length > 0} +
+ +
+ {/if} {#if selectedIds.length > 0}
(selectedIds = [])} + onClear={() => { selectedIds = []; openProcessingAfterSelect = false; }} primaryLanguage={primaryProcessingLanguage} contentLanguages={enabledProcessingLanguages} - onProcess={(types, language) => void startProcessing(types, language)} + onProcess={(types, language) => startProcessing(types, language)} onExport={openExportDialog} onMarkAsProcessed={requestMarkAsProcessed} onResetToUnprocessed={canAdmin ? requestResetToUnprocessed : undefined}