Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript

feat(app): warm the record a row opens, on hover

grimf223d263cf7a
diff --git a/apps/app/app/(app)/companies/companies-table.tsx b/apps/app/app/(app)/companies/companies-table.tsx
index b769629f..dd9e8ae1 100644
--- a/apps/app/app/(app)/companies/companies-table.tsx
+++ b/apps/app/app/(app)/companies/companies-table.tsx
@@ -19,6 +19,7 @@ import {
isEnriching,
} from "@/components/crm/enrichment-status";
import { OwnerCell } from "@/components/crm/owner-cell";
+import { usePrefetchRecord } from "@/components/crm/record-sheet/record-prefetch";
import { useOpenRecord } from "@/components/crm/record-sheet/record-stack";
import { useTableQuery } from "@/components/data-table/use-table-query";
import { useTRPC } from "@/lib/trpc/client";
@@ -146,6 +147,7 @@ const COLUMNS: DataTableColumn<CompanyRow>[] = [
export function CompaniesTable() {
const openRecord = useOpenRecord();
const trpc = useTRPC();
+ const prefetchRecord = usePrefetchRecord();
const { query, input } = useTableQuery(companiesSearchParams);
const companies = useQuery({
@@ -210,6 +212,7 @@ export function CompaniesTable() {
facets={facets}
getRowId={(row) => row.id}
loading={companies.isFetching}
+ onRowHover={(row) => prefetchRecord({ kind: "company", id: row.id })}
onRowClick={(row) => openRecord({ kind: "company", id: row.id })}
empty="No companies match this view."
/>
diff --git a/apps/app/app/(app)/contacts/contacts-table.tsx b/apps/app/app/(app)/contacts/contacts-table.tsx
index c3fa99a3..3ee24496 100644
--- a/apps/app/app/(app)/contacts/contacts-table.tsx
+++ b/apps/app/app/(app)/contacts/contacts-table.tsx
@@ -12,6 +12,7 @@ import { useQuery } from "@tanstack/react-query";
import { CompanyCell } from "@/components/crm/company-cell";
import { contactName } from "@/components/crm/contact-name";
import { OwnerCell } from "@/components/crm/owner-cell";
+import { usePrefetchRecord } from "@/components/crm/record-sheet/record-prefetch";
import { useOpenRecord } from "@/components/crm/record-sheet/record-stack";
import { useTableQuery } from "@/components/data-table/use-table-query";
import { useTRPC } from "@/lib/trpc/client";
@@ -113,6 +114,7 @@ const COLUMNS: DataTableColumn<ContactRow>[] = [
export function ContactsTable() {
const openRecord = useOpenRecord();
const trpc = useTRPC();
+ const prefetchRecord = usePrefetchRecord();
const { query, input } = useTableQuery(contactsSearchParams);
const contacts = useQuery({
@@ -159,6 +161,7 @@ export function ContactsTable() {
facets={facets}
getRowId={(row) => row.id}
loading={contacts.isFetching}
+ onRowHover={(row) => prefetchRecord({ kind: "contact", id: row.id })}
onRowClick={(row) => openRecord({ kind: "contact", id: row.id })}
empty="No contacts match this view."
/>
diff --git a/apps/app/app/(app)/deals/deals-table.tsx b/apps/app/app/(app)/deals/deals-table.tsx
index 3343bb27..91b4d767 100644
--- a/apps/app/app/(app)/deals/deals-table.tsx
+++ b/apps/app/app/(app)/deals/deals-table.tsx
@@ -12,6 +12,7 @@ import { CLOSING_OPTIONS } from "@/components/crm/closing-window";
import { CompanyCell } from "@/components/crm/company-cell";
import { DEAL_STAGE_OPTIONS } from "@/components/crm/deal-stage";
import { OwnerCell } from "@/components/crm/owner-cell";
+import { usePrefetchRecord } from "@/components/crm/record-sheet/record-prefetch";
import { useOpenRecord } from "@/components/crm/record-sheet/record-stack";
import { DealStageMenu } from "@/components/crm/stage-change";
import { useTableQuery } from "@/components/data-table/use-table-query";
@@ -126,6 +127,7 @@ const COLUMNS: DataTableColumn<DealRow>[] = [
export function DealsTable() {
const openRecord = useOpenRecord();
const trpc = useTRPC();
+ const prefetchRecord = usePrefetchRecord();
const { query, input } = useTableQuery(dealsSearchParams);
const deals = useQuery({
@@ -180,6 +182,7 @@ export function DealsTable() {
}}
getRowId={(row) => row.id}
loading={deals.isFetching}
+ onRowHover={(row) => prefetchRecord({ kind: "deal", id: row.id })}
onRowClick={(row) => openRecord({ kind: "deal", id: row.id })}
empty="No deals match this view."
meta={
diff --git a/apps/app/components/crm/record-sheet/record-prefetch.ts b/apps/app/components/crm/record-sheet/record-prefetch.ts
new file mode 100644
index 00000000..dbf479f0
--- /dev/null
+++ b/apps/app/components/crm/record-sheet/record-prefetch.ts
@@ -0,0 +1,49 @@
+"use client";
+
+import { useQueryClient } from "@tanstack/react-query";
+import { useCallback } from "react";
+import { useTRPC } from "@/lib/trpc/client";
+import type { RecordRef } from "./record-stack";
+
+/**
+ * Warms the record a row would open, on hover.
+ *
+ * Opening a sheet is otherwise a cold fetch, so the panel arrives with a
+ * spinner and a placeholder title and only becomes the record a moment later —
+ * which reads as the sheet not having opened at all. A hover is half a second
+ * of warning, and it is free: the query client's 30s `staleTime` means moving
+ * back over the same row costs nothing.
+ *
+ * Takes a `RecordRef` so it is the exact twin of `useOpenRecord` at the call
+ * site — `prefetchRecord(ref)` on hover, `openRecord(ref)` on click. Which
+ * query backs which kind is decided here rather than in each of the three
+ * tables, for the same reason `useCrmCache` owns invalidation.
+ */
+export function usePrefetchRecord() {
+ const trpc = useTRPC();
+ const queryClient = useQueryClient();
+
+ return useCallback(
+ ({ kind, id }: RecordRef) => {
+ // A switch rather than a kind → options lookup: each procedure returns a
+ // different row shape, so a map of them is a union `prefetchQuery` will
+ // not accept. Three lines, and every branch keeps its own types.
+ switch (kind) {
+ case "company":
+ void queryClient.prefetchQuery(
+ trpc.companies.byId.queryOptions({ id }),
+ );
+ return;
+ case "contact":
+ void queryClient.prefetchQuery(
+ trpc.contacts.byId.queryOptions({ id }),
+ );
+ return;
+ case "deal":
+ void queryClient.prefetchQuery(trpc.deals.byId.queryOptions({ id }));
+ return;
+ }
+ },
+ [trpc, queryClient],
+ );
+}
diff --git a/packages/ui/src/components/data-table.tsx b/packages/ui/src/components/data-table.tsx
index 68409136..5dc496bb 100644
--- a/packages/ui/src/components/data-table.tsx
+++ b/packages/ui/src/components/data-table.tsx
@@ -88,6 +88,15 @@ export type DataTableProps<TRow, TSub> = {
facets?: DataTableFacet[];
tabs?: DataTableTabs;
onRowClick?: (row: TRow) => void;
+ /**
+ * The pointer or the keyboard has landed on a row, ahead of any click.
+ *
+ * For warming the record the row opens. Opening a sheet is a cold fetch
+ * otherwise, so the panel appears with a spinner and a placeholder title
+ * and only becomes the record a moment later — which reads as the sheet
+ * not having opened at all.
+ */
+ onRowHover?: (row: TRow) => void;
expandable?: DataTableExpandable<TRow, TSub>;
actions?: ReactNode;
leadingActions?: ReactNode;
@@ -157,6 +166,7 @@ export function DataTable<TRow, TSub = unknown>({
facets,
tabs,
onRowClick,
+ onRowHover,
expandable,
actions,
leadingActions,
@@ -530,6 +540,10 @@ export function DataTable<TRow, TSub = unknown>({
<Fragment key={id}>
<TableRow
onClick={clickable ? handleClick : undefined}
+ onMouseEnter={
+ onRowHover ? () => onRowHover(row) : undefined
+ }
+ onFocus={onRowHover ? () => onRowHover(row) : undefined}
className={
clickable
? anyExpandable