diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c0f7aa79..b7bfde72fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ and this project adheres to - ✨(backend) order pinned documents by last updated at #2028 - 🛂(frontend) fix cannot manage member on small screen #2226 +### Changed + +- ♻️(frontend) centralize allowed conversion formats in ContentTypes #2215 + ## [v4.8.6] - 2026-04-08 ### Added diff --git a/src/frontend/apps/impress/src/features/docs/docs-grid/api/useImportDoc.tsx b/src/frontend/apps/impress/src/features/docs/docs-grid/api/useImportDoc.tsx index d4b9d1e936..956e79c0ba 100644 --- a/src/frontend/apps/impress/src/features/docs/docs-grid/api/useImportDoc.tsx +++ b/src/frontend/apps/impress/src/features/docs/docs-grid/api/useImportDoc.tsx @@ -17,12 +17,30 @@ import { } from '@/api'; import { Doc, DocsResponse, KEY_LIST_DOC } from '@/docs/doc-management'; -export enum ContentTypes { - Docx = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - Markdown = 'text/markdown', - OctetStream = 'application/octet-stream', +interface ContentType { + mime: string; + extensions: string[]; } +export const ContentTypes: { + Docx: ContentType; + Markdown: ContentType; + OctetStream: ContentType; +} = { + Docx: { + mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + extensions: ['.docx'], + }, + Markdown: { + mime: 'text/markdown', + extensions: ['.md', '.markdown'], + }, + OctetStream: { + mime: 'application/octet-stream', + extensions: [], + }, +}; + export const importDoc = async ([file, mimeType]: [ File, string, diff --git a/src/frontend/apps/impress/src/features/docs/docs-grid/hooks/useImport.tsx b/src/frontend/apps/impress/src/features/docs/docs-grid/hooks/useImport.tsx index 6c1036f2d8..37cc1f8df2 100644 --- a/src/frontend/apps/impress/src/features/docs/docs-grid/hooks/useImport.tsx +++ b/src/frontend/apps/impress/src/features/docs/docs-grid/hooks/useImport.tsx @@ -14,6 +14,10 @@ interface UseImportProps { onDragOver: (isDragOver: boolean) => void; } +interface AcceptedMap { + [mime: string]: string[]; +} + export const useImport = ({ onDragOver }: UseImportProps) => { const { toast } = useToastProvider(); const { data: config } = useConfig(); @@ -36,31 +40,25 @@ export const useImport = ({ onDragOver }: UseImportProps) => { }; }, [config?.CONVERSION_FILE_MAX_SIZE]); - const ACCEPT = useMemo(() => { - const extensions = config?.CONVERSION_FILE_EXTENSIONS_ALLOWED; - const accept: { [key: string]: string[] } = {}; - - if (extensions && extensions.length > 0) { - extensions.forEach((ext) => { - switch (ext.toLowerCase()) { - case '.docx': - accept[ContentTypes.Docx] = ['.docx']; - break; - case '.md': - case '.markdown': - accept[ContentTypes.Markdown] = ['.md']; - break; - default: - break; + const ACCEPT = useMemo((): AcceptedMap => { + const allowedExtensions = config?.CONVERSION_FILE_EXTENSIONS_ALLOWED?.map( + (ext: string) => ext.toLowerCase(), + ) ?? ['.docx', '.md']; + + return Object.values(ContentTypes).reduce( + (acc: AcceptedMap, contentType) => { + const matchedExtensions = contentType.extensions.filter((ext: string) => + allowedExtensions.includes(ext), + ); + + if (matchedExtensions.length > 0) { + acc[contentType.mime] = matchedExtensions; } - }); - } else { - // Default to docx and md if no configuration is provided - accept[ContentTypes.Docx] = ['.docx']; - accept[ContentTypes.Markdown] = ['.md']; - } - return accept; + return acc; + }, + {}, + ); }, [config?.CONVERSION_FILE_EXTENSIONS_ALLOWED]); const { getRootProps, getInputProps, open } = useDropzone({ @@ -96,11 +94,16 @@ export const useImport = ({ onDragOver }: UseImportProps) => { VariantType.ERROR, ); } else { + const allowedExtensions = Object.values(ACCEPT).flat().join(', '); + toast( t( - `The document "{{documentName}}" import has failed (only .docx and .md files are allowed)`, + allowedExtensions + ? `The document "{{documentName}}" import has failed (only {{allowedExtensions}} files are allowed)` + : `The document "{{documentName}}" import has failed`, { documentName: rejection.file.name, + allowedExtensions, }, ), VariantType.ERROR,