Skip to content

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

add mobile contact list

Jean-Baptiste Kaiser6c56b89a95ee
diff --git a/registry.json b/registry.json
index d86b47ee..4493c26a 100644
--- a/registry.json
+++ b/registry.json
@@ -344,6 +344,10 @@
"path": "src/components/atomic-crm/misc/RelativeDate.tsx",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/misc/InfinitePagination.tsx",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/misc/ImageEditorField.tsx",
"type": "registry:component"
diff --git a/src/components/atomic-crm/contacts/ContactList.tsx b/src/components/atomic-crm/contacts/ContactList.tsx
index 94817df2..c6561aee 100644
--- a/src/components/atomic-crm/contacts/ContactList.tsx
+++ b/src/components/atomic-crm/contacts/ContactList.tsx
@@ -1,6 +1,7 @@
import jsonExport from "jsonexport/dist";
import {
downloadCSV,
+ InfiniteListBase,
useGetIdentity,
useListContext,
type Exporter,
@@ -8,7 +9,7 @@ import {
import { BulkActionsToolbar } from "@/components/admin/bulk-actions-toolbar";
import { CreateButton } from "@/components/admin/create-button";
import { ExportButton } from "@/components/admin/export-button";
-import { List } from "@/components/admin/list";
+import { List, ListView } from "@/components/admin/list";
import { SortButton } from "@/components/admin/sort-button";
import { Card } from "@/components/ui/card";
@@ -18,8 +19,15 @@ import { ContactImportButton } from "./ContactImportButton";
import { ContactListContent } from "./ContactListContent";
import { ContactListFilter } from "./ContactListFilter";
import { TopToolbar } from "../layout/TopToolbar";
+import { useIsMobile } from "@/hooks/use-mobile";
+import { InfinitePagination } from "../misc/InfinitePagination";
export const ContactList = () => {
+ const isMobile = useIsMobile();
+ return isMobile ? <ContactListMobile /> : <ContactListDesktop />;
+};
+
+const ContactListDesktop = () => {
const { identity } = useGetIdentity();
if (!identity) return null;
@@ -32,12 +40,30 @@ export const ContactList = () => {
sort={{ field: "last_seen", order: "DESC" }}
exporter={exporter}
>
- <ContactListLayout />
+ <DesktopContactListLayout />
</List>
);
};
-const ContactListLayout = () => {
+const ContactListMobile = () => {
+ const { identity } = useGetIdentity();
+
+ if (!identity) return null;
+
+ return (
+ <InfiniteListBase
+ perPage={25}
+ sort={{ field: "last_seen", order: "DESC" }}
+ exporter={exporter}
+ >
+ <ListView pagination={<InfinitePagination />} actions={false}>
+ <MobileContactListLayout />
+ </ListView>
+ </InfiniteListBase>
+ );
+};
+
+const DesktopContactListLayout = () => {
const { data, isPending, filterValues } = useListContext();
const { identity } = useGetIdentity();
@@ -60,6 +86,26 @@ const ContactListLayout = () => {
);
};
+const MobileContactListLayout = () => {
+ const { data, isPending, filterValues } = useListContext();
+ const { identity } = useGetIdentity();
+
+ const hasFilters = filterValues && Object.keys(filterValues).length > 0;
+
+ if (!identity || isPending) return null;
+
+ if (!data?.length && !hasFilters) return <ContactEmpty />;
+
+ return (
+ <div>
+ <ContactListFilter />
+ <Card className="py-0">
+ <ContactListContent />
+ </Card>
+ </div>
+ );
+};
+
const ContactListActions = () => (
<TopToolbar>
<SortButton fields={["first_name", "last_name", "last_seen"]} />
diff --git a/src/components/atomic-crm/contacts/ContactListContent.tsx b/src/components/atomic-crm/contacts/ContactListContent.tsx
index 5c73e707..bedc2c71 100644
--- a/src/components/atomic-crm/contacts/ContactListContent.tsx
+++ b/src/components/atomic-crm/contacts/ContactListContent.tsx
@@ -14,13 +14,7 @@ import { Avatar } from "./Avatar";
import { TagsList } from "./TagsList";
export const ContactListContent = () => {
- const {
- data: contacts,
- error,
- isPending,
- onToggleItem,
- selectedIds,
- } = useListContext<Contact>();
+ const { data: contacts, error, isPending } = useListContext<Contact>();
const isSmall = useIsMobile();
// StopPropagation does not work for some reason on Checkbox, this handler is a workaround
@@ -39,7 +33,6 @@ export const ContactListContent = () => {
if (error) {
return null;
}
- const now = Date.now();
return (
<div className="divide-y">
@@ -50,17 +43,90 @@ export const ContactListContent = () => {
className="flex flex-row gap-4 items-center px-4 py-2 hover:bg-muted transition-colors first:rounded-t-xl last:rounded-b-xl"
onClick={handleLinkClick}
>
- <Checkbox
- className="cursor-pointer"
- checked={selectedIds.includes(contact.id)}
- onCheckedChange={() => onToggleItem(contact.id)}
- />
- <Avatar />
- <div className="flex-1 min-w-0">
- <div className="font-medium">
- {`${contact.first_name} ${contact.last_name ?? ""}`}
- </div>
- <div className="text-sm text-muted-foreground">
+ {isSmall ? (
+ <ContactItemContentMobile contact={contact} />
+ ) : (
+ <ContactItemContentDesktop contact={contact} />
+ )}
+ </Link>
+ </RecordContextProvider>
+ ))}
+
+ {contacts.length === 0 && (
+ <div className="p-4">
+ <div className="text-muted-foreground">No contacts found</div>
+ </div>
+ )}
+ </div>
+ );
+};
+
+const ContactItemContentDesktop = ({ contact }: { contact: Contact }) => {
+ const { onToggleItem, selectedIds } = useListContext<Contact>();
+ const now = Date.now();
+
+ return (
+ <>
+ <Checkbox
+ className="cursor-pointer"
+ checked={selectedIds.includes(contact.id)}
+ onCheckedChange={() => onToggleItem(contact.id)}
+ />
+ <Avatar />
+ <div className="flex-1 min-w-0">
+ <div className="font-medium">
+ {`${contact.first_name} ${contact.last_name ?? ""}`}
+ </div>
+ <div className="text-sm text-muted-foreground">
+ {contact.title}
+ {contact.title && contact.company_id != null && " at "}
+ {contact.company_id != null && (
+ <ReferenceField
+ source="company_id"
+ reference="companies"
+ link={false}
+ >
+ <TextField source="name" />
+ </ReferenceField>
+ )}
+ {contact.nb_tasks
+ ? ` - ${contact.nb_tasks} task${contact.nb_tasks > 1 ? "s" : ""}`
+ : ""}
+ &nbsp;&nbsp;
+ <TagsList />
+ </div>
+ </div>
+ {contact.last_seen && (
+ <div className="text-right ml-4">
+ <div
+ className="text-sm text-muted-foreground"
+ title={contact.last_seen}
+ >
+ {"last activity "}
+ {formatRelative(contact.last_seen, now)}{" "}
+ <Status status={contact.status} />
+ </div>
+ </div>
+ )}
+ </>
+ );
+};
+
+const ContactItemContentMobile = ({ contact }: { contact: Contact }) => {
+ return (
+ <>
+ <Avatar />
+ <div className="flex flex-col grow justify-between">
+ <div className="flex-1 min-w-0">
+ <div className="flex justify-between">
+ <div className="font-medium">
+ {`${contact.first_name} ${contact.last_name ?? ""}`}
+ </div>
+ <Status status={contact.status} />
+ </div>
+ <div className="text-sm text-muted-foreground">
+ <div className="flex flex-col gap-1">
+ <span>
{contact.title}
{contact.title && contact.company_id != null && " at "}
{contact.company_id != null && (
@@ -72,36 +138,16 @@ export const ContactListContent = () => {
<TextField source="name" />
</ReferenceField>
)}
- {contact.nb_tasks
- ? ` - ${contact.nb_tasks} task${
- contact.nb_tasks > 1 ? "s" : ""
- }`
- : ""}
- &nbsp;&nbsp;
- <TagsList />
- </div>
+ </span>
+ {contact.nb_tasks ? (
+ <span>
+ {contact.nb_tasks} task{contact.nb_tasks > 1 ? "s" : ""}
+ </span>
+ ) : null}
</div>
- {contact.last_seen && (
- <div className="text-right ml-4">
- <div
- className="text-sm text-muted-foreground"
- title={contact.last_seen}
- >
- {!isSmall && "last activity "}
- {formatRelative(contact.last_seen, now)}{" "}
- <Status status={contact.status} />
- </div>
- </div>
- )}
- </Link>
- </RecordContextProvider>
- ))}
-
- {contacts.length === 0 && (
- <div className="p-4">
- <div className="text-muted-foreground">No contacts found</div>
+ </div>
</div>
- )}
- </div>
+ </div>
+ </>
);
};
diff --git a/src/components/atomic-crm/misc/InfinitePagination.tsx b/src/components/atomic-crm/misc/InfinitePagination.tsx
new file mode 100644
index 00000000..0fff5a37
--- /dev/null
+++ b/src/components/atomic-crm/misc/InfinitePagination.tsx
@@ -0,0 +1,114 @@
+import * as React from "react";
+import { useEffect, useRef } from "react";
+import {
+ useInfinitePaginationContext,
+ useListContext,
+ useEvent,
+} from "ra-core";
+import { Item, ItemContent, ItemMedia, ItemTitle } from "@/components/ui/item";
+import { Spinner } from "@/components/admin/spinner";
+
+/**
+ * A pagination component that loads more results when the user scrolls to the bottom of the list.
+ *
+ * Used as the default pagination component in the <InfiniteList> component.
+ *
+ * @example
+ * import { InfiniteList, InfinitePagination, Datagrid, TextField } from 'react-admin';
+ *
+ * const PostList = () => (
+ * <InfiniteList pagination={<InfinitePagination sx={{ py: 5 }} />}>
+ * <Datagrid>
+ * <TextField source="id" />
+ * <TextField source="title" />
+ * </Datagrid>
+ * </InfiniteList>
+ * );
+ */
+export const InfinitePagination = ({
+ offline = null,
+ options = defaultOptions,
+}: InfinitePaginationProps) => {
+ const { isPaused, isPending } = useListContext();
+ const { fetchNextPage, hasNextPage, isFetchingNextPage } =
+ useInfinitePaginationContext();
+
+ if (!fetchNextPage) {
+ throw new Error(
+ "InfinitePagination must be used inside an InfinitePaginationContext, usually created by <InfiniteList>. You cannot use it as child of a <List> component.",
+ );
+ }
+
+ const [hasRequestedNextPage, setHasRequestedNextPage] = React.useState(false);
+ const observerElem = useRef(null);
+ const handleObserver = useEvent<[IntersectionObserverEntry[]], void>(
+ (entries) => {
+ const [target] = entries;
+ if (target.isIntersecting && hasNextPage && !isFetchingNextPage) {
+ setHasRequestedNextPage(true);
+ fetchNextPage();
+ }
+ },
+ );
+
+ useEffect(() => {
+ // Whenever the query is unpaused, reset the requested next page state
+ if (!isPaused) {
+ setHasRequestedNextPage(false);
+ }
+ }, [isPaused]);
+
+ useEffect(() => {
+ const element = observerElem.current;
+ if (!element) return;
+ const observer = new IntersectionObserver(handleObserver, options);
+ observer.observe(element);
+ return () => observer.unobserve(element);
+ }, [
+ fetchNextPage,
+ hasNextPage,
+ handleObserver,
+ options,
+ isPending,
+ isFetchingNextPage,
+ ]);
+
+ if (isPending) return null;
+
+ const showOffline =
+ isPaused &&
+ hasNextPage &&
+ hasRequestedNextPage &&
+ offline !== false &&
+ offline !== undefined;
+
+ return (
+ <div ref={observerElem} className="py-2 text-center">
+ {showOffline ? (
+ offline
+ ) : isFetchingNextPage && hasNextPage ? (
+ <Item variant="default">
+ <ItemMedia>
+ <Spinner />
+ </ItemMedia>
+ <ItemContent>
+ <ItemTitle className="line-clamp-1">Loading...</ItemTitle>
+ </ItemContent>
+ </Item>
+ ) : (
+ <Item variant="default">
+ <ItemContent>
+ <ItemTitle className="line-clamp-1">&nbsp;</ItemTitle>
+ </ItemContent>
+ </Item>
+ )}
+ </div>
+ );
+};
+
+const defaultOptions = { threshold: 0 };
+
+export interface InfinitePaginationProps {
+ offline?: React.ReactNode;
+ options?: IntersectionObserverInit;
+}
diff --git a/src/components/ui/item.tsx b/src/components/ui/item.tsx
new file mode 100644
index 00000000..d97de21e
--- /dev/null
+++ b/src/components/ui/item.tsx
@@ -0,0 +1,193 @@
+import * as React from "react"
+import { Slot } from "@radix-ui/react-slot"
+import { cva, type VariantProps } from "class-variance-authority"
+
+import { cn } from "@/lib/utils"
+import { Separator } from "@/components/ui/separator"
+
+function ItemGroup({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ role="list"
+ data-slot="item-group"
+ className={cn("group/item-group flex flex-col", className)}
+ {...props}
+ />
+ )
+}
+
+function ItemSeparator({
+ className,
+ ...props
+}: React.ComponentProps<typeof Separator>) {
+ return (
+ <Separator
+ data-slot="item-separator"
+ orientation="horizontal"
+ className={cn("my-0", className)}
+ {...props}
+ />
+ )
+}
+
+const itemVariants = cva(
+ "group/item flex items-center border border-transparent text-sm rounded-md transition-colors [a]:hover:bg-accent/50 [a]:transition-colors duration-100 flex-wrap outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
+ {
+ variants: {
+ variant: {
+ default: "bg-transparent",
+ outline: "border-border",
+ muted: "bg-muted/50",
+ },
+ size: {
+ default: "p-4 gap-4 ",
+ sm: "py-3 px-4 gap-2.5",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ size: "default",
+ },
+ }
+)
+
+function Item({
+ className,
+ variant = "default",
+ size = "default",
+ asChild = false,
+ ...props
+}: React.ComponentProps<"div"> &
+ VariantProps<typeof itemVariants> & { asChild?: boolean }) {
+ const Comp = asChild ? Slot : "div"
+ return (
+ <Comp
+ data-slot="item"
+ data-variant={variant}
+ data-size={size}
+ className={cn(itemVariants({ variant, size, className }))}
+ {...props}
+ />
+ )
+}
+
+const itemMediaVariants = cva(
+ "flex shrink-0 items-center justify-center gap-2 group-has-[[data-slot=item-description]]/item:self-start [&_svg]:pointer-events-none group-has-[[data-slot=item-description]]/item:translate-y-0.5",
+ {
+ variants: {
+ variant: {
+ default: "bg-transparent",
+ icon: "size-8 border rounded-sm bg-muted [&_svg:not([class*='size-'])]:size-4",
+ image:
+ "size-10 rounded-sm overflow-hidden [&_img]:size-full [&_img]:object-cover",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ }
+)
+
+function ItemMedia({
+ className,
+ variant = "default",
+ ...props
+}: React.ComponentProps<"div"> & VariantProps<typeof itemMediaVariants>) {
+ return (
+ <div
+ data-slot="item-media"
+ data-variant={variant}
+ className={cn(itemMediaVariants({ variant, className }))}
+ {...props}
+ />
+ )
+}
+
+function ItemContent({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="item-content"
+ className={cn(
+ "flex flex-1 flex-col gap-1 [&+[data-slot=item-content]]:flex-none",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function ItemTitle({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="item-title"
+ className={cn(
+ "flex w-fit items-center gap-2 text-sm leading-snug font-medium",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function ItemDescription({ className, ...props }: React.ComponentProps<"p">) {
+ return (
+ <p
+ data-slot="item-description"
+ className={cn(
+ "text-muted-foreground line-clamp-2 text-sm leading-normal font-normal text-balance",
+ "[&>a:hover]:text-primary [&>a]:underline [&>a]:underline-offset-4",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function ItemActions({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="item-actions"
+ className={cn("flex items-center gap-2", className)}
+ {...props}
+ />
+ )
+}
+
+function ItemHeader({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="item-header"
+ className={cn(
+ "flex basis-full items-center justify-between gap-2",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function ItemFooter({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="item-footer"
+ className={cn(
+ "flex basis-full items-center justify-between gap-2",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+export {
+ Item,
+ ItemMedia,
+ ItemContent,
+ ItemActions,
+ ItemGroup,
+ ItemSeparator,
+ ItemTitle,
+ ItemDescription,
+ ItemHeader,
+ ItemFooter,
+}
diff --git a/src/components/ui/separator.tsx b/src/components/ui/separator.tsx
index 3cf4f89b..bb3ad74c 100644
--- a/src/components/ui/separator.tsx
+++ b/src/components/ui/separator.tsx
@@ -11,7 +11,7 @@ function Separator({
}: React.ComponentProps<typeof SeparatorPrimitive.Root>) {
return (
<SeparatorPrimitive.Root
- data-slot="separator-root"
+ data-slot="separator"
decorative={decorative}
orientation={orientation}
className={cn(