Skip to content

Commit 12f7f6c

Browse files
committed
♻️(frontend) centralize allowed conversion formats in ContentTypes
Replace ContentTypes enum with a structured object mapping MIME types to their file extensions, removing the manual switch-case in useImport Signed-off-by: Stephan Meijer <me@stephanmeijer.com>
1 parent 599b909 commit 12f7f6c

3 files changed

Lines changed: 53 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project adheres to
1818
- ✨(backend) order pinned documents by last updated at #2028
1919
- 🛂(frontend) fix cannot manage member on small screen #2226
2020

21+
### Changed
22+
23+
- ♻️(frontend) centralize allowed conversion formats in ContentTypes #2215
24+
2125
## [v4.8.6] - 2026-04-08
2226

2327
### Added

src/frontend/apps/impress/src/features/docs/docs-grid/api/useImportDoc.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,30 @@ import {
1717
} from '@/api';
1818
import { Doc, DocsResponse, KEY_LIST_DOC } from '@/docs/doc-management';
1919

20-
export enum ContentTypes {
21-
Docx = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
22-
Markdown = 'text/markdown',
23-
OctetStream = 'application/octet-stream',
20+
interface ContentType {
21+
mime: string;
22+
extensions: string[];
2423
}
2524

25+
export const ContentTypes: {
26+
Docx: ContentType;
27+
Markdown: ContentType;
28+
OctetStream: ContentType;
29+
} = {
30+
Docx: {
31+
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
32+
extensions: ['.docx'],
33+
},
34+
Markdown: {
35+
mime: 'text/markdown',
36+
extensions: ['.md', '.markdown'],
37+
},
38+
OctetStream: {
39+
mime: 'application/octet-stream',
40+
extensions: [],
41+
},
42+
};
43+
2644
export const importDoc = async ([file, mimeType]: [
2745
File,
2846
string,

src/frontend/apps/impress/src/features/docs/docs-grid/hooks/useImport.tsx

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ interface UseImportProps {
1414
onDragOver: (isDragOver: boolean) => void;
1515
}
1616

17+
interface AcceptedMap {
18+
[mime: string]: string[];
19+
}
20+
1721
export const useImport = ({ onDragOver }: UseImportProps) => {
1822
const { toast } = useToastProvider();
1923
const { data: config } = useConfig();
@@ -36,31 +40,25 @@ export const useImport = ({ onDragOver }: UseImportProps) => {
3640
};
3741
}, [config?.CONVERSION_FILE_MAX_SIZE]);
3842

39-
const ACCEPT = useMemo(() => {
40-
const extensions = config?.CONVERSION_FILE_EXTENSIONS_ALLOWED;
41-
const accept: { [key: string]: string[] } = {};
42-
43-
if (extensions && extensions.length > 0) {
44-
extensions.forEach((ext) => {
45-
switch (ext.toLowerCase()) {
46-
case '.docx':
47-
accept[ContentTypes.Docx] = ['.docx'];
48-
break;
49-
case '.md':
50-
case '.markdown':
51-
accept[ContentTypes.Markdown] = ['.md'];
52-
break;
53-
default:
54-
break;
43+
const ACCEPT = useMemo((): AcceptedMap => {
44+
const allowedExtensions = config?.CONVERSION_FILE_EXTENSIONS_ALLOWED?.map(
45+
(ext: string) => ext.toLowerCase(),
46+
) ?? ['.docx', '.md'];
47+
48+
return Object.values(ContentTypes).reduce(
49+
(acc: AcceptedMap, contentType) => {
50+
const matchedExtensions = contentType.extensions.filter((ext: string) =>
51+
allowedExtensions.includes(ext),
52+
);
53+
54+
if (matchedExtensions.length > 0) {
55+
acc[contentType.mime] = matchedExtensions;
5556
}
56-
});
57-
} else {
58-
// Default to docx and md if no configuration is provided
59-
accept[ContentTypes.Docx] = ['.docx'];
60-
accept[ContentTypes.Markdown] = ['.md'];
61-
}
6257

63-
return accept;
58+
return acc;
59+
},
60+
{},
61+
);
6462
}, [config?.CONVERSION_FILE_EXTENSIONS_ALLOWED]);
6563

6664
const { getRootProps, getInputProps, open } = useDropzone({
@@ -96,11 +94,16 @@ export const useImport = ({ onDragOver }: UseImportProps) => {
9694
VariantType.ERROR,
9795
);
9896
} else {
97+
const allowedExtensions = Object.values(ACCEPT).flat().join(', ');
98+
9999
toast(
100100
t(
101-
`The document "{{documentName}}" import has failed (only .docx and .md files are allowed)`,
101+
allowedExtensions
102+
? `The document "{{documentName}}" import has failed (only {{allowedExtensions}} files are allowed)`
103+
: `The document "{{documentName}}" import has failed`,
102104
{
103105
documentName: rejection.file.name,
106+
allowedExtensions,
104107
},
105108
),
106109
VariantType.ERROR,

0 commit comments

Comments
 (0)