Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({
Expand Down Expand Up @@ -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,
Expand Down
Loading