A full-featured CRM built with React, shadcn/ui, and Supabase.
WIP(upload): Send files to storage supabase
Anthony Rimet2a6a125e61eb
diff --git a/makefile b/makefileindex 6a621f96..bdd95189 100644--- a/makefile+++ b/makefile@@ -15,7 +15,7 @@ start-functions:start-app: ## start the app locallynpm run dev-start: start-supabase start-app ## start the stack locally+start: start-supabase start-app start-functions## start the stack locallystop-supabase: ## stop local supabasenpx supabase stopdiff --git a/src/dataProvider.ts b/src/dataProvider.tsindex 043a2bfd..5b168556 100644--- a/src/dataProvider.ts+++ b/src/dataProvider.ts@@ -163,6 +163,46 @@ export type CustomDataProvider = typeof dataProviderWithCustomMethods;export const dataProvider = withLifecycleCallbacks(dataProviderWithCustomMethods,[+ {+ resource: 'contactNotes',+ beforeSave: async (data, _, __) => {+ if (data.attachments) {+ for (const fi of data.attachments) {+ const response = await fetch(+ `${import.meta.env.VITE_SUPABASE_URL}/storage/v1/object/attachments/${fi.src}`,+ {+ method: 'HEAD',+ headers: {+ authorization: import.meta.env+ .VITE_SUPABASE_ANON_KEY,+ },+ }+ );++ if (response.status === 200) {+ continue;+ }++ const file = fi.rawFile;+ const fileExt = file.name.split('.').pop();+ const fileName = `${Math.random()}.${fileExt}`;+ const filePath = `${fileName}`;+ const { error: uploadError } = await supabase.storage+ .from('attachments')+ .upload(filePath, file);++ if (uploadError) {+ console.error('uploadError', uploadError);+ throw new Error('Failed to upload attachment');+ }++ fi.src = filePath;+ }+ }+ console.log('data', data);+ return data;+ },+ },{resource: 'contacts',beforeGetList: async params => {diff --git a/src/notes/NoteAttachments.tsx b/src/notes/NoteAttachments.tsxindex 88af1d06..aeff6422 100644--- a/src/notes/NoteAttachments.tsx+++ b/src/notes/NoteAttachments.tsx@@ -1,18 +1,22 @@import { ImageList, ImageListItem, Stack } from '@mui/material';import { AttachmentNote, ContactNote, DealNote } from '../types';-import { FileField } from 'react-admin';import AttachFileIcon from '@mui/icons-material/AttachFile';+import { supabase } from '../supabase';export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {if (!note.attachments || note.attachments.length === 0) {return null;}+ if (!note.attachments) {+ return null;+ }+const imageAttachments = note.attachments.filter(- (attachment: AttachmentNote) => isImage(attachment.rawFile)+ (attachment: AttachmentNote) => isImage(attachment.src));const otherAttachments = note.attachments.filter(- (attachment: AttachmentNote) => !isImage(attachment.rawFile)+ (attachment: AttachmentNote) => !isImage(attachment.src));return (@@ -23,7 +27,7 @@ export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {(attachment: AttachmentNote, index: number) => (<ImageListItem key={index}><img- src={attachment.src}+ src={getPublicUrl(attachment.src)}alt={attachment.title}style={{width: '200px',@@ -47,12 +51,15 @@ export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {(attachment: AttachmentNote, index: number) => (<Stack key={index} direction="row" alignItems="center"><AttachFileIcon fontSize="small" />- <FileField- record={{ attachment }}- source="attachment.src"- title="attachment.title"- target="_blank"- />++ <div>+ <a+ href={getPublicUrl(attachment.src)}+ title={attachment.title}+ >+ {attachment.title}+ </a>+ </div></Stack>))}@@ -60,6 +67,18 @@ export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {);};-const isImage = (file: File) => {- return file && file.type.startsWith('image/');+const isImage = (pathFile: string) => {+ const extension = pathFile.split('.').pop();+ if (!extension) {+ return false;+ }+ return ['jpg', 'jpeg', 'png', 'gif', 'svg'].includes(extension);+};++const getPublicUrl = (filePath: string) => {+ const { data } = supabase.storage+ .from('attachments')+ .getPublicUrl(filePath);++ return data.publicUrl;};