import jsonExport from "jsonexport/dist";
import {
downloadCSV,
useGetIdentity,
useListContext,
type Exporter,
} from "ra-core";
import { BulkActionsToolbar } from "@/components/ds/admin/bulk-actions-toolbar";
import { CreateButton } from "@/components/ds/admin/create-button";
import { ExportButton } from "@/components/ds/admin/export-button";
import { List } from "@/components/ds/admin/list";
import { SortButton } from "@/components/ds/admin/sort-button";
import { Card } from "@/components/ds/ui/card";
import { Skeleton } from "@/components/ds/ui/skeleton";
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 { TopToolbar } from "../layout/TopToolbar";
export const ContactList = () => {
return (
}
perPage={25}
sort={{ field: "last_seen", order: "DESC" }}
exporter={exporter}
>
);
};
const ContactListLayout = () => {
const { data, isPending, filterValues } = useListContext();
const { identity } = useGetIdentity();
const hasFilters = filterValues && Object.keys(filterValues).length > 0;
// Show loading skeleton while identity or data is loading
if (!identity || isPending) {
return (
);
}
if (!data?.length && !hasFilters) return ;
return (
);
};
const ContactListActions = () => (
);
const exporter: Exporter = async (records, fetchRelatedRecords) => {
const companies = await fetchRelatedRecords(
records,
"company_id",
"companies",
);
const sales = await fetchRelatedRecords(records, "sales_id", "sales");
const tags = await fetchRelatedRecords(records, "tags", "tags");
const contacts = records.map((contact) => {
const exportedContact = {
...contact,
company:
contact.company_id != null
? companies[contact.company_id].name
: undefined,
sales: `${sales[contact.sales_id].first_name} ${
sales[contact.sales_id].last_name
}`,
tags: contact.tags?.map((tagId) => tags[tagId].name).join(", ") ?? "",
email_work: contact.email_jsonb?.find((email) => email.type === "Work")
?.email,
email_home: contact.email_jsonb?.find((email) => email.type === "Home")
?.email,
email_other: contact.email_jsonb?.find((email) => email.type === "Other")
?.email,
email_jsonb: JSON.stringify(contact.email_jsonb),
email_fts: undefined,
phone_work: contact.phone_jsonb?.find((phone) => phone.type === "Work")
?.number,
phone_home: contact.phone_jsonb?.find((phone) => phone.type === "Home")
?.number,
phone_other: contact.phone_jsonb?.find((phone) => phone.type === "Other")
?.number,
phone_jsonb: JSON.stringify(contact.phone_jsonb),
phone_fts: undefined,
};
delete exportedContact.email_fts;
delete exportedContact.phone_fts;
return exportedContact;
});
return jsonExport(contacts, {}, (_err: any, csv: string) => {
downloadCSV(csv, "contacts");
});
};