Skip to content

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

Address review feedback on the global search structure

François Zaninottod7747f818c71
diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.ts
index 683a6fa7..ce3b8bf7 100644
--- a/src/components/atomic-crm/providers/fakerest/dataProvider.ts
+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.ts
@@ -27,7 +27,7 @@ import { getActivityLog } from "../commons/activity";
import { getCompanyAvatar } from "../commons/getCompanyAvatar";
import { getContactAvatar } from "../commons/getContactAvatar";
import { mergeContacts } from "../commons/mergeContacts";
-import { getSearchResults } from "../commons/search";
+import { getSearchResults } from "./search";
import type { CrmDataProvider } from "../types";
import {
authProvider as defaultAuthProvider,
diff --git a/src/components/atomic-crm/providers/fakerest/internal/listParser.ts b/src/components/atomic-crm/providers/fakerest/internal/listParser.ts
index 26b53986..c2c3ca99 100644
--- a/src/components/atomic-crm/providers/fakerest/internal/listParser.ts
+++ b/src/components/atomic-crm/providers/fakerest/internal/listParser.ts
@@ -1,5 +1,5 @@
-export const UNQUOTED_ALLOWED_CHARS = "[A-Za-zÀ-ÖØ-öø-ÿ0-9_-]+";
-export const QUOTED_ALLOWED_CHARS = "[A-Za-zÀ-ÖØ-öø-ÿ0-9_, -]+";
+export const UNQUOTED_ALLOWED_CHARS = "[A-Za-zÀ-ÖØ-öø-ÿ0-9-]+";
+export const QUOTED_ALLOWED_CHARS = "[A-Za-zÀ-ÖØ-öø-ÿ0-9, -]+";
export const LIST_REGEX_BASE = `(${UNQUOTED_ALLOWED_CHARS}|"${QUOTED_ALLOWED_CHARS}")(,(${UNQUOTED_ALLOWED_CHARS}|"${QUOTED_ALLOWED_CHARS}"))*`;
/**
diff --git a/src/components/atomic-crm/providers/commons/search.ts b/src/components/atomic-crm/providers/fakerest/search.ts
similarity index 96%
rename from src/components/atomic-crm/providers/commons/search.ts
rename to src/components/atomic-crm/providers/fakerest/search.ts
index d0d0b4ce..2ef7d7d9 100644
--- a/src/components/atomic-crm/providers/commons/search.ts
+++ b/src/components/atomic-crm/providers/fakerest/search.ts
@@ -106,7 +106,6 @@ export async function getSearchResults(
return {
document: [company.name, content].join(" "),
result: {
- id: `company.${company.id}`,
resource: "companies",
record_id: company.id,
title: company.name,
@@ -129,7 +128,6 @@ export async function getSearchResults(
return {
document: [contact.first_name, contact.last_name, content].join(" "),
result: {
- id: `contact.${contact.id}`,
resource: "contacts",
record_id: contact.id,
title: contactName(contact),
@@ -147,7 +145,6 @@ export async function getSearchResults(
return {
document: [deal.name, content].join(" "),
result: {
- id: `deal.${deal.id}`,
resource: "deals",
record_id: deal.id,
title: deal.name,
@@ -167,7 +164,6 @@ export async function getSearchResults(
return {
document: content ?? "",
result: {
- id: `task.${task.id}`,
resource: "tasks",
record_id: task.id,
title: task.text?.slice(0, TITLE_LENGTH) ?? null,
@@ -185,7 +181,6 @@ export async function getSearchResults(
return {
document: content ?? "",
result: {
- id: `contactNote.${note.id}`,
resource: "contact_notes",
record_id: note.id,
title: note.text?.slice(0, TITLE_LENGTH) ?? null,
@@ -208,7 +203,6 @@ export async function getSearchResults(
return {
document: content ?? "",
result: {
- id: `dealNote.${note.id}`,
resource: "deal_notes",
record_id: note.id,
title: note.text?.slice(0, TITLE_LENGTH) ?? null,
diff --git a/src/components/atomic-crm/search/GlobalSearchDialog.tsx b/src/components/atomic-crm/search/GlobalSearchDialog.tsx
index c95d97a5..51825beb 100644
--- a/src/components/atomic-crm/search/GlobalSearchDialog.tsx
+++ b/src/components/atomic-crm/search/GlobalSearchDialog.tsx
@@ -3,14 +3,22 @@ import { useMemo, useState } from "react";
import { useNavigate } from "react-router";
import {
- CommandDialog,
+ Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
import { useIsMobile } from "@/hooks/use-mobile";
+import { cn } from "@/lib/utils";
import type { SearchResult } from "../types";
import { getSearchResultUrl } from "./getSearchResultUrl";
@@ -27,6 +35,10 @@ type LinkedResult = { result: SearchResult; url: string };
// Desktop: pinned near the top so the box grows downwards as results arrive
// instead of re-centering on every keystroke. Mobile: the whole screen.
const DESKTOP_DIALOG_CLASSES = "top-[12vh] translate-y-0 sm:max-w-xl";
+// Same look as shadcn's CommandDialog, which this composes from Dialog and
+// Command itself to control the layout and filtering.
+const COMMAND_CLASSES =
+ "[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5";
const MOBILE_DIALOG_CLASSES =
"inset-0 h-dvh max-w-none translate-x-0 translate-y-0 grid-rows-[minmax(0,1fr)] rounded-none border-0 sm:max-w-none";
@@ -90,109 +102,129 @@ export const GlobalSearchDialog = ({
const terms = useMemo(() => getSearchTerms(debouncedQuery), [debouncedQuery]);
return (
- <CommandDialog
- open={open}
- onOpenChange={handleOpenChange}
- title={translate("crm.search.title")}
- description={translate("crm.search.placeholder")}
- shouldFilter={false}
- className={isMobile ? MOBILE_DIALOG_CLASSES : DESKTOP_DIALOG_CLASSES}
- >
- <CommandInput
- placeholder={translate("crm.search.placeholder")}
- value={query}
- onValueChange={setQuery}
- // keep the text clear of the dialog's close button, drawn over the
- // input's right end
- className="pr-8"
- />
- <CommandList className={isMobile ? "max-h-none flex-1" : "max-h-[60vh]"}>
- {isTooShort ? (
- <div className="py-6 text-center text-sm text-muted-foreground">
- {translate("crm.search.hint", { min: MIN_SEARCH_LENGTH })}
- </div>
- ) : error ? (
- <div
- role="alert"
- className="py-6 text-center text-sm text-destructive"
- >
- {translate("crm.search.error")}
- </div>
- ) : isPending ? (
- <div className="py-6 text-center text-sm text-muted-foreground">
- {translate("crm.common.loading")}
- </div>
- ) : (
- <>
- <CommandEmpty>{translate("crm.search.empty")}</CommandEmpty>
- {SEARCH_RESOURCES.map(({ name, icon: Icon, labelKey }) => {
- const groupResults = linkedResults.filter(
- ({ result }) => result.resource === name,
- );
- if (groupResults.length === 0) {
- return null;
- }
- return (
- <CommandGroup key={name} heading={translate(labelKey)}>
- {groupResults.map(({ result, url }) => {
- const title =
- result.title || translate("crm.search.untitled_result");
- // A note's or task's title is the start of its text: when
- // the match is further in, show the text around it instead.
- const isTitleFromContent =
- !!result.content && result.content.startsWith(title);
- const snippet = result.content
- ? getSnippet(
- title,
- result.content,
- terms,
- isTitleFromContent ? TITLE_SNIPPET_RADIUS : undefined,
- )
- : null;
- const displayedTitle =
- isTitleFromContent && snippet ? snippet : title;
- const secondLine = isTitleFromContent ? null : snippet;
- return (
- <CommandItem
- key={result.id}
- value={String(result.id)}
- onSelect={() => handleSelect(url)}
- className="items-start"
- >
- <Icon className="mt-0.5 shrink-0" />
- <div className="flex min-w-0 flex-1 flex-col gap-0.5">
- <div className="flex items-baseline gap-2">
- <span
- className={
- isTitleFromContent ? "line-clamp-2" : "truncate"
- }
- >
- <Highlighted
- text={displayedTitle}
- terms={terms}
- />
- </span>
- {result.subtitle ? (
- <span className="ml-auto shrink-0 text-xs text-muted-foreground">
- {result.subtitle}
- </span>
- ) : null}
- </div>
- {secondLine ? (
- <span className="line-clamp-2 text-xs text-muted-foreground">
- <Highlighted text={secondLine} terms={terms} />
- </span>
- ) : null}
- </div>
- </CommandItem>
- );
- })}
- </CommandGroup>
- );
- })}
- </>
+ <Dialog open={open} onOpenChange={handleOpenChange}>
+ <DialogContent
+ className={cn(
+ "overflow-hidden p-0",
+ isMobile ? MOBILE_DIALOG_CLASSES : DESKTOP_DIALOG_CLASSES,
)}
- </CommandList>
- </CommandDialog>
+ >
+ {/* inside the content, so nothing renders while the dialog is closed */}
+ <DialogHeader className="sr-only">
+ <DialogTitle>{translate("crm.search.title")}</DialogTitle>
+ <DialogDescription>
+ {translate("crm.search.placeholder")}
+ </DialogDescription>
+ </DialogHeader>
+ {/* results come from the server, already filtered */}
+ <Command shouldFilter={false} className={COMMAND_CLASSES}>
+ <CommandInput
+ placeholder={translate("crm.search.placeholder")}
+ value={query}
+ onValueChange={setQuery}
+ // keep the text clear of the dialog's close button, drawn over the
+ // input's right end
+ className="pr-8"
+ />
+ <CommandList
+ className={isMobile ? "max-h-none flex-1" : "max-h-[60vh]"}
+ >
+ {isTooShort ? (
+ <div className="py-6 text-center text-sm text-muted-foreground">
+ {translate("crm.search.hint", { min: MIN_SEARCH_LENGTH })}
+ </div>
+ ) : error ? (
+ <div
+ role="alert"
+ className="py-6 text-center text-sm text-destructive"
+ >
+ {translate("crm.search.error")}
+ </div>
+ ) : isPending ? (
+ <div className="py-6 text-center text-sm text-muted-foreground">
+ {translate("crm.common.loading")}
+ </div>
+ ) : (
+ <>
+ <CommandEmpty>{translate("crm.search.empty")}</CommandEmpty>
+ {SEARCH_RESOURCES.map(({ name, icon: Icon, labelKey }) => {
+ const groupResults = linkedResults.filter(
+ ({ result }) => result.resource === name,
+ );
+ if (groupResults.length === 0) {
+ return null;
+ }
+ return (
+ <CommandGroup key={name} heading={translate(labelKey)}>
+ {groupResults.map(({ result, url }) => {
+ const title =
+ result.title ||
+ translate("crm.search.untitled_result");
+ // A note's or task's title is the start of its text: when
+ // the match is further in, show the text around it instead.
+ const isTitleFromContent =
+ !!result.content && result.content.startsWith(title);
+ const snippet = result.content
+ ? getSnippet(
+ title,
+ result.content,
+ terms,
+ isTitleFromContent
+ ? TITLE_SNIPPET_RADIUS
+ : undefined,
+ )
+ : null;
+ const displayedTitle =
+ isTitleFromContent && snippet ? snippet : title;
+ const secondLine = isTitleFromContent ? null : snippet;
+ return (
+ <CommandItem
+ key={`${result.resource}.${result.record_id}`}
+ value={`${result.resource}.${result.record_id}`}
+ onSelect={() => handleSelect(url)}
+ className="items-start"
+ >
+ <Icon className="mt-0.5 shrink-0" />
+ <div className="flex min-w-0 flex-1 flex-col gap-0.5">
+ <div className="flex items-baseline gap-2">
+ <span
+ className={
+ isTitleFromContent
+ ? "line-clamp-2"
+ : "truncate"
+ }
+ >
+ <Highlighted
+ text={displayedTitle}
+ terms={terms}
+ />
+ </span>
+ {result.subtitle ? (
+ <span className="ml-auto shrink-0 text-xs text-muted-foreground">
+ {result.subtitle}
+ </span>
+ ) : null}
+ </div>
+ {secondLine ? (
+ <span className="line-clamp-2 text-xs text-muted-foreground">
+ <Highlighted
+ text={secondLine}
+ terms={terms}
+ />
+ </span>
+ ) : null}
+ </div>
+ </CommandItem>
+ );
+ })}
+ </CommandGroup>
+ );
+ })}
+ </>
+ )}
+ </CommandList>
+ </Command>
+ </DialogContent>
+ </Dialog>
);
};
diff --git a/src/components/atomic-crm/types.ts b/src/components/atomic-crm/types.ts
index 21782854..0f3ac2fc 100644
--- a/src/components/atomic-crm/types.ts
+++ b/src/components/atomic-crm/types.ts
@@ -240,7 +240,6 @@ export type SearchResourceName =
| "deal_notes";
export type SearchResult = {
- id: Identifier;
resource: SearchResourceName;
record_id: Identifier;
title: string | null;
diff --git a/src/components/ui/command.tsx b/src/components/ui/command.tsx
index 4131280d..5360f6db 100644
--- a/src/components/ui/command.tsx
+++ b/src/components/ui/command.tsx
@@ -30,27 +30,20 @@ function Command({
function CommandDialog({
title = "Command Palette",
description = "Search for a command to run...",
- shouldFilter,
- className,
children,
...props
}: React.ComponentProps<typeof Dialog> & {
title?: string
description?: string
- shouldFilter?: boolean
- className?: string
}) {
return (
<Dialog {...props}>
- <DialogContent className={cn("overflow-hidden p-0", className)}>
- <DialogHeader className="sr-only">
- <DialogTitle>{title}</DialogTitle>
- <DialogDescription>{description}</DialogDescription>
- </DialogHeader>
- <Command
- shouldFilter={shouldFilter}
- className="[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5"
- >
+ <DialogHeader className="sr-only">
+ <DialogTitle>{title}</DialogTitle>
+ <DialogDescription>{description}</DialogDescription>
+ </DialogHeader>
+ <DialogContent className="overflow-hidden p-0">
+ <Command className="[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
{children}
</Command>
</DialogContent>
diff --git a/supabase/migrations/20261010054346_global_search.sql b/supabase/migrations/20261010054346_global_search.sql
index 168fab75..b4571727 100644
--- a/supabase/migrations/20261010054346_global_search.sql
+++ b/supabase/migrations/20261010054346_global_search.sql
@@ -120,7 +120,6 @@ create or replace function public.global_search(
resources text[] default null,
max_results integer default 50
) returns table (
- id text,
resource text,
record_id bigint,
title text,
@@ -137,14 +136,14 @@ declare
tsq tsquery := public.search_query(query);
branch_templates constant jsonb := jsonb_build_object(
'companies', $q$
- select 'company.' || c.id, 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at,
+ select 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at,
concat_ws(' · ', c.sector, c.description, c.website::text, c.phone_number, c.zipcode, c.city, c.state_abbr)
from public.companies c
where c.fts @@ %1$L::tsquery
order by c.created_at desc
limit %2$s $q$,
'contacts', $q$
- select 'contact.' || co.id, 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),
+ select 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),
(select cc.name from public.companies cc where cc.id = co.company_id), co.id, null::bigint, co.first_seen,
concat_ws(' · ', co.title, co.background,
(select string_agg(e ->> 'email', ' ') from jsonb_array_elements(co.email_jsonb) e),
@@ -154,7 +153,7 @@ declare
order by co.first_seen desc nulls last
limit %2$s $q$,
'deals', $q$
- select 'deal.' || d.id, 'deals', d.id, d.name,
+ select 'deals', d.id, d.name,
(select dc.name from public.companies dc where dc.id = d.company_id), null::bigint, d.id, d.created_at,
concat_ws(' · ', d.category, d.description)
from public.deals d
@@ -162,7 +161,7 @@ declare
order by d.created_at desc
limit %2$s $q$,
'tasks', $q$
- select 'task.' || t.id, 'tasks', t.id, left(t.text, 200),
+ select 'tasks', t.id, left(t.text, 200),
(select concat_ws(' ', tc.first_name, tc.last_name) from public.contacts tc where tc.id = t.contact_id),
t.contact_id, null::bigint, t.created_at,
concat_ws(' · ', t.text, t.type)
@@ -171,7 +170,7 @@ declare
order by t.created_at desc
limit %2$s $q$,
'contact_notes', $q$
- select 'contactNote.' || cn.id, 'contact_notes', cn.id, left(cn.text, 200),
+ select 'contact_notes', cn.id, left(cn.text, 200),
(select concat_ws(' ', nc.first_name, nc.last_name) from public.contacts nc where nc.id = cn.contact_id),
cn.contact_id, null::bigint, cn.date,
concat_ws(' · ', cn.text, public.search_attachment_titles(cn.attachments))
@@ -180,7 +179,7 @@ declare
order by cn.date desc nulls last
limit %2$s $q$,
'deal_notes', $q$
- select 'dealNote.' || dn.id, 'deal_notes', dn.id, left(dn.text, 200), nd.name, null::bigint, dn.deal_id, dn.date,
+ select 'deal_notes', dn.id, left(dn.text, 200), nd.name, null::bigint, dn.deal_id, dn.date,
concat_ws(' · ', dn.text, public.search_attachment_titles(dn.attachments))
from public.deal_notes dn
join public.deals nd on nd.id = dn.deal_id
@@ -204,7 +203,7 @@ begin
end if;
return query execute format(
- 'select * from (%s) results order by 8 desc nulls last limit %s',
+ 'select * from (%s) results order by 7 desc nulls last limit %s',
array_to_string(branches, ' union all '),
max_results
);
diff --git a/supabase/schemas/08_search.sql b/supabase/schemas/08_search.sql
index 9903e3c6..fe423d1c 100644
--- a/supabase/schemas/08_search.sql
+++ b/supabase/schemas/08_search.sql
@@ -107,7 +107,6 @@ create or replace function public.global_search(
resources text[] default null,
max_results integer default 50
) returns table (
- id text,
resource text,
record_id bigint,
title text,
@@ -124,14 +123,14 @@ declare
tsq tsquery := public.search_query(query);
branch_templates constant jsonb := jsonb_build_object(
'companies', $q$
- select 'company.' || c.id, 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at,
+ select 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at,
concat_ws(' · ', c.sector, c.description, c.website::text, c.phone_number, c.zipcode, c.city, c.state_abbr)
from public.companies c
where c.fts @@ %1$L::tsquery
order by c.created_at desc
limit %2$s $q$,
'contacts', $q$
- select 'contact.' || co.id, 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),
+ select 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),
(select cc.name from public.companies cc where cc.id = co.company_id), co.id, null::bigint, co.first_seen,
concat_ws(' · ', co.title, co.background,
(select string_agg(e ->> 'email', ' ') from jsonb_array_elements(co.email_jsonb) e),
@@ -141,7 +140,7 @@ declare
order by co.first_seen desc nulls last
limit %2$s $q$,
'deals', $q$
- select 'deal.' || d.id, 'deals', d.id, d.name,
+ select 'deals', d.id, d.name,
(select dc.name from public.companies dc where dc.id = d.company_id), null::bigint, d.id, d.created_at,
concat_ws(' · ', d.category, d.description)
from public.deals d
@@ -149,7 +148,7 @@ declare
order by d.created_at desc
limit %2$s $q$,
'tasks', $q$
- select 'task.' || t.id, 'tasks', t.id, left(t.text, 200),
+ select 'tasks', t.id, left(t.text, 200),
(select concat_ws(' ', tc.first_name, tc.last_name) from public.contacts tc where tc.id = t.contact_id),
t.contact_id, null::bigint, t.created_at,
concat_ws(' · ', t.text, t.type)
@@ -158,7 +157,7 @@ declare
order by t.created_at desc
limit %2$s $q$,
'contact_notes', $q$
- select 'contactNote.' || cn.id, 'contact_notes', cn.id, left(cn.text, 200),
+ select 'contact_notes', cn.id, left(cn.text, 200),
(select concat_ws(' ', nc.first_name, nc.last_name) from public.contacts nc where nc.id = cn.contact_id),
cn.contact_id, null::bigint, cn.date,
concat_ws(' · ', cn.text, public.search_attachment_titles(cn.attachments))
@@ -167,7 +166,7 @@ declare
order by cn.date desc nulls last
limit %2$s $q$,
'deal_notes', $q$
- select 'dealNote.' || dn.id, 'deal_notes', dn.id, left(dn.text, 200), nd.name, null::bigint, dn.deal_id, dn.date,
+ select 'deal_notes', dn.id, left(dn.text, 200), nd.name, null::bigint, dn.deal_id, dn.date,
concat_ws(' · ', dn.text, public.search_attachment_titles(dn.attachments))
from public.deal_notes dn
join public.deals nd on nd.id = dn.deal_id
@@ -191,7 +190,7 @@ begin
end if;
return query execute format(
- 'select * from (%s) results order by 8 desc nulls last limit %s',
+ 'select * from (%s) results order by 7 desc nulls last limit %s',
array_to_string(branches, ' union all '),
max_results
);