Skip to content

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

Improve attachments previews

React-Admin CIf6fed7a597fc
diff --git a/src/components/atomic-crm/notes/AttachmentField.tsx b/src/components/atomic-crm/notes/AttachmentField.tsx
new file mode 100644
index 00000000..c1689d7f
--- /dev/null
+++ b/src/components/atomic-crm/notes/AttachmentField.tsx
@@ -0,0 +1,97 @@
+import { useFieldValue, useRecordContext, useTranslate } from "ra-core";
+import get from "lodash/get";
+import type { FileFieldProps } from "@/components/admin";
+import { cn } from "@/lib/utils";
+import { isImageMimeType } from "./isImageMimeType";
+
+export const AttachmentField = (props: FileFieldProps) => {
+ const {
+ className,
+ empty,
+ title,
+ src,
+ target,
+ download,
+ defaultValue,
+ source,
+ record: _recordProp,
+ ...rest
+ } = props;
+ const record = useRecordContext();
+ const sourceValue = useFieldValue({ defaultValue, source, record });
+ const titleValue =
+ useFieldValue({
+ ...props,
+ // @ts-expect-error We ignore here because title might be a custom label or undefined instead of a field name
+ source: title,
+ })?.toString() ?? title;
+ const translate = useTranslate();
+
+ if (
+ sourceValue == null ||
+ (Array.isArray(sourceValue) && sourceValue.length === 0)
+ ) {
+ if (!empty) {
+ return null;
+ }
+
+ return (
+ <div className={cn("inline-block", className)} {...rest}>
+ {typeof empty === "string" ? translate(empty, { _: empty }) : empty}
+ </div>
+ );
+ }
+
+ if (Array.isArray(sourceValue)) {
+ return (
+ <ul className={cn("inline-block", className)} {...rest}>
+ {sourceValue.map((file, index) => {
+ const fileTitleValue = title ? get(file, title, title) : title;
+ const srcValue = src ? get(file, src, title) : title;
+
+ return (
+ <li key={index}>
+ {isImageMimeType(file.type) ? (
+ <img
+ alt={fileTitleValue}
+ title={fileTitleValue}
+ src={srcValue}
+ />
+ ) : (
+ <a
+ href={srcValue}
+ title={fileTitleValue}
+ target={target}
+ download={download}
+ // useful to prevent click bubbling in a DataTable with rowClick
+ onClick={(e) => e.stopPropagation()}
+ >
+ {fileTitleValue}
+ </a>
+ )}
+ </li>
+ );
+ })}
+ </ul>
+ );
+ }
+
+ return (
+ <div className={cn("inline-block", className)} {...rest}>
+ {isImageMimeType(record?.rawFile.type) ? (
+ <img alt={titleValue} title={titleValue} src={sourceValue?.toString()} />
+ ) : (
+ <a
+ href={sourceValue?.toString()}
+ title={titleValue}
+ target={target}
+ download={download}
+ // useful to prevent click bubbling in a DataTable with rowClick
+ onClick={(e) => e.stopPropagation()}
+ >
+ {titleValue}
+ </a>
+ )}
+ </div>
+ );
+};
diff --git a/src/components/atomic-crm/notes/NoteAttachments.tsx b/src/components/atomic-crm/notes/NoteAttachments.tsx
index 914916ab..8746d0a5 100644
--- a/src/components/atomic-crm/notes/NoteAttachments.tsx
+++ b/src/components/atomic-crm/notes/NoteAttachments.tsx
@@ -1,6 +1,7 @@
import { Paperclip } from "lucide-react";
import type { AttachmentNote, ContactNote, DealNote } from "../types";
+import { isImageMimeType } from "./isImageMimeType";
export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {
if (!note.attachments || note.attachments.length === 0) {
@@ -47,10 +48,3 @@ export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {
</div>
);
};
-
-const isImageMimeType = (mimeType?: string): boolean => {
- if (!mimeType) {
- return false;
- }
- return mimeType.startsWith("image/");
-};
diff --git a/src/components/atomic-crm/notes/NoteInputs.tsx b/src/components/atomic-crm/notes/NoteInputs.tsx
index c97eaa7b..907789a5 100644
--- a/src/components/atomic-crm/notes/NoteInputs.tsx
+++ b/src/components/atomic-crm/notes/NoteInputs.tsx
@@ -2,7 +2,6 @@ import { useState } from "react";
import { useFormContext } from "react-hook-form";
import { TextInput } from "@/components/admin/text-input";
import { FileInput } from "@/components/admin/file-input";
-import { FileField } from "@/components/admin/file-field";
import { SelectInput } from "@/components/admin/select-input";
import { DateTimeInput } from "@/components/admin/date-time-input";
import { Button } from "@/components/ui/button";
@@ -11,6 +10,7 @@ import { cn } from "@/lib/utils";
import { Status } from "../misc/Status";
import { useConfigurationContext } from "../root/ConfigurationContext";
import { getCurrentDate } from "./utils";
+import { AttachmentField } from "./AttachmentField";
export const NoteInputs = ({ showStatus }: { showStatus?: boolean }) => {
const { noteStatuses } = useConfigurationContext();
@@ -76,7 +76,7 @@ export const NoteInputs = ({ showStatus }: { showStatus?: boolean }) => {
/>
</div>
<FileInput source="attachments" multiple>
- <FileField source="src" title="title" target="_blank" />
+ <AttachmentField source="src" title="title" target="_blank" />
</FileInput>
</div>
</div>
diff --git a/src/components/atomic-crm/notes/isImageMimeType.ts b/src/components/atomic-crm/notes/isImageMimeType.ts
new file mode 100644
index 00000000..ed03ffc7
--- /dev/null
+++ b/src/components/atomic-crm/notes/isImageMimeType.ts
@@ -0,0 +1,6 @@
+export const isImageMimeType = (mimeType?: string): boolean => {
+ if (!mimeType) {
+ return false;
+ }
+ return mimeType.startsWith("image/");
+};
diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.ts
index 6f5930ee..bdbcde28 100644
--- a/src/components/atomic-crm/providers/fakerest/dataProvider.ts
+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.ts
@@ -11,6 +11,7 @@ import fakeRestDataProvider from "ra-data-fakerest";
import type {
Company,
Contact,
+ ContactNote,
Deal,
Sale,
SalesFormData,
@@ -501,6 +502,21 @@ export const dataProvider = withLifecycleCallbacks(
return result;
},
} satisfies ResourceCallbacks<Deal>,
+ {
+ resource: "contact_notes",
+ beforeSave: async (params) => {
+ return {
+ ...params,
+ attachments: await Promise.all(
+ (params.attachments ?? []).map(async (attachment) => ({
+ ...attachment,
+ type: attachment.rawFile.type,
+ src: await convertFileToBase64(attachment),
+ })),
+ ),
+ };
+ },
+ } satisfies ResourceCallbacks<ContactNote>,
],
);
@@ -509,10 +525,11 @@ export const dataProvider = withLifecycleCallbacks(
* That's not the most optimized way to store images in production, but it's
* enough to illustrate the idea of dataprovider decoration.
*/
-const convertFileToBase64 = (file: { rawFile: Blob }) =>
+const convertFileToBase64 = (file: { rawFile: Blob }): Promise<string> =>
new Promise((resolve, reject) => {
const reader = new FileReader();
- reader.onload = () => resolve(reader.result);
+ // We know result is a string as we used readAsDataURL
+ reader.onload = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsDataURL(file.rawFile);
});