Skip to content

Commit bd243b7

Browse files
committed
fix(wizard): enhance PDF upload error handling and localization
- Improved error handling in the PDF upload process to set a user-friendly error message when a PDF cannot be read due to corruption or password protection. - Added localization support for the new error message in English, Spanish, and Portuguese, ensuring consistent user feedback across languages. - Updated the PDF upload component to display the error message conditionally, enhancing the user experience during file uploads.
1 parent 08c886a commit bd243b7

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

apps/studio/src/components/wizard/bookCreationConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function buildConfigOverrides(values: WizardFormValues): Record<string, u
4343
if (validPageRange && parsedEndPage !== undefined) config.end_page = parsedEndPage
4444
if (values.imageSegmentation && values.segmentationMinSide.trim()) {
4545
const n = Number(values.segmentationMinSide.trim())
46-
if (!isNaN(n)) config.image_segmentation = { min_side: n }
46+
if (Number.isInteger(n) && n >= 0) config.image_segmentation = { min_side: n }
4747
}
4848

4949
return config

apps/studio/src/components/wizard/step1BasicInfo/PdfUpload.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useRef, useState } from "react"
22
import { Trans, useLingui } from "@lingui/react/macro"
3+
import { msg } from "@lingui/core/macro"
34
import { Upload, Trash2, XCircle } from "lucide-react"
45
import { useStore } from "@tanstack/react-form"
56
import { Button } from "@/components/ui/button"
@@ -56,9 +57,11 @@ const pdfCache = { file: null as File | null, totalPages: 0 }
5657

5758
export function usePdfUpload() {
5859
const form = useWizardForm()
60+
const { i18n } = useLingui()
5961
const { data: books } = useBooks()
6062
const file = useStore(form.store, (s) => s.values.file)
6163
const [totalPages, setTotalPages] = useState(pdfCache.file === file ? pdfCache.totalPages : 0)
64+
const [pdfError, setPdfError] = useState<string | null>(null)
6265

6366
useEffect(() => {
6467
if (!file) {
@@ -74,6 +77,7 @@ export function usePdfUpload() {
7477
return
7578
}
7679

80+
setPdfError(null)
7781
let cancelled = false
7882
;(async () => {
7983
await waitTwoAnimationFrames()
@@ -88,9 +92,11 @@ export function usePdfUpload() {
8892
form.setFieldValue("endPage", String(count))
8993
} catch {
9094
if (cancelled) return
91-
pdfCache.file = file
95+
pdfCache.file = null
9296
pdfCache.totalPages = 0
9397
setTotalPages(0)
98+
setPdfError(i18n._(msg`Could not read this PDF. The file may be corrupted or password-protected.`))
99+
form.setFieldValue("file", null)
94100
}
95101
})()
96102

@@ -112,12 +118,12 @@ export function usePdfUpload() {
112118
form.setFieldValue("file", null)
113119
}, [form])
114120

115-
return { file, totalPages, setFile, clearFile }
121+
return { file, totalPages, pdfError, setFile, clearFile }
116122
}
117123

118124
export function PdfUpload() {
119125
const { t } = useLingui()
120-
const { file, setFile, clearFile } = usePdfUpload()
126+
const { file, pdfError, setFile, clearFile } = usePdfUpload()
121127
const pdfRef = useRef<HTMLInputElement>(null)
122128
const [overlay, setOverlay] = useState<OverlayState>("idle")
123129
const overlayRef = useRef<OverlayState>("idle")
@@ -274,6 +280,9 @@ export function PdfUpload() {
274280
)}
275281
</div>
276282

283+
{pdfError && (
284+
<p className="text-xs text-[#ef4444] mt-1">{pdfError}</p>
285+
)}
277286
</div>
278287
</>
279288
)

apps/studio/src/locales/en.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ msgstr "Converted"
10301030
msgid "Coral reefs cover less than 1% of the ocean floor, yet they support roughly 25% of all marine species. Often called the 'rainforests of the sea,' these underwater ecosystems are built by tiny animals called coral polyps."
10311031
msgstr "Coral reefs cover less than 1% of the ocean floor, yet they support roughly 25% of all marine species. Often called the 'rainforests of the sea,' these underwater ecosystems are built by tiny animals called coral polyps."
10321032

1033+
msgid "Could not read this PDF. The file may be corrupted or password-protected."
1034+
msgstr "Could not read this PDF. The file may be corrupted or password-protected."
1035+
10331036
msgid "Cover"
10341037
msgstr "Cover"
10351038

apps/studio/src/locales/es.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ msgstr "Convertido"
10301030
msgid "Coral reefs cover less than 1% of the ocean floor, yet they support roughly 25% of all marine species. Often called the 'rainforests of the sea,' these underwater ecosystems are built by tiny animals called coral polyps."
10311031
msgstr "Los arrecifes de coral cubren menos del 1% del fondo oceánico, pero sostienen aproximadamente el 25% de todas las especies marinas. A menudo llamados 'los bosques lluviosos del mar', estos ecosistemas submarinos son construidos por pequeños animales llamados pólipos de coral."
10321032

1033+
msgid "Could not read this PDF. The file may be corrupted or password-protected."
1034+
msgstr "No se pudo leer este PDF. El archivo puede estar dañado o protegido con contraseña."
1035+
10331036
msgid "Cover"
10341037
msgstr "Portada"
10351038

apps/studio/src/locales/pt-BR.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ msgstr "Convertido"
10301030
msgid "Coral reefs cover less than 1% of the ocean floor, yet they support roughly 25% of all marine species. Often called the 'rainforests of the sea,' these underwater ecosystems are built by tiny animals called coral polyps."
10311031
msgstr "Os recifes de coral cobrem menos de 1% do fundo do oceano, mas sustentam cerca de 25% de todas as espécies marinhas. Frequentemente chamados de 'florestas tropicais do mar', esses ecossistemas subaquáticos são construídos por pequenos animais chamados pólipos de coral."
10321032

1033+
msgid "Could not read this PDF. The file may be corrupted or password-protected."
1034+
msgstr "Não foi possível ler este PDF. O arquivo pode estar corrompido ou protegido por senha."
1035+
10331036
msgid "Cover"
10341037
msgstr "Capa"
10351038

0 commit comments

Comments
 (0)