Skip to content

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

Feat(crm): Change logic for isImage

Anthony Rimet888b2b98c122
diff --git a/src/dataProvider.ts b/src/dataProvider.ts
index 9b78ea1c..9953d804 100644
--- a/src/dataProvider.ts
+++ b/src/dataProvider.ts
@@ -201,6 +201,23 @@ 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;
@@ -346,17 +363,3 @@ const uploadToBucket = async (fi: RAFile) => {
return fi;
};
-
-/**
- * Return if an attachment is an image.
- * @param attachment
- * @returns boolean
- */
-export const isImage = (attachment: AttachmentNote) => {
- const pathFile = attachment.src;
- const extension = pathFile.split('.').pop();
- if (!extension) {
- return false;
- }
- return ['jpg', 'jpeg', 'png', 'gif', 'svg'].includes(extension);
-};
diff --git a/src/notes/NoteAttachments.tsx b/src/notes/NoteAttachments.tsx
index 55c27ebc..f2f2b10b 100644
--- a/src/notes/NoteAttachments.tsx
+++ b/src/notes/NoteAttachments.tsx
@@ -1,21 +1,47 @@
import { ImageList, ImageListItem, Stack } from '@mui/material';
-import { AttachmentNote, ContactNote, DealNote } from '../types';
-import { FileField } from 'react-admin';
+import { AttachmentNote, ContactNote, DealNote, RAFile } from '../types';
+import { FileField, useDataProvider } from 'react-admin';
import AttachFileIcon from '@mui/icons-material/AttachFile';
-import { isImage } from '../dataProvider';
-
+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]);
+
if (!note.attachments || note.attachments.length === 0) {
return null;
}
- const imageAttachments = note.attachments.filter(
- (attachment: AttachmentNote) => isImage(attachment)
- );
- const otherAttachments = note.attachments.filter(
- (attachment: AttachmentNote) => !isImage(attachment)
- );
-
return (
<Stack direction="column">
{imageAttachments.length > 0 && (