Skip to content

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

add contact filter summary

Jean-Baptiste Kaiser7734a95aed21
diff --git a/registry.json b/registry.json
index af5b9b62..bf098e7c 100644
--- a/registry.json
+++ b/registry.json
@@ -408,6 +408,10 @@
"path": "src/components/atomic-crm/misc/AsideSection.tsx",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/misc/ActiveFilterButton.tsx",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/login/StartPage.tsx",
"type": "registry:component"
diff --git a/src/components/atomic-crm/contacts/ContactList.tsx b/src/components/atomic-crm/contacts/ContactList.tsx
index 445c0969..91f8d3dc 100644
--- a/src/components/atomic-crm/contacts/ContactList.tsx
+++ b/src/components/atomic-crm/contacts/ContactList.tsx
@@ -17,7 +17,10 @@ import type { Company, Contact, Sale, Tag } from "../types";
import { ContactEmpty } from "./ContactEmpty";
import { ContactImportButton } from "./ContactImportButton";
import { ContactListContent } from "./ContactListContent";
-import { ContactListFilter } from "./ContactListFilter";
+import {
+ ContactListActiveFilters,
+ ContactListFilter,
+} from "./ContactListFilter";
import { TopToolbar } from "../layout/TopToolbar";
import { useIsMobile } from "@/hooks/use-mobile";
import { InfinitePagination } from "../misc/InfinitePagination";
@@ -100,6 +103,7 @@ const MobileContactListLayout = () => {
<ContactListFilter />
</MobileHeader>
<MobileContent>
+ <ContactListActiveFilters />
<Card className="py-0">
<ContactListContent />
</Card>
diff --git a/src/components/atomic-crm/contacts/ContactListFilter.tsx b/src/components/atomic-crm/contacts/ContactListFilter.tsx
index 5499c0c2..5ca806f1 100644
--- a/src/components/atomic-crm/contacts/ContactListFilter.tsx
+++ b/src/components/atomic-crm/contacts/ContactListFilter.tsx
@@ -1,6 +1,6 @@
import { endOfYesterday, startOfMonth, startOfWeek, subMonths } from "date-fns";
import { CheckSquare, Clock, Tag, TrendingUp, Users } from "lucide-react";
-import { useGetIdentity, useGetList } from "ra-core";
+import { useGetIdentity, useGetList, useListContext } from "ra-core";
import { ToggleFilterButton } from "@/components/admin/toggle-filter-button";
import { Badge } from "@/components/ui/badge";
@@ -9,6 +9,7 @@ import { Status } from "../misc/Status";
import { useConfigurationContext } from "../root/ConfigurationContext";
import { ResponsiveFilters } from "../misc/ResponsiveFilters";
import { useIsMobile } from "@/hooks/use-mobile";
+import { ActiveFilterButton } from "../misc/ActiveFilterButton";
export const ContactListFilter = () => {
const { noteStatuses } = useConfigurationContext();
@@ -131,3 +132,110 @@ export const ContactListFilter = () => {
</ResponsiveFilters>
);
};
+
+export const ContactListActiveFilters = () => {
+ const { noteStatuses } = useConfigurationContext();
+ const { identity } = useGetIdentity();
+ const { data } = useGetList("tags", {
+ pagination: { page: 1, perPage: 10 },
+ sort: { field: "name", order: "ASC" },
+ });
+ const { filterValues } = useListContext();
+ const hasFilters = !!Object.entries(filterValues || {}).filter(
+ ([key]) => key !== "q",
+ ).length;
+
+ if (!hasFilters) {
+ return null;
+ }
+
+ return (
+ <div className="flex flex-wrap items-start mb-4 gap-1">
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label="Today"
+ value={{
+ "last_seen@gte": endOfYesterday().toISOString(),
+ "last_seen@lte": undefined,
+ }}
+ />
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label="This week"
+ value={{
+ "last_seen@gte": startOfWeek(new Date()).toISOString(),
+ "last_seen@lte": undefined,
+ }}
+ />
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label="Before this week"
+ value={{
+ "last_seen@gte": undefined,
+ "last_seen@lte": startOfWeek(new Date()).toISOString(),
+ }}
+ />
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label="Before this month"
+ value={{
+ "last_seen@gte": undefined,
+ "last_seen@lte": startOfMonth(new Date()).toISOString(),
+ }}
+ />
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label="Before last month"
+ value={{
+ "last_seen@gte": undefined,
+ "last_seen@lte": subMonths(startOfMonth(new Date()), 1).toISOString(),
+ }}
+ />
+
+ {noteStatuses.map((status) => (
+ <ActiveFilterButton
+ key={status.value}
+ className="w-auto justify-between h-8"
+ label={
+ <span>
+ {status.label} <Status status={status.value} />
+ </span>
+ }
+ value={{ status: status.value }}
+ />
+ ))}
+
+ {data &&
+ data.map((record) => (
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ key={record.id}
+ label={
+ <Badge
+ variant="secondary"
+ className="text-black text-sm md:text-xs font-normal cursor-pointer"
+ style={{
+ backgroundColor: record?.color,
+ }}
+ >
+ {record?.name}
+ </Badge>
+ }
+ value={{ "tags@cs": `{${record.id}}` }}
+ />
+ ))}
+
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label={"With pending tasks"}
+ value={{ "nb_tasks@gt": 0 }}
+ />
+
+ <ActiveFilterButton
+ className="w-auto justify-between h-8"
+ label={"Managed by me"}
+ value={{ sales_id: identity?.id }}
+ />
+ </div>
+ );
+};
diff --git a/src/components/atomic-crm/misc/ActiveFilterButton.tsx b/src/components/atomic-crm/misc/ActiveFilterButton.tsx
new file mode 100644
index 00000000..78e50cb1
--- /dev/null
+++ b/src/components/atomic-crm/misc/ActiveFilterButton.tsx
@@ -0,0 +1,72 @@
+import React from "react";
+import { cn } from "@/lib/utils";
+import { Button } from "@/components/ui/button";
+import { useListContext, useTranslate } from "ra-core";
+import matches from "lodash/matches";
+import pickBy from "lodash/pickBy";
+import { CircleX } from "lucide-react";
+
+/**
+ * A button that renders only if a specific filter is on, and allows to remove the filter.
+ *
+ * @example
+ * import { ActiveFilterButton } from '@/components/atomic-crm/misc';
+ *
+ * const PostFilters = () => (
+ * <div className="flex flex-row gap-2">
+ * <ActiveFilterButton label="Published" value={{ status: 'published' }} />
+ * <ActiveFilterButton label="Draft" value={{ status: 'draft' }} />
+ * </div>
+ * );
+ */
+export const ActiveFilterButton = ({
+ label,
+ size = "sm",
+ value,
+ className,
+}: {
+ label: React.ReactElement | string;
+ value: any;
+ className?: string;
+ size?: "default" | "sm" | "lg" | "icon" | null | undefined;
+}) => {
+ const { filterValues, setFilters } = useListContext();
+ const translate = useTranslate();
+ const isSelected = getIsSelected(value, filterValues);
+ const handleClick = () => setFilters(toggleFilter(value, filterValues));
+ return isSelected ? (
+ <Button
+ variant="secondary"
+ onClick={handleClick}
+ className={cn(
+ "cursor-pointer",
+ "flex flex-row items-center justify-between gap-2 px-2 w-full h-8",
+ className,
+ )}
+ size={size}
+ >
+ {typeof label === "string" ? translate(label, { _: label }) : label}
+ <CircleX className="opacity-50" />
+ </Button>
+ ) : null;
+};
+
+const toggleFilter = (value: any, filters: any) => {
+ const isSelected = matches(
+ pickBy(value, (val) => typeof val !== "undefined"),
+ )(filters);
+
+ if (isSelected) {
+ const keysToRemove = Object.keys(value);
+ return Object.keys(filters).reduce(
+ (acc, key) =>
+ keysToRemove.includes(key) ? acc : { ...acc, [key]: filters[key] },
+ {},
+ );
+ }
+
+ return { ...filters, ...value };
+};
+
+const getIsSelected = (value: any, filters: any) =>
+ matches(pickBy(value, (val) => typeof val !== "undefined"))(filters);