From 73d337bc2be1799fa0c11f12968e99757d0ce2d6 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Tue, 24 Mar 2026 21:35:07 +0100 Subject: [PATCH] chore: - full i8n support - loading state added - Array of sizes displayed - Accept list updated --- src/pages/tools/pdf/convert-to-pdf/index.tsx | 120 +++++++++++-------- 1 file changed, 69 insertions(+), 51 deletions(-) diff --git a/src/pages/tools/pdf/convert-to-pdf/index.tsx b/src/pages/tools/pdf/convert-to-pdf/index.tsx index c52c332d..59fe7857 100644 --- a/src/pages/tools/pdf/convert-to-pdf/index.tsx +++ b/src/pages/tools/pdf/convert-to-pdf/index.tsx @@ -14,67 +14,66 @@ import ToolMultipleImageInput, { } from '@components/input/ToolMultipleImageInput'; import ToolFileResult from 'components/result/ToolFileResult'; import { ToolComponentProps } from '@tools/defineTool'; -import { FormValues, Orientation, PageType, initialValues } from './types'; +import { Orientation, PageType, InitialValuesType, ImageSize } from './types'; import { buildPdf } from './service'; +import { useTranslation } from 'react-i18next'; -const initialFormValues: FormValues = initialValues; +const initialValues: InitialValuesType = { + pageType: 'full', + orientation: 'portrait', + scale: 100 +}; export default function ConvertToPdf({ title }: ToolComponentProps) { - const [input, setInput] = useState([]); - const [inputImages, setInputImages] = useState([]); + const { t } = useTranslation('pdf'); + const [input, setInput] = useState([]); const [result, setResult] = useState(null); - const [imageSize, setImageSize] = useState<{ - widthMm: number; - heightMm: number; - widthPx: number; - heightPx: number; - } | null>(null); + const [imageSizes, setImageSizes] = useState(null); + const [isProcessing, setIsProcessing] = useState(false); - const handleInputChange = (files: MultiImageInput[]) => { - setInputImages(files); - setInput(files.map(({ file }) => file)); - }; - - const compute = async (values: FormValues) => { + const compute = async ( + optionsValues: InitialValuesType, + input: MultiImageInput[] + ) => { if (!input.length) return; - const { pdfFile, imageSize } = await buildPdf({ - files: input, - pageType: values.pageType, - orientation: values.orientation, - scale: values.scale - }); - setResult(pdfFile); - setImageSize(imageSize); + + setIsProcessing(true); + + try { + const files = input.sort((a, b) => a.order - b.order).map((i) => i.file); + + const { pdfFile, imageSizes } = await buildPdf(files, optionsValues); + setResult(pdfFile); + setImageSizes(imageSizes); + } catch (error) { + throw new Error('Error converting image(s) to PDF:' + error); + } finally { + setIsProcessing(false); + } }; return ( - + } @@ -85,7 +84,9 @@ export default function ConvertToPdf({ title }: ToolComponentProps) { component: ( - PDF Type + + {t('convertToPdf.options.type')} + } - label="Full Size (Same as Image)" + label={t('convertToPdf.options.fullsize')} /> } - label="A4 Page" + label={t('convertToPdf.options.a4')} /> - {values.pageType === 'full' && imageSize && ( - - Image size: {imageSize.widthMm.toFixed(1)} ×{' '} - {imageSize.heightMm.toFixed(1)} mm ({imageSize.widthPx} ×{' '} - {imageSize.heightPx} px) - + {values.pageType === 'full' && imageSizes && ( + + {imageSizes.map((size, index) => ( + + {t('convertToPdf.options.image')} {index + 1}:{' '} + {size.widthMm.toFixed(1)} × {size.heightMm.toFixed(1)}{' '} + mm ({size.widthPx} × {size.heightPx} px) + + ))} + )} {values.pageType === 'a4' && ( - Orientation + + {t('convertToPdf.options.orientation')} + } - label="Portrait (Vertical)" + label={t('convertToPdf.options.portrait')} /> } - label="Landscape (Horizontal)" + label={t('convertToPdf.options.landscape')} /> @@ -143,8 +154,10 @@ export default function ConvertToPdf({ title }: ToolComponentProps) { {values.pageType === 'a4' && ( - Scale - Scale image: {values.scale}% + + {t('convertToPdf.options.scale')} + + {values.scale}% updateField('scale', v as number)} @@ -161,7 +174,12 @@ export default function ConvertToPdf({ title }: ToolComponentProps) { ] as const; }} resultComponent={ - + } /> );