Skip to content

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

Show a few results per search group with a link to the rest

fzaninottob43fe460cd49
diff --git a/e2e/globalSearch.spec.ts b/e2e/globalSearch.spec.ts
index 40ae0bf2..d91ce6d1 100644
--- a/e2e/globalSearch.spec.ts
+++ b/e2e/globalSearch.spec.ts
@@ -38,7 +38,10 @@ test.describe("global search", () => {
await expect(contacts.getByRole("option")).toHaveCount(1);
await expect(contacts.getByRole("option")).toContainText("Zebulon Durand");
+ // 3 notes, then the rest of the 10 fetched once expanded
const notes = page.getByRole("group", { name: "Contact notes" });
+ await expect(notes.getByRole("option")).toHaveCount(4);
+ await notes.getByRole("option", { name: "Show more" }).click();
await expect(notes.getByRole("option")).toHaveCount(10);
});
});
diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
index 7d7319bf..506b904d 100644
--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
@@ -464,6 +464,7 @@ export const englishCrmMessages = {
empty: "No result found",
error: "Search is unavailable right now. Please try again.",
untitled_result: "(no title)",
+ show_more: "Show more",
groups: {
contacts: "Contacts",
companies: "Companies",
diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
index 37e5fc34..59f15f67 100644
--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
@@ -473,6 +473,7 @@ export const frenchCrmMessages = {
error:
"La recherche est indisponible pour le moment. Veuillez réessayer.",
untitled_result: "(sans titre)",
+ show_more: "Afficher plus",
groups: {
contacts: "Contacts",
companies: "Entreprises",
diff --git a/src/components/atomic-crm/search/GlobalSearchButton.test.tsx b/src/components/atomic-crm/search/GlobalSearchButton.test.tsx
index 14c87f21..7e9e57c8 100644
--- a/src/components/atomic-crm/search/GlobalSearchButton.test.tsx
+++ b/src/components/atomic-crm/search/GlobalSearchButton.test.tsx
@@ -361,6 +361,60 @@ describe("GlobalSearchDialog", () => {
});
});
+describe("GlobalSearchDialog with many results in a group", () => {
+ const manyCompanies = {
+ ...data,
+ companies: Array.from({ length: 5 }, (_, index) => ({
+ id: index + 1,
+ name: `Bakery ${index + 1}`,
+ sector: "Food",
+ created_at: `2025-01-0${index + 1}T10:00:00.000Z`,
+ })),
+ contact_notes: [
+ {
+ contact_id: 1,
+ date: "2025-02-01T10:00:00.000Z",
+ id: 1,
+ sales_id: 0,
+ status: "warm",
+ text: "Visited the bakery",
+ },
+ ],
+ };
+
+ beforeEach(async () => {
+ await page.viewport(1600, 900);
+ });
+
+ it("shows the first results of each group and a link to the rest", async () => {
+ const screen = await render(<Dialog data={manyCompanies} />);
+
+ await screen.getByPlaceholder(PLACEHOLDER).fill("bakery");
+
+ const companies = screen.getByRole("group", { name: "Companies" });
+ await expect.element(companies.getByText("Show more")).toBeVisible();
+ expect(companies.getByRole("option").elements()).toHaveLength(4);
+ await expect
+ .element(
+ screen
+ .getByRole("group", { name: "Contact notes" })
+ .getByText("Visited the bakery"),
+ )
+ .toBeVisible();
+ });
+
+ it("shows the rest of a group when asked", async () => {
+ const screen = await render(<Dialog data={manyCompanies} />);
+
+ await screen.getByPlaceholder(PLACEHOLDER).fill("bakery");
+ const companies = screen.getByRole("group", { name: "Companies" });
+ await companies.getByText("Show more").click();
+
+ await expect.element(companies.getByText("Bakery 1")).toBeVisible();
+ expect(companies.getByRole("option").elements()).toHaveLength(5);
+ });
+});
+
describe("GlobalSearchDialog on mobile", () => {
beforeEach(async () => {
await page.viewport(375, 667);
diff --git a/src/components/atomic-crm/search/GlobalSearchButton.tsx b/src/components/atomic-crm/search/GlobalSearchButton.tsx
index 178253ea..55e6fdf5 100644
--- a/src/components/atomic-crm/search/GlobalSearchButton.tsx
+++ b/src/components/atomic-crm/search/GlobalSearchButton.tsx
@@ -1,5 +1,5 @@
import { useQuery } from "@tanstack/react-query";
-import { Search } from "lucide-react";
+import { ChevronDown, Search } from "lucide-react";
import { useDataProvider, useDebouncedEvent, useTranslate } from "ra-core";
import { useEffect, useMemo, useState } from "react";
import { useNavigate } from "react-router";
@@ -125,6 +125,9 @@ export const GlobalSearchButton = ({
const MIN_SEARCH_LENGTH = 2;
const DEBOUNCE_MS = 300;
const MAX_RESULTS_PER_RESOURCE = 10;
+// Results shown per group until the user asks for more, so a busy group does
+// not push the others out of view.
+const GROUP_PREVIEW_SIZE = 3;
/**
* Run the global search for an already debounced search term.
@@ -194,12 +197,15 @@ export const GlobalSearchDialog = ({
const isMobile = useIsMobile();
const [query, setQuery] = useState("");
const [debouncedQuery, setDebouncedQuery] = useState("");
+ const [expandedGroups, setExpandedGroups] = useState<
+ ReadonlySet<SearchResourceName>
+ >(new Set());
const updateDebouncedQuery = useDebouncedEvent(
setDebouncedQuery,
DEBOUNCE_MS,
);
const { results, isPending, error } = useGlobalSearch(
- query,
+ debouncedQuery,
isMobile ? MOBILE_SEARCH_RESOURCES : DESKTOP_SEARCH_RESOURCES,
);
@@ -218,6 +224,7 @@ export const GlobalSearchDialog = ({
if (!nextOpen) {
setQuery("");
setDebouncedQuery("");
+ setExpandedGroups(new Set());
// replaces an update still pending from the last keystroke
updateDebouncedQuery("");
}
@@ -264,14 +271,25 @@ export const GlobalSearchDialog = ({
value={query}
onValueChange={(value) => {
setQuery(value);
+ setExpandedGroups(new Set());
updateDebouncedQuery(value.trim());
}}
// keep the text clear of the dialog's close button, drawn over the
// input's right end
- className="pr-8"
+ className="pr-8 text-base"
/>
<CommandList
- className={isMobile ? "max-h-none flex-1" : "max-h-[60vh]"}
+ className={cn(
+ "overscroll-contain",
+ isMobile ? "max-h-none min-h-0 flex-1" : "max-h-[60vh]",
+ )}
+ // On a phone the keyboard covers the bottom of the list, and
+ // dragging then pans the page behind the dialog: close it first.
+ onTouchStart={
+ isMobile
+ ? () => (document.activeElement as HTMLElement | null)?.blur()
+ : undefined
+ }
>
{isTooShort ? (
<div className="py-6 text-center text-sm text-muted-foreground">
@@ -298,9 +316,20 @@ export const GlobalSearchDialog = ({
if (groupResults.length === 0) {
return null;
}
+ const hiddenCount = expandedGroups.has(name)
+ ? 0
+ : Math.max(0, groupResults.length - GROUP_PREVIEW_SIZE);
+ const shownResults = groupResults.slice(
+ 0,
+ groupResults.length - hiddenCount,
+ );
return (
- <CommandGroup key={name} heading={translate(labelKey)}>
- {groupResults.map(({ result, url }) => {
+ <CommandGroup
+ key={name}
+ heading={translate(labelKey)}
+ className="[&_[cmdk-group-heading]]:text-sm"
+ >
+ {shownResults.map(({ result, url }) => {
const title =
result.title ||
translate("crm.search.untitled_result");
@@ -326,7 +355,7 @@ export const GlobalSearchDialog = ({
key={`${result.resource}.${result.record_id}`}
value={`${result.resource}.${result.record_id}`}
onSelect={() => handleSelect(url)}
- className="items-start"
+ className="items-start text-base"
>
<Icon className="mt-0.5 shrink-0" />
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
@@ -344,13 +373,13 @@ export const GlobalSearchDialog = ({
/>
</span>
{result.subtitle ? (
- <span className="ml-auto shrink-0 text-xs text-muted-foreground">
+ <span className="ml-auto shrink-0 text-sm text-muted-foreground">
{result.subtitle}
</span>
) : null}
</div>
{secondLine ? (
- <span className="line-clamp-2 text-xs text-muted-foreground">
+ <span className="line-clamp-2 text-sm text-muted-foreground">
<Highlighted
text={secondLine}
terms={terms}
@@ -361,6 +390,20 @@ export const GlobalSearchDialog = ({
</CommandItem>
);
})}
+ {hiddenCount > 0 ? (
+ <CommandItem
+ value={`${name}.show_more`}
+ onSelect={() =>
+ setExpandedGroups(
+ (groups) => new Set([...groups, name]),
+ )
+ }
+ className="text-sm text-muted-foreground"
+ >
+ <ChevronDown className="shrink-0" />
+ {translate("crm.search.show_more")}
+ </CommandItem>
+ ) : null}
</CommandGroup>
);
})}