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): move list search into the page header

grimb7872f5c7a23
diff --git a/apps/app/app/(app)/companies/companies-table.tsx b/apps/app/app/(app)/companies/companies-table.tsx
index 48f865d0..b769629f 100644
--- a/apps/app/app/(app)/companies/companies-table.tsx
+++ b/apps/app/app/(app)/companies/companies-table.tsx
@@ -210,7 +210,6 @@ export function CompaniesTable() {
facets={facets}
getRowId={(row) => row.id}
loading={companies.isFetching}
- searchPlaceholder="Search companies by name or domain…"
onRowClick={(row) => openRecord({ kind: "company", id: row.id })}
empty="No companies match this view."
/>
diff --git a/apps/app/app/(app)/companies/page.tsx b/apps/app/app/(app)/companies/page.tsx
index 6d75fa05..4f224fc7 100644
--- a/apps/app/app/(app)/companies/page.tsx
+++ b/apps/app/app/(app)/companies/page.tsx
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import type { SearchParams } from "nuqs/server";
+import { ListSearch } from "@/components/data-table/list-search";
import {
PageShell,
PageShellActions,
@@ -51,6 +52,7 @@ export default async function CompaniesPage({
</PageShellDescription>
</PageShellHeading>
<PageShellActions>
+ <ListSearch placeholder="Search companies by name or domain…" />
<CreateCompanySheet />
</PageShellActions>
</PageShellHeader>
diff --git a/apps/app/app/(app)/contacts/contacts-table.tsx b/apps/app/app/(app)/contacts/contacts-table.tsx
index 1cbe9599..c3fa99a3 100644
--- a/apps/app/app/(app)/contacts/contacts-table.tsx
+++ b/apps/app/app/(app)/contacts/contacts-table.tsx
@@ -159,7 +159,6 @@ export function ContactsTable() {
facets={facets}
getRowId={(row) => row.id}
loading={contacts.isFetching}
- searchPlaceholder="Search by name, email or company…"
onRowClick={(row) => openRecord({ kind: "contact", id: row.id })}
empty="No contacts match this view."
/>
diff --git a/apps/app/app/(app)/contacts/page.tsx b/apps/app/app/(app)/contacts/page.tsx
index 60efc7d1..63f6fd48 100644
--- a/apps/app/app/(app)/contacts/page.tsx
+++ b/apps/app/app/(app)/contacts/page.tsx
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import type { SearchParams } from "nuqs/server";
+import { ListSearch } from "@/components/data-table/list-search";
import {
PageShell,
PageShellActions,
@@ -47,6 +48,7 @@ export default async function ContactsPage({
<PageShellDescription>Everyone in the pipeline.</PageShellDescription>
</PageShellHeading>
<PageShellActions>
+ <ListSearch placeholder="Search by name, email or company…" />
<CreateContactSheet />
</PageShellActions>
</PageShellHeader>
diff --git a/apps/app/app/(app)/deals/deals-table.tsx b/apps/app/app/(app)/deals/deals-table.tsx
index 114ccade..3343bb27 100644
--- a/apps/app/app/(app)/deals/deals-table.tsx
+++ b/apps/app/app/(app)/deals/deals-table.tsx
@@ -180,7 +180,6 @@ export function DealsTable() {
}}
getRowId={(row) => row.id}
loading={deals.isFetching}
- searchPlaceholder="Search deals by name or company…"
onRowClick={(row) => openRecord({ kind: "deal", id: row.id })}
empty="No deals match this view."
meta={
diff --git a/apps/app/app/(app)/deals/page.tsx b/apps/app/app/(app)/deals/page.tsx
index 3af54650..ca349bea 100644
--- a/apps/app/app/(app)/deals/page.tsx
+++ b/apps/app/app/(app)/deals/page.tsx
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import type { SearchParams } from "nuqs/server";
+import { ListSearch } from "@/components/data-table/list-search";
import {
PageShell,
PageShellActions,
@@ -52,6 +53,7 @@ export default async function DealsPage({
</PageShellDescription>
</PageShellHeading>
<PageShellActions>
+ <ListSearch placeholder="Search deals by name or company…" />
<CreateDealSheet />
</PageShellActions>
</PageShellHeader>
diff --git a/apps/app/components/data-table/list-search-params.ts b/apps/app/components/data-table/list-search-params.ts
index e439639e..dbb0f180 100644
--- a/apps/app/components/data-table/list-search-params.ts
+++ b/apps/app/components/data-table/list-search-params.ts
@@ -1,7 +1,6 @@
import type { SortDirection } from "@crm/ui/lib/table-query";
import {
createLoader,
- debounce,
type LoaderFunction,
type ParserBuilder,
parseAsInteger,
@@ -24,6 +23,22 @@ const SORT_DIRECTIONS = ["asc", "desc"] as const;
type StringParser = ParserBuilder<string> & { defaultValue: string };
+/**
+ * The two keys the search box owns, identical on every list.
+ *
+ * Exported so `ListSearch` can bind to them without being handed a module's
+ * whole parser map. Search genuinely does not care which list it is on, and
+ * taking the map would cost more than it looks: parsers carry functions, a
+ * server component cannot pass a function to a client one, so every page would
+ * need a client wrapper of its own whose only job is to import the map on the
+ * right side of the boundary.
+ */
+export const searchParsers = {
+ q: parseAsString.withDefault(""),
+ // Paging is the one table change worth a Back button.
+ page: parseAsInteger.withDefault(1).withOptions({ history: "push" }),
+};
+
/**
* Base keys every list has, plus one string key per tab and facet.
*
@@ -103,16 +118,9 @@ export function createListSearchParams<
}
const parsers = {
- // Typing writes the URL once the user pauses. The value the input renders
- // updates immediately regardless — nuqs returns the queued value — so this
- // costs nothing in responsiveness; it just keeps the history clean.
- q: parseAsString.withDefault("").withOptions({
- limitUrlUpdates: debounce(300),
- }),
+ ...searchParsers,
sort: parseAsString.withDefault(defaultSort),
dir: parseAsStringLiteral(SORT_DIRECTIONS).withDefault(defaultDir),
- // Paging is the one table change worth a Back button.
- page: parseAsInteger.withDefault(1).withOptions({ history: "push" }),
...extras,
} as ListParsers<TTab | TFacet>;
diff --git a/apps/app/components/data-table/list-search.tsx b/apps/app/components/data-table/list-search.tsx
new file mode 100644
index 00000000..cd3e6cc5
--- /dev/null
+++ b/apps/app/components/data-table/list-search.tsx
@@ -0,0 +1,46 @@
+"use client";
+
+import Search from "@carbon/icons-react/es/Search";
+import {
+ InputGroup,
+ InputGroupAddon,
+ InputGroupInput,
+} from "@crm/ui/components/input-group";
+import { useSearchInput } from "@crm/ui/hooks/use-search-input";
+import { useQueryStates } from "nuqs";
+import { searchParsers } from "./list-search-params";
+
+/**
+ * The list's search box, in the page header rather than above the table.
+ *
+ * It shares no props with the table and needs none: both read `q` out of the
+ * URL, so typing here updates the table without either knowing the other
+ * exists. The table renders no search field at all, so there is only ever one
+ * input bound to the value.
+ *
+ * `q` and `page` are the same two keys on every list, so this takes no parser
+ * map and is safe to render straight from a server page.
+ */
+export function ListSearch({ placeholder }: { placeholder: string }) {
+ const [{ q }, setState] = useQueryStates(searchParsers);
+
+ // Back to page 1: staying on page 7 of a result set that now has two pages
+ // shows an empty table.
+ const [value, setValue] = useSearchInput(q, (next) =>
+ setState({ q: next, page: 1 }),
+ );
+
+ return (
+ <InputGroup className="w-full sm:w-64">
+ <InputGroupAddon>
+ <Search />
+ </InputGroupAddon>
+ <InputGroupInput
+ placeholder={placeholder}
+ value={value}
+ onChange={(event) => setValue(event.target.value)}
+ autoComplete="off"
+ />
+ </InputGroup>
+ );
+}
diff --git a/apps/app/components/data-table/use-table-query.ts b/apps/app/components/data-table/use-table-query.ts
index 2cabce4e..67db4af0 100644
--- a/apps/app/components/data-table/use-table-query.ts
+++ b/apps/app/components/data-table/use-table-query.ts
@@ -2,7 +2,6 @@
import type { TableQueryState } from "@crm/ui/lib/table-query";
import { useQueryStates } from "nuqs";
-import { useDeferredValue } from "react";
import type {
ListInput,
ListSearchParams,
@@ -21,6 +20,11 @@ export type TableQuery<TKey extends string> = {
*
* Lives in the app rather than in `@crm/ui` because it is routing state: the
* design system renders the controls, the app decides what a URL means.
+ *
+ * Nothing here is debounced or deferred. `q` reaches the URL only once typing
+ * has paused, because `ListSearch` holds the keystrokes in local state until
+ * then — so by the time this reads it, every value in the URL is one a user
+ * settled on and is worth a request.
*/
export function useTableQuery<TTab extends string, TFacet extends string>(
searchParams: ListSearchParams<TTab, TFacet>,
@@ -41,7 +45,6 @@ export function useTableQuery<TTab extends string, TFacet extends string>(
}
const query: TableQueryState = {
- q: values.q,
sort: values.sort,
dir: values.dir,
page,
@@ -51,7 +54,6 @@ export function useTableQuery<TTab extends string, TFacet extends string>(
filters,
// Every filter change resets to page 1: staying on page 7 of a result set
// that now has two pages shows an empty table.
- setSearch: (value) => setState((prev) => ({ ...prev, q: value, page: 1 })),
toggleSort: (id) =>
setState((prev) =>
prev.sort === id
@@ -69,11 +71,5 @@ export function useTableQuery<TTab extends string, TFacet extends string>(
setState((prev) => ({ ...prev, [id]: value, page: 1 })),
};
- // The search box is responsive because `q` updates on every keystroke; the
- // fetch does not need to be. Deferring the value the query is built from lets
- // a burst of typing collapse into a single request.
- const deferredQ = useDeferredValue(values.q);
- const input = toInput({ ...values, q: deferredQ });
-
- return { query, input };
+ return { query, input: toInput(values) };
}
diff --git a/packages/ui/src/components/data-table.tsx b/packages/ui/src/components/data-table.tsx
index 2699233e..68409136 100644
--- a/packages/ui/src/components/data-table.tsx
+++ b/packages/ui/src/components/data-table.tsx
@@ -7,7 +7,6 @@ import ChevronDown from "@carbon/icons-react/es/ChevronDown";
import ChevronRight from "@carbon/icons-react/es/ChevronRight";
import Column from "@carbon/icons-react/es/Column";
import Filter from "@carbon/icons-react/es/Filter";
-import Search from "@carbon/icons-react/es/Search";
import { Button } from "@crm/ui/components/button";
import {
DropdownMenu,
@@ -19,11 +18,6 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@crm/ui/components/dropdown-menu";
-import {
- InputGroup,
- InputGroupAddon,
- InputGroupInput,
-} from "@crm/ui/components/input-group";
import { Spinner } from "@crm/ui/components/spinner";
import { TablePagination } from "@crm/ui/components/table-pagination";
import {
@@ -91,7 +85,6 @@ export type DataTableProps<TRow, TSub> = {
total: number;
facetCounts?: Record<string, Record<string, number>>;
loading?: boolean;
- searchPlaceholder?: string;
facets?: DataTableFacet[];
tabs?: DataTableTabs;
onRowClick?: (row: TRow) => void;
@@ -161,7 +154,6 @@ export function DataTable<TRow, TSub = unknown>({
total,
facetCounts,
loading,
- searchPlaceholder = "Search…",
facets,
tabs,
onRowClick,
@@ -433,18 +425,6 @@ export function DataTable<TRow, TSub = unknown>({
{actions}
</div>
</div>
-
- <InputGroup>
- <InputGroupAddon>
- <Search />
- </InputGroupAddon>
- <InputGroupInput
- placeholder={searchPlaceholder}
- value={query.q}
- onChange={(event) => query.setSearch(event.target.value)}
- autoComplete="off"
- />
- </InputGroup>
</div>
<Table
diff --git a/packages/ui/src/hooks/use-search-input.ts b/packages/ui/src/hooks/use-search-input.ts
new file mode 100644
index 00000000..7aad6ce9
--- /dev/null
+++ b/packages/ui/src/hooks/use-search-input.ts
@@ -0,0 +1,47 @@
+"use client";
+
+import { useEffect, useRef, useState } from "react";
+
+/**
+ * A search box that types at keyboard speed and updates the URL afterwards.
+ *
+ * List state lives in the query string, so the obvious wiring — value from the
+ * URL, `onChange` writes it straight back — puts a router transition between
+ * the key press and the character appearing. Every keystroke re-renders the
+ * page and the table with it, which reads as a laggy input even though the
+ * fetch itself is already deferred.
+ *
+ * So the input is local state, and the URL is caught up once typing pauses.
+ * The committed value is still the single source of truth: anything that
+ * changes it elsewhere — the back button, a cleared filter, a reset link —
+ * flows back down and replaces what is in the box.
+ */
+export function useSearchInput(
+ committed: string,
+ commit: (value: string) => void,
+ delayMs = 250,
+) {
+ const [value, setValue] = useState(committed);
+
+ // Kept in a ref so the debounce effect depends on the typed value alone.
+ // `commit` is a fresh closure on every render, and depending on it directly
+ // would restart the timer each time and never fire.
+ const commitRef = useRef(commit);
+ commitRef.current = commit;
+
+ const committedRef = useRef(committed);
+ committedRef.current = committed;
+
+ // The URL changed without us: adopt it.
+ useEffect(() => {
+ setValue(committed);
+ }, [committed]);
+
+ useEffect(() => {
+ if (value === committedRef.current) return;
+ const timer = setTimeout(() => commitRef.current(value), delayMs);
+ return () => clearTimeout(timer);
+ }, [value, delayMs]);
+
+ return [value, setValue] as const;
+}
diff --git a/packages/ui/src/lib/table-query.ts b/packages/ui/src/lib/table-query.ts
index 645c80d0..e09ee102 100644
--- a/packages/ui/src/lib/table-query.ts
+++ b/packages/ui/src/lib/table-query.ts
@@ -7,9 +7,12 @@ export type SortDirection = "asc" | "desc";
* them. The implementation lives in the app (`useTableQuery`) because routing
* is not the design system's business — but the shape belongs next to the
* component that consumes it, so the two cannot drift.
+ *
+ * Search is not in here. It is the one list control that lives in the page
+ * header rather than the toolbar, so it reads `q` out of the URL itself and the
+ * table never sees it.
*/
export type TableQueryState = {
- q: string;
sort: string;
dir: SortDirection;
page: number;
@@ -19,7 +22,6 @@ export type TableQueryState = {
tabId?: string;
/** Facet id → selected value, `"all"` when unfiltered. */
filters: Record<string, string>;
- setSearch: (value: string) => void;
toggleSort: (id: string) => void;
setSort: (id: string) => void;
setDir: (dir: SortDirection) => void;