Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Feat(crm): Set Mime type to file upload

Anthony Rimet36bc99f19c0f
diff --git a/src/dataProvider.ts b/src/dataProvider.ts
index 9953d804..f78cf8e0 100644
--- a/src/dataProvider.ts
+++ b/src/dataProvider.ts
@@ -11,10 +11,10 @@ import {
import { getActivityLog } from './dataProvider/activity';
import { supabase } from './supabase';
import {
- AttachmentNote,
Contact,
ContactNote,
Deal,
+ DealNote,
RAFile,
Sale,
SalesFormData,
@@ -201,23 +201,6 @@ const dataProviderWithCustomMethods = {
return data?.at(0)?.is_initialized > 0;
},
- isImage(attachment: AttachmentNote): Promise<boolean> {
- return new Promise((resolve, reject) => {
- try {
- const pathFile = attachment.src;
- const extension = pathFile.split('.').pop();
- if (!extension) {
- resolve(false);
- } else {
- resolve(
- ['jpg', 'jpeg', 'png', 'gif', 'svg'].includes(extension)
- );
- }
- } catch (error) {
- reject(error);
- }
- });
- },
} satisfies DataProvider;
export type CustomDataProvider = typeof dataProviderWithCustomMethods;
@@ -236,6 +219,17 @@ export const dataProvider = withLifecycleCallbacks(
return data;
},
},
+ {
+ resource: 'dealNotes',
+ beforeSave: async (data: DealNote, _, __) => {
+ if (data.attachments) {
+ for (const fi of data.attachments) {
+ await uploadToBucket(fi);
+ }
+ }
+ return data;
+ },
+ },
{
resource: 'sales',
beforeSave: async (data: Sale, _, __) => {
@@ -361,5 +355,9 @@ const uploadToBucket = async (fi: RAFile) => {
fi.path = filePath;
fi.src = data.publicUrl;
+ // save MIME type
+ const mimeType = file.type;
+ fi.type = mimeType;
+
return fi;
};
diff --git a/src/notes/NoteAttachments.tsx b/src/notes/NoteAttachments.tsx
index f2f2b10b..bc838a06 100644
--- a/src/notes/NoteAttachments.tsx
+++ b/src/notes/NoteAttachments.tsx
@@ -1,47 +1,20 @@
import { ImageList, ImageListItem, Stack } from '@mui/material';
-import { AttachmentNote, ContactNote, DealNote, RAFile } from '../types';
-import { FileField, useDataProvider } from 'react-admin';
+import { AttachmentNote, ContactNote, DealNote } from '../types';
+import { FileField } from 'react-admin';
import AttachFileIcon from '@mui/icons-material/AttachFile';
-import { useEffect, useState } from 'react';
-export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {
- const dataProvider = useDataProvider();
-
- const [imageAttachments, setImageAttachments] = useState<RAFile[]>([]);
- const [otherAttachments, setOtherAttachments] = useState<RAFile[]>([]);
-
- useEffect(() => {
- const filterAttachments = async () => {
- if (!note.attachments) {
- return null;
- }
- const isImagePromises = note.attachments.map(
- async (attachment: AttachmentNote) => {
- const isImage = await dataProvider.isImage(attachment);
- return { attachment, isImage };
- }
- );
-
- const resolvedAttachments = await Promise.all(isImagePromises);
-
- const imageAttachments = resolvedAttachments
- .filter(({ isImage }) => isImage)
- .map(({ attachment }) => attachment);
-
- const otherAttachments = resolvedAttachments
- .filter(({ isImage }) => !isImage)
- .map(({ attachment }) => attachment);
-
- setImageAttachments(imageAttachments);
- setOtherAttachments(otherAttachments);
- };
-
- filterAttachments();
- }, [note.attachments, dataProvider]);
+export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {
if (!note.attachments || note.attachments.length === 0) {
return null;
}
+ const imageAttachments = note.attachments.filter(
+ (attachment: AttachmentNote) => isImageMimeType(attachment.type)
+ );
+ const otherAttachments = note.attachments.filter(
+ (attachment: AttachmentNote) => !isImageMimeType(attachment.type)
+ );
+
return (
<Stack direction="column">
{imageAttachments.length > 0 && (
@@ -86,3 +59,10 @@ export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {
</Stack>
);
};
+
+const isImageMimeType = (mimeType?: string): boolean => {
+ if (!mimeType) {
+ return false;
+ }
+ return mimeType.startsWith('image/');
+};
diff --git a/src/types.ts b/src/types.ts
index 6dd68a43..5acb9b1b 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -175,6 +175,7 @@ export interface RAFile {
title: string;
path?: string;
rawFile: File;
+ type?: string;
}
export type AttachmentNote = RAFile;