Skip to content

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

fix redirections

Jean-Baptiste Kaiser6c46ba3951dd
diff --git a/registry.json b/registry.json
index 5fdf63ec..37e14d7b 100644
--- a/registry.json
+++ b/registry.json
@@ -377,7 +377,7 @@
"type": "registry:component"
},
{
- "path": "src/components/atomic-crm/misc/ListButton.tsx",
+ "path": "src/components/atomic-crm/misc/MobileBackButton.tsx",
"type": "registry:component"
},
{
diff --git a/src/components/atomic-crm/contacts/ContactCreate.tsx b/src/components/atomic-crm/contacts/ContactCreate.tsx
index 96af9343..26455613 100644
--- a/src/components/atomic-crm/contacts/ContactCreate.tsx
+++ b/src/components/atomic-crm/contacts/ContactCreate.tsx
@@ -5,7 +5,7 @@ import { SaveButton } from "@/components/admin";
import { useIsMobile } from "@/hooks/use-mobile";
import { MobileContent } from "../layout/MobileContent";
import MobileHeader from "../layout/MobileHeader";
-import { ListButton } from "../misc/ListButton";
+import { MobileBackButton } from "../misc/MobileBackButton";
import type { Contact } from "../types";
import { ContactInputs } from "./ContactInputs";
import { FormToolbar } from "../layout/FormToolbar";
@@ -50,7 +50,7 @@ const ContactCreateContentMobile = () => {
return (
<div>
<MobileHeader>
- <ListButton />
+ <MobileBackButton />
<div className="flex flex-1 text-xl font-semibold">Create Contact</div>
</MobileHeader>
<MobileContent>
diff --git a/src/components/atomic-crm/contacts/ContactEdit.tsx b/src/components/atomic-crm/contacts/ContactEdit.tsx
index df897052..7036447f 100644
--- a/src/components/atomic-crm/contacts/ContactEdit.tsx
+++ b/src/components/atomic-crm/contacts/ContactEdit.tsx
@@ -1,11 +1,17 @@
import { Card, CardContent } from "@/components/ui/card";
-import { EditBase, Form, RecordRepresentation, useEditContext } from "ra-core";
+import {
+ EditBase,
+ Form,
+ RecordRepresentation,
+ useCreatePath,
+ useEditContext,
+} from "ra-core";
import { useIsMobile } from "@/hooks/use-mobile";
import { FormToolbar } from "../layout/FormToolbar";
import { MobileContent } from "../layout/MobileContent";
import MobileHeader from "../layout/MobileHeader";
-import { ListButton } from "../misc/ListButton";
+import { MobileBackButton } from "../misc/MobileBackButton";
import type { Contact } from "../types";
import { ContactAside } from "./ContactAside";
import { ContactInputs } from "./ContactInputs";
@@ -41,11 +47,17 @@ const ContactEditContent = () => {
const ContactEditContentMobile = () => {
const { isPending, record } = useEditContext<Contact>();
+ const createPath = useCreatePath();
+ const to = createPath({
+ resource: "contacts",
+ type: "show",
+ id: record?.id,
+ });
if (isPending || !record) return null;
return (
<div>
<MobileHeader>
- <ListButton />
+ <MobileBackButton to={to} />
<div className="flex flex-1 text-xl font-semibold text-ellipsis overflow-hidden whitespace-nowrap">
Edit <RecordRepresentation />
</div>
diff --git a/src/components/atomic-crm/contacts/ContactShow.tsx b/src/components/atomic-crm/contacts/ContactShow.tsx
index 49267e4e..3d7a0428 100644
--- a/src/components/atomic-crm/contacts/ContactShow.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.tsx
@@ -18,7 +18,7 @@ import { ContactTasksList } from "./ContactTasksList";
import type { Contact } from "../types";
import { Avatar } from "./Avatar";
import { ContactAside } from "./ContactAside";
-import { ListButton } from "../misc/ListButton";
+import { MobileBackButton } from "../misc/MobileBackButton";
import { EditButton } from "../misc/EditButton";
import { CreateButton } from "@/components/admin";
@@ -39,7 +39,7 @@ const ContactShowContentMobile = () => {
return (
<>
<MobileHeader>
- <ListButton />
+ <MobileBackButton />
<div className="flex flex-1">
<h1 className="text-xl font-semibold">Contacts</h1>
</div>
diff --git a/src/components/atomic-crm/misc/ListButton.tsx b/src/components/atomic-crm/misc/ListButton.tsx
deleted file mode 100644
index fc6ff01c..00000000
--- a/src/components/atomic-crm/misc/ListButton.tsx
+++ /dev/null
@@ -1,27 +0,0 @@
-import { useResourceContext, useCreatePath } from "ra-core";
-import { useNavigate } from "react-router";
-import { ChevronLeft } from "lucide-react";
-import { Button } from "@/components/ui/button";
-
-export const ListButton = (props: { resource?: string }) => {
- const resource = useResourceContext(props);
- const navigate = useNavigate();
- const createPath = useCreatePath();
- const to = createPath({ resource, type: "list" });
-
- return (
- <Button
- type="button"
- variant="ghost"
- size="icon"
- className="rounded-full"
- onClick={(e) => {
- e.preventDefault();
- navigate(to);
- }}
- >
- <ChevronLeft className="size-6" />
- <span className="sr-only">Back to list</span>
- </Button>
- );
-};
diff --git a/src/components/atomic-crm/misc/MobileBackButton.tsx b/src/components/atomic-crm/misc/MobileBackButton.tsx
new file mode 100644
index 00000000..a07b180d
--- /dev/null
+++ b/src/components/atomic-crm/misc/MobileBackButton.tsx
@@ -0,0 +1,40 @@
+import {
+ useResourceContext,
+ useCreatePath,
+ useRecordFromLocation,
+} from "ra-core";
+import { useNavigate } from "react-router";
+import { ChevronLeft } from "lucide-react";
+import { Button } from "@/components/ui/button";
+
+export const MobileBackButton = (props: { resource?: string; to?: string }) => {
+ const resource = useResourceContext(props);
+ const recordFromLocation = useRecordFromLocation();
+ const contact_id = recordFromLocation?.contact_id;
+ const navigate = useNavigate();
+ const createPath = useCreatePath();
+ const { to } = props;
+ const finalTo =
+ to ??
+ createPath({
+ resource: contact_id ? "contacts" : resource,
+ type: contact_id ? "show" : "list",
+ id: contact_id,
+ });
+
+ return (
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ className="rounded-full"
+ onClick={(e) => {
+ e.preventDefault();
+ navigate(finalTo);
+ }}
+ >
+ <ChevronLeft className="size-6" />
+ <span className="sr-only">Back to {contact_id ? "contact" : "list"}</span>
+ </Button>
+ );
+};
diff --git a/src/components/atomic-crm/notes/MobileNoteCreate.tsx b/src/components/atomic-crm/notes/MobileNoteCreate.tsx
index 778d8fbf..7b1b4ae1 100644
--- a/src/components/atomic-crm/notes/MobileNoteCreate.tsx
+++ b/src/components/atomic-crm/notes/MobileNoteCreate.tsx
@@ -10,7 +10,7 @@ import {
import { MobileContent } from "../layout/MobileContent";
import MobileHeader from "../layout/MobileHeader";
-import { ListButton } from "../misc/ListButton";
+import { MobileBackButton } from "../misc/MobileBackButton";
import { NoteCreateButton } from "./NoteCreate";
import { NoteInputs } from "./NoteInputs";
@@ -32,7 +32,7 @@ export const MobileNoteCreate = () => {
return (
<CreateBase redirect={createPath({ resource: "contacts", type: "list" })}>
<MobileHeader>
- <ListButton resource="contacts" />
+ <MobileBackButton resource="contacts" />
<div className="flex flex-1">
<h1 className="text-xl font-semibold text-ellipsis overflow-hidden whitespace-nowrap">
{!selectContact ? "Create Note for " : "Create Note"}
diff --git a/src/components/atomic-crm/notes/MobileNoteEdit.tsx b/src/components/atomic-crm/notes/MobileNoteEdit.tsx
index 7134ecf2..0bdfbc30 100644
--- a/src/components/atomic-crm/notes/MobileNoteEdit.tsx
+++ b/src/components/atomic-crm/notes/MobileNoteEdit.tsx
@@ -1,27 +1,33 @@
+import type { Identifier, RaRecord } from "ra-core";
import {
EditBase,
Form,
RecordRepresentation,
useCreatePath,
useGetIdentity,
+ WithRecord,
} from "ra-core";
-import { NoteInputs } from "./NoteInputs";
import { DeleteButton, ReferenceField, SaveButton } from "@/components/admin";
-import MobileHeader from "../layout/MobileHeader";
-import { ListButton } from "../misc/ListButton";
import { MobileContent } from "../layout/MobileContent";
+import MobileHeader from "../layout/MobileHeader";
+import { MobileBackButton } from "../misc/MobileBackButton";
+import { NoteInputs } from "./NoteInputs";
export const MobileNoteEdit = () => {
const { identity } = useGetIdentity();
const createPath = useCreatePath();
+ const getRedirectTo = (record: Partial<RaRecord<Identifier>> | undefined) =>
+ createPath({ resource: "contacts", type: "show", id: record?.contact_id });
if (!identity) return null;
return (
- <EditBase redirect={createPath({ resource: "contacts", type: "list" })}>
+ <EditBase redirect={(_resource, _id, record) => getRedirectTo(record)}>
<MobileHeader>
- <ListButton resource="contacts" />
+ <WithRecord
+ render={(record) => <MobileBackButton to={getRedirectTo(record)} />}
+ />
<div className="flex flex-1">
<ReferenceField
source="contact_id"
@@ -48,8 +54,10 @@ export const MobileNoteEdit = () => {
<NoteInputs showStatus />
<div className="flex flex-col gap-2 mt-6">
<SaveButton />
- <DeleteButton
- redirect={createPath({ resource: "contacts", type: "list" })}
+ <WithRecord
+ render={(record) => (
+ <DeleteButton redirect={getRedirectTo(record)} />
+ )}
/>
</div>
</Form>
diff --git a/src/components/atomic-crm/tasks/MobileTaskCreate.tsx b/src/components/atomic-crm/tasks/MobileTaskCreate.tsx
index c18c7c7f..29f94e11 100644
--- a/src/components/atomic-crm/tasks/MobileTaskCreate.tsx
+++ b/src/components/atomic-crm/tasks/MobileTaskCreate.tsx
@@ -13,7 +13,7 @@ import {
} from "ra-core";
import { MobileContent } from "../layout/MobileContent";
import MobileHeader from "../layout/MobileHeader";
-import { ListButton } from "../misc/ListButton";
+import { MobileBackButton } from "../misc/MobileBackButton";
import { TaskFormContent } from "./TaskFormContent";
export const MobileTaskCreate = () => {
@@ -45,7 +45,11 @@ export const MobileTaskCreate = () => {
});
notify("Task added");
- redirect("list", "tasks");
+ redirect(
+ selectContact ? "list" : "show",
+ selectContact ? "tasks" : "contacts",
+ selectContact ? undefined : contact_id,
+ );
};
if (!identity) return null;
@@ -69,7 +73,7 @@ export const MobileTaskCreate = () => {
mutationOptions={{ onSuccess: handleSuccess }}
>
<MobileHeader>
- <ListButton />
+ <MobileBackButton />
<div className="flex flex-1">
<h1 className="text-xl font-semibold text-ellipsis overflow-hidden whitespace-nowrap">
{!selectContact ? "Create Task for " : "Create Task"}
diff --git a/src/components/atomic-crm/tasks/MobileTaskEdit.tsx b/src/components/atomic-crm/tasks/MobileTaskEdit.tsx
index e13cd7da..e59e4504 100644
--- a/src/components/atomic-crm/tasks/MobileTaskEdit.tsx
+++ b/src/components/atomic-crm/tasks/MobileTaskEdit.tsx
@@ -3,38 +3,47 @@ import MobileHeader from "../layout/MobileHeader";
import { MobileContent } from "../layout/MobileContent";
import { TaskFormContent } from "./TaskFormContent";
import { DeleteButton, ReferenceField, SaveButton } from "@/components/admin";
-import { ListButton } from "../misc/ListButton";
+import { MobileBackButton } from "../misc/MobileBackButton";
+import { useLocation } from "react-router";
-export const MobileTaskEdit = () => (
- <EditBase>
- <MobileHeader>
- <ListButton />
- <div className="flex flex-1">
- <ReferenceField
- source="contact_id"
- reference="contacts"
- render={({ referenceRecord: contact }) => (
- <h1 className="text-xl font-semibold text-ellipsis overflow-hidden whitespace-nowrap">
- Edit Task
- {contact ? (
- <>
- {" for "}
- <RecordRepresentation record={contact} resource="contacts" />
- </>
- ) : null}
- </h1>
- )}
- />
- </div>
- </MobileHeader>
- <MobileContent>
- <Form>
- <TaskFormContent />
- <div className="flex flex-col gap-2 mt-6">
- <SaveButton />
- <DeleteButton />
+export const MobileTaskEdit = () => {
+ const location = useLocation();
+ const from = location.state?.from as string | undefined;
+
+ return (
+ <EditBase redirect={from}>
+ <MobileHeader>
+ <MobileBackButton to={from} />
+ <div className="flex flex-1">
+ <ReferenceField
+ source="contact_id"
+ reference="contacts"
+ render={({ referenceRecord: contact }) => (
+ <h1 className="text-xl font-semibold text-ellipsis overflow-hidden whitespace-nowrap">
+ Edit Task
+ {contact ? (
+ <>
+ {" for "}
+ <RecordRepresentation
+ record={contact}
+ resource="contacts"
+ />
+ </>
+ ) : null}
+ </h1>
+ )}
+ />
</div>
- </Form>
- </MobileContent>
- </EditBase>
-);
+ </MobileHeader>
+ <MobileContent>
+ <Form>
+ <TaskFormContent />
+ <div className="flex flex-col gap-2 mt-6">
+ <SaveButton />
+ <DeleteButton redirect={from} />
+ </div>
+ </Form>
+ </MobileContent>
+ </EditBase>
+ );
+};
diff --git a/src/components/atomic-crm/tasks/Task.tsx b/src/components/atomic-crm/tasks/Task.tsx
index 92a19506..7d998a9d 100644
--- a/src/components/atomic-crm/tasks/Task.tsx
+++ b/src/components/atomic-crm/tasks/Task.tsx
@@ -21,7 +21,7 @@ import {
import type { Contact, Task as TData } from "../types";
import { TaskEdit } from "./TaskEdit";
import { useIsMobile } from "@/hooks/use-mobile";
-import { useNavigate } from "react-router";
+import { useLocation, useNavigate } from "react-router";
export const Task = ({
task,
@@ -32,6 +32,7 @@ export const Task = ({
}) => {
const isMobile = useIsMobile();
const navigate = useNavigate();
+ const location = useLocation();
const createPath = useCreatePath();
const notify = useNotify();
const queryClient = useQueryClient();
@@ -61,7 +62,7 @@ export const Task = ({
id: task.id,
type: "edit",
});
- navigate(path);
+ navigate(path, { state: { from: location.pathname } });
return;
}
setOpenEdit(true);