@salesforce/ui-bundle-template-app-react-sample-b2e 11.20.0 → 11.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/dist/CHANGELOG.md +8 -0
  2. package/dist/force-app/main/default/uiBundles/propertymanagementapp/package.json +4 -4
  3. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/applications/applicationSearchService.ts +25 -43
  4. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceRequests/maintenanceRequestSearchService.ts +31 -65
  5. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceWorkers/maintenanceWorkerSearchService.ts +21 -43
  6. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/properties/propertySearchService.ts +33 -53
  7. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/applications/ApplicationCard.tsx +80 -0
  8. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/dashboard/GlobalSearchBar.tsx +2 -2
  9. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/layout/VerticalNav.tsx +28 -18
  10. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/maintenanceRequests/MaintenanceRequestCard.tsx +79 -52
  11. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/maintenanceWorkers/WorkerCard.tsx +87 -0
  12. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/search/UnifiedSearch.tsx +213 -0
  13. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/api/distinctValuesService.ts +84 -0
  14. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/api/searchService.ts +71 -0
  15. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/MergedSearchResults.tsx +263 -0
  16. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/Search.tsx +185 -0
  17. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/SearchResults.tsx +77 -0
  18. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/SourceSection.tsx +166 -0
  19. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/controls/PaginationControls.tsx +185 -0
  20. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/controls/ScopeSelector.tsx +55 -0
  21. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/controls/SearchBar.tsx +55 -0
  22. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/controls/SortControl.tsx +62 -0
  23. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/ActiveFilters.tsx +61 -0
  24. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/DefaultFilterPanel.tsx +125 -0
  25. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/{object-search/components → search/components/filters}/FilterContext.tsx +7 -20
  26. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/BooleanFilter.tsx +50 -0
  27. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/DateRangeFilter.tsx +160 -0
  28. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/FilterFieldWrapper.tsx +26 -0
  29. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/MultiSelectFilter.tsx +88 -0
  30. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/NumericRangeFilter.tsx +78 -0
  31. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/SelectFilter.tsx +46 -0
  32. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/filters/inputs/TextFilter.tsx +52 -0
  33. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/components/results/DefaultResultRow.tsx +109 -0
  34. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/config.json +83 -0
  35. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/{object-search → search}/hooks/useAsyncData.ts +3 -10
  36. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/hooks/useDistinctValues.ts +59 -0
  37. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/hooks/useSearch.ts +703 -0
  38. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/index.ts +90 -0
  39. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/loadConfig.ts +14 -0
  40. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/queryBuilder.ts +156 -0
  41. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/types.ts +307 -0
  42. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/utils/debounce.ts +17 -0
  43. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/utils/fieldUtils.ts +14 -0
  44. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/utils/filterUtils.ts +285 -0
  45. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/search/utils/sortUtils.ts +34 -0
  46. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/lib/constants.ts +16 -29
  47. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/lib/routeConfig.ts +1 -0
  48. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/lib/searchConfig.ts +190 -0
  49. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/pages/ApplicationSearch.tsx +8 -385
  50. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/pages/Home.tsx +4 -2
  51. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/pages/MaintenanceRequestSearch.tsx +8 -458
  52. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/pages/MaintenanceWorkerSearch.tsx +8 -341
  53. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/pages/PropertySearch.tsx +7 -243
  54. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/pages/Search.tsx +16 -0
  55. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/routes.tsx +6 -0
  56. package/dist/force-app/main/default/uiBundles/propertymanagementapp/tsconfig.tsbuildinfo +1 -1
  57. package/dist/package-lock.json +2 -2
  58. package/dist/package.json +1 -1
  59. package/package.json +2 -2
  60. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/applications/query/distinctApplicationStatus.graphql +0 -19
  61. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/applications/query/searchApplications.graphql +0 -65
  62. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceRequests/query/distinctMaintenanceRequestPriority.graphql +0 -19
  63. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceRequests/query/distinctMaintenanceRequestStatus.graphql +0 -19
  64. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceRequests/query/distinctMaintenanceRequestType.graphql +0 -19
  65. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceRequests/query/searchMaintenanceRequests.graphql +0 -82
  66. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceWorkers/query/distinctMaintenanceWorkerEmploymentType.graphql +0 -19
  67. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/maintenanceWorkers/query/searchMaintenanceWorkers.graphql +0 -64
  68. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/properties/query/distinctPropertyStatus.graphql +0 -19
  69. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/properties/query/distinctPropertyType.graphql +0 -19
  70. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/api/properties/query/searchProperties.graphql +0 -105
  71. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/shared/ObjectSearchErrorState.tsx +0 -45
  72. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/components/shared/UserAvatar.tsx +0 -35
  73. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/api/accountSearchService.ts +0 -46
  74. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/api/query/distinctAccountIndustries.graphql +0 -19
  75. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/api/query/distinctAccountTypes.graphql +0 -19
  76. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/api/query/getAccountDetail.graphql +0 -121
  77. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/api/query/searchAccounts.graphql +0 -51
  78. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/pages/AccountObjectDetailPage.tsx +0 -355
  79. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/pages/AccountSearch.tsx +0 -304
  80. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/__examples__/pages/Home.tsx +0 -34
  81. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/api/objectSearchService.ts +0 -82
  82. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/ActiveFilters.tsx +0 -89
  83. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/ObjectBreadcrumb.tsx +0 -66
  84. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/PaginationControls.tsx +0 -109
  85. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/SearchBar.tsx +0 -41
  86. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/SortControl.tsx +0 -143
  87. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/BooleanFilter.tsx +0 -78
  88. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/DateFilter.tsx +0 -128
  89. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/DateRangeFilter.tsx +0 -70
  90. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/FilterFieldWrapper.tsx +0 -43
  91. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/MultiSelectFilter.tsx +0 -105
  92. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/NumericRangeFilter.tsx +0 -171
  93. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/SearchFilter.tsx +0 -52
  94. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/SelectFilter.tsx +0 -97
  95. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/components/filters/TextFilter.tsx +0 -93
  96. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/hooks/useDebouncedCallback.ts +0 -34
  97. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/hooks/useObjectSearchParams.ts +0 -254
  98. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/utils/debounce.ts +0 -25
  99. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/utils/fieldUtils.ts +0 -29
  100. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/utils/filterUtils.ts +0 -404
  101. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/features/object-search/utils/sortUtils.ts +0 -38
  102. package/dist/force-app/main/default/uiBundles/propertymanagementapp/src/lib/filterUtils.ts +0 -11
@@ -0,0 +1,263 @@
1
+ /**
2
+ * Merged results layout — a single combined grid across every in-scope source,
3
+ * driven by one global pager over the cumulative result set.
4
+ *
5
+ * This is the counterpart to {@link SearchResults} (which renders one stacked
6
+ * section per source). Use it when you want a unified "search everything" grid:
7
+ * heterogeneous cards laid out together, one shared pagination control, and a
8
+ * single results count — the layout the drop-in `<Search>` renders when the
9
+ * config sets `pagination.mode: "merged"`, and the same shape most apps
10
+ * hand-build on top of `useSearch`.
11
+ *
12
+ * It reads three aggregate accessors off the {@link SearchHandle}
13
+ * (`mergedResults`, `globalPagination`, `inScopeSources`), so it honours the
14
+ * config's `pagination.mode` / `mergeOrder` automatically. For a true combined
15
+ * grid, pair it with `pagination.mode: "merged"` (see the feature README, §9).
16
+ *
17
+ * Layout, top to bottom:
18
+ * - **Single-source filter chrome** — when the scope is narrowed to one
19
+ * source (selected or `lockedScope`), that source's filter panel + sort +
20
+ * active-filter chips render here. Under "All", per-source filters can't be
21
+ * combined across heterogeneous objects, so this row is omitted.
22
+ * - **Results count** — the cumulative in-scope total, on its own row directly
23
+ * above the grid, in every scope. Hidden when there are no results.
24
+ * - **The grid** — one card per `mergedResults` item, rendered via the
25
+ * per-source `renderResult[sourceKey]` callback. In a mixed grid (2+ sources
26
+ * in scope) each card is topped with an auto source badge — the source's
27
+ * `labelSingular` (falling back to `label`), colored from a palette by its
28
+ * config order — so heterogeneous results stay self-identifying with no
29
+ * configuration. Skeletons while loading; an empty-state message when there
30
+ * are none.
31
+ * - **Global pager** — one {@link PaginationControls} over the cumulative set.
32
+ */
33
+
34
+ import type { ReactNode } from "react";
35
+ import { AlertCircle } from "lucide-react";
36
+ import { Alert, AlertDescription, AlertTitle } from "../../../components/ui/alert";
37
+ import { Skeleton } from "../../../components/ui/skeleton";
38
+ import { ActiveFilters } from "./filters/ActiveFilters";
39
+ import { DefaultFilterPanel } from "./filters/DefaultFilterPanel";
40
+ import { FilterProvider, FilterResetButton } from "./filters/FilterContext";
41
+ import { PaginationControls } from "./controls/PaginationControls";
42
+ import { SortControl } from "./controls/SortControl";
43
+ import { DefaultResultRow } from "./results/DefaultResultRow";
44
+ import { ALL_SCOPE, type SearchHandle, type SourceController } from "../types";
45
+
46
+ type ResultRenderer = ((node: unknown) => ReactNode) | false;
47
+
48
+ export interface MergedSearchResultsProps {
49
+ handle: SearchHandle;
50
+ /**
51
+ * Per source key: how to render one node in the merged grid. Falls back to
52
+ * the built-in {@link DefaultResultRow} for any source without an entry.
53
+ * Pass `false` to skip a source's cards entirely.
54
+ */
55
+ renderResult?: Record<string, ResultRenderer>;
56
+ /** Message shown when there are no results. Defaults to "No results found." */
57
+ emptyMessage?: string;
58
+ /** Number of skeleton placeholders to show while loading. Defaults to `pageSize`. */
59
+ skeletonCount?: number;
60
+ /** Extra classes on the grid container. Defaults to a responsive 1/2/3-column grid. */
61
+ gridClassName?: string;
62
+ /** Extra classes on the outer wrapper. */
63
+ className?: string;
64
+ }
65
+
66
+ const DEFAULT_GRID_CLASS = "grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3";
67
+
68
+ /**
69
+ * Palette for the auto source badges in a mixed grid. A source's color is its
70
+ * index in the config's `sources` array, wrapped around the palette — so badges
71
+ * stay stable and distinct without any per-source configuration. Add more
72
+ * entries here if you routinely mix more source types than there are colors.
73
+ */
74
+ const BADGE_PALETTE = [
75
+ "bg-blue-100 text-blue-700",
76
+ "bg-amber-100 text-amber-700",
77
+ "bg-purple-100 text-purple-700",
78
+ "bg-pink-100 text-pink-700",
79
+ "bg-emerald-100 text-emerald-700",
80
+ "bg-sky-100 text-sky-700",
81
+ ] as const;
82
+
83
+ export function MergedSearchResults({
84
+ handle,
85
+ renderResult,
86
+ emptyMessage,
87
+ skeletonCount,
88
+ gridClassName = DEFAULT_GRID_CLASS,
89
+ className,
90
+ }: MergedSearchResultsProps) {
91
+ const { globalPagination: page, mergedResults, loading, error } = handle;
92
+
93
+ // A single source is in scope when the scope is narrowed (selected from the
94
+ // dropdown) or locked (`lockedScope` / restrictTo). Its filter chrome can
95
+ // render meaningfully; under "All" it can't (heterogeneous sources).
96
+ const singleSourceInScope = handle.scope !== ALL_SCOPE;
97
+ const soleController = singleSourceInScope ? handle.inScopeSources[0] : undefined;
98
+ // Per-card source badges only make sense when the grid is genuinely mixed —
99
+ // a single-source grid (narrowed or locked) has one type throughout.
100
+ const showSourceBadges = !singleSourceInScope;
101
+ // Assign each source a stable badge color by its declaration order. Badges
102
+ // only show in "All" scope, where `inScopeSources` is every source in config
103
+ // order, so the index here is the source's config position.
104
+ const badgeColorByKey: Record<string, string> = {};
105
+ handle.inScopeSources.forEach((controller, i) => {
106
+ badgeColorByKey[controller.config.key] = BADGE_PALETTE[i % BADGE_PALETTE.length];
107
+ });
108
+
109
+ const skeletons = skeletonCount ?? page.pageSize;
110
+
111
+ return (
112
+ <div className={`space-y-6 ${className ?? ""}`}>
113
+ {soleController && <SingleSourceChrome controller={soleController} />}
114
+
115
+ {error && (
116
+ <Alert variant="destructive" role="alert">
117
+ <AlertCircle />
118
+ <AlertTitle>Search failed</AlertTitle>
119
+ <AlertDescription>{error}</AlertDescription>
120
+ </Alert>
121
+ )}
122
+
123
+ {/* Results count — its own row, directly above the grid, in every scope.
124
+ Hidden when there are none (the empty state covers that case). */}
125
+ {!loading && !error && page.totalCount > 0 && (
126
+ <p className="text-sm text-muted-foreground" role="status">
127
+ {page.totalCount} result{page.totalCount === 1 ? "" : "s"}
128
+ </p>
129
+ )}
130
+
131
+ {loading ? (
132
+ <div className={gridClassName}>
133
+ {Array.from({ length: Math.min(skeletons, 6) }, (_, i) => (
134
+ <Skeleton key={i} className="h-48 w-full rounded-lg" />
135
+ ))}
136
+ </div>
137
+ ) : mergedResults.length === 0 ? (
138
+ <p className="py-12 text-center text-sm text-muted-foreground">
139
+ {emptyMessage ?? "No results found."}
140
+ </p>
141
+ ) : (
142
+ <div className={gridClassName}>
143
+ {mergedResults.map(({ sourceKey, node }, i) => {
144
+ const override = renderResult?.[sourceKey];
145
+ if (override === false) return null;
146
+ const source = handle.sources[sourceKey]?.config;
147
+ const rendered = override ? (
148
+ override(node)
149
+ ) : source ? (
150
+ <DefaultResultRow node={node} source={source} />
151
+ ) : null;
152
+ const badge =
153
+ showSourceBadges && source ? (
154
+ <SourceBadge
155
+ label={source.labelSingular ?? source.label}
156
+ colorClass={badgeColorByKey[sourceKey]}
157
+ />
158
+ ) : null;
159
+ return (
160
+ <div
161
+ key={getItemKey(sourceKey, node, i)}
162
+ className="flex flex-col gap-1.5 [&>:last-child]:min-h-0 [&>:last-child]:flex-1"
163
+ >
164
+ {badge}
165
+ {rendered}
166
+ </div>
167
+ );
168
+ })}
169
+ </div>
170
+ )}
171
+
172
+ {page.totalCount > 0 && (
173
+ <PaginationControls
174
+ pageIndex={page.pageIndex}
175
+ pageCount={page.pageCount}
176
+ hasNextPage={page.hasNextPage}
177
+ hasPreviousPage={page.hasPreviousPage}
178
+ pageSize={page.pageSize}
179
+ pageSizeOptions={page.pageSizeOptions}
180
+ onNextPage={page.goToNextPage}
181
+ onPreviousPage={page.goToPreviousPage}
182
+ onGoToPage={page.goToPage}
183
+ onPageSizeChange={page.setPageSize}
184
+ disabled={loading || !!error}
185
+ />
186
+ )}
187
+ </div>
188
+ );
189
+ }
190
+
191
+ function getItemKey(sourceKey: string, node: unknown, fallback: number): string {
192
+ const id = node && typeof node === "object" ? (node as { Id?: unknown }).Id : undefined;
193
+ return typeof id === "string" ? `${sourceKey}:${id}` : `${sourceKey}:${fallback}`;
194
+ }
195
+
196
+ interface SourceBadgeProps {
197
+ label: string;
198
+ colorClass: string;
199
+ }
200
+
201
+ /**
202
+ * Auto source badge shown above each card in a mixed grid — the source's
203
+ * `labelSingular` (falling back to `label`), colored from {@link BADGE_PALETTE}
204
+ * by its config order. Identifies which object type a heterogeneous card came
205
+ * from with no per-app config.
206
+ */
207
+ function SourceBadge({ label, colorClass }: SourceBadgeProps) {
208
+ return (
209
+ <span className={`w-fit rounded-full px-2.5 py-0.5 text-xs font-medium ${colorClass}`}>
210
+ {label}
211
+ </span>
212
+ );
213
+ }
214
+
215
+ interface SingleSourceChromeProps {
216
+ controller: SourceController;
217
+ }
218
+
219
+ /**
220
+ * Filter panel + sort + active-filter chips for the one source in scope. Shown
221
+ * whenever the search is narrowed to a single object (selected or locked).
222
+ * Hidden under "All", where heterogeneous per-source filters can't be combined.
223
+ */
224
+ function SingleSourceChrome({ controller }: SingleSourceChromeProps) {
225
+ const { config: source, filters, sort } = controller;
226
+ const showFilterPanel = (source.filterBy?.length ?? 0) > 0;
227
+ const showSortControl = (source.sortBy?.length ?? 0) > 0;
228
+ const showActiveChips = filters.active.length > 0;
229
+
230
+ if (!showFilterPanel && !showSortControl && !showActiveChips) return null;
231
+
232
+ return (
233
+ <div className="flex flex-col gap-4">
234
+ {showFilterPanel && (
235
+ <FilterProvider
236
+ filters={filters.active}
237
+ onFilterChange={filters.set}
238
+ onFilterRemove={filters.remove}
239
+ onReset={() => filters.active.forEach((f) => filters.remove(f.field))}
240
+ >
241
+ <div className="rounded-md border p-3 space-y-3">
242
+ <div className="flex items-center justify-between">
243
+ <h3 className="text-sm font-semibold">Filters</h3>
244
+ <FilterResetButton size="sm" />
245
+ </div>
246
+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3">
247
+ <DefaultFilterPanel source={source} />
248
+ </div>
249
+ </div>
250
+ </FilterProvider>
251
+ )}
252
+
253
+ {(showSortControl || showActiveChips) && (
254
+ <div className="flex flex-wrap items-center gap-2">
255
+ {showSortControl && source.sortBy && (
256
+ <SortControl configs={source.sortBy} sort={sort.current} onSortChange={sort.set} />
257
+ )}
258
+ {showActiveChips && <ActiveFilters filters={filters.active} onRemove={filters.remove} />}
259
+ </div>
260
+ )}
261
+ </div>
262
+ );
263
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Drop-in search component. Given a `SearchConfig`, renders a complete
3
+ * search experience: search bar, an aggregate result count, and results.
4
+ *
5
+ * The results layout follows the config's pagination mode — no separate prop:
6
+ * - `pagination.mode: "per-source"` (default): one stacked section per
7
+ * source, each with its own filters, sort, and pagination.
8
+ * - `pagination.mode: "merged"`: a single combined grid across every in-scope
9
+ * source, driven by one global pager over the cumulative result set (with a
10
+ * results count and, under a single scope, that source's filter chrome).
11
+ *
12
+ * The simplest possible integration:
13
+ *
14
+ * ```tsx
15
+ * import { Search, config } from ".../features/search";
16
+ * export default function MySearchPage() {
17
+ * return <Search config={config} />;
18
+ * }
19
+ * ```
20
+ *
21
+ * Override slots:
22
+ * - `renderResult[sourceKey]` — replace the default row renderer for one
23
+ * source. Pass `false` to hide that source entirely.
24
+ * - `renderFilters[sourceKey]` — replace the default filter sidebar for
25
+ * one source. Pass `false` to suppress filters for that source.
26
+ * - `renderHeader` — replace the default header (title + subtitle).
27
+ * - `searchPlaceholder` — placeholder text for the search input.
28
+ * - `title`, `subtitle` — defaults passed to the built-in header.
29
+ */
30
+
31
+ import { useMemo, type ReactNode } from "react";
32
+ import { useSearch } from "../hooks/useSearch";
33
+ import { SearchBar } from "./controls/SearchBar";
34
+ import { ScopeSelector } from "./controls/ScopeSelector";
35
+ import { SearchResults } from "./SearchResults";
36
+ import { MergedSearchResults } from "./MergedSearchResults";
37
+ import { ALL_SCOPE } from "../types";
38
+ import type { SearchConfig, SearchHandle, SObjectSourceConfig } from "../types";
39
+
40
+ type ResultRenderer = ((node: unknown) => ReactNode) | false;
41
+ type FilterRenderer = (() => ReactNode) | false;
42
+
43
+ /**
44
+ * Locks the search to a single source. Reuses the same shared `config.json`
45
+ * but shows only results matching the given (kind, key) tuple.
46
+ *
47
+ * When set:
48
+ * - The scope dropdown is hidden.
49
+ * - URL `?scope=` is not written (the page route already implies the source).
50
+ * - Only the matching source is fetched and rendered.
51
+ */
52
+ export interface RestrictTo {
53
+ kind: SObjectSourceConfig["kind"];
54
+ key: string;
55
+ }
56
+
57
+ interface SearchProps {
58
+ config: SearchConfig;
59
+ renderResult?: Record<string, ResultRenderer>;
60
+ renderFilters?: Record<string, FilterRenderer>;
61
+ emptyMessages?: Record<string, string>;
62
+ searchPlaceholder?: string;
63
+ title?: string;
64
+ subtitle?: string;
65
+ /**
66
+ * Lock the search to a single source by `(kind, key)`. Hides the scope
67
+ * dropdown and forces results to that one source. Useful for embedding
68
+ * a single-object search inside a page where the source is implicit.
69
+ */
70
+ restrictTo?: RestrictTo;
71
+ /**
72
+ * Show the source-scope dropdown next to the search bar.
73
+ * Defaults to `true` when the config has 2+ sources, `false` otherwise
74
+ * (a one-source dropdown would be redundant). Always hidden when
75
+ * `restrictTo` is set.
76
+ */
77
+ showScopeSelector?: boolean;
78
+ /** Label for the "search everything" entry in the scope dropdown. */
79
+ allScopeLabel?: string;
80
+ /** Optional override for the entire header region. */
81
+ renderHeader?: (handle: SearchHandle) => ReactNode;
82
+ className?: string;
83
+ }
84
+
85
+ export function Search({
86
+ config,
87
+ renderResult,
88
+ renderFilters,
89
+ emptyMessages,
90
+ searchPlaceholder = "Search…",
91
+ title = "Search",
92
+ subtitle,
93
+ restrictTo,
94
+ showScopeSelector,
95
+ allScopeLabel,
96
+ renderHeader,
97
+ className,
98
+ }: SearchProps) {
99
+ // Resolve restrictTo to a lockedScope value, surfacing a clear error if
100
+ // the (kind, key) pair doesn't match any source. Memoize so it's
101
+ // stable across renders.
102
+ const lockedScope = useMemo<string | undefined>(() => {
103
+ if (!restrictTo) return undefined;
104
+ const match = config.sources.find(
105
+ (s) => s.kind === restrictTo.kind && s.key === restrictTo.key,
106
+ );
107
+ if (!match) {
108
+ console.warn(
109
+ `<Search restrictTo>: no source with kind="${restrictTo.kind}" and key="${restrictTo.key}" in config. Falling back to "all".`,
110
+ );
111
+ return undefined;
112
+ }
113
+ return match.key;
114
+ }, [restrictTo, config.sources]);
115
+
116
+ const handle = useSearch(config, { lockedScope });
117
+ // The results layout follows the pagination mode — a "merged" config means a
118
+ // single combined grid, "per-source" (default) means one section per source.
119
+ // There is no separate `layout` prop: the two would only ever be set in lockstep.
120
+ const merged = config.pagination?.mode === "merged";
121
+ const totalResults = Object.values(handle.sources).reduce(
122
+ (acc, controller) => acc + (controller.result?.totalCount ?? 0),
123
+ 0,
124
+ );
125
+ const shouldShowScope = !handle.scopeLocked && (showScopeSelector ?? config.sources.length >= 2);
126
+ // The aggregate count is only meaningful when results from more than one
127
+ // source are combined. With a single configured source, a locked scope, or
128
+ // a specific source picked in the dropdown, exactly one source is on screen
129
+ // and that source's own section already shows its count — so suppress the
130
+ // redundant total here.
131
+ const sourcesInScope = handle.scope === ALL_SCOPE ? config.sources.length : 1;
132
+ // In merged mode the results grid renders its own count row directly above the
133
+ // grid (in every scope), so the inline aggregate here would be redundant.
134
+ const showTotalResults = !merged && sourcesInScope >= 2;
135
+
136
+ return (
137
+ <div className={`max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-6 space-y-6 ${className ?? ""}`}>
138
+ {renderHeader ? (
139
+ renderHeader(handle)
140
+ ) : (
141
+ <header className="space-y-2">
142
+ <h1 className="text-2xl font-bold">{title}</h1>
143
+ {subtitle && <p className="text-sm text-muted-foreground">{subtitle}</p>}
144
+ </header>
145
+ )}
146
+
147
+ <div className="flex flex-wrap items-center gap-3">
148
+ <SearchBar value={handle.q} onChange={handle.setQ} placeholder={searchPlaceholder} />
149
+ {shouldShowScope && (
150
+ <ScopeSelector
151
+ config={config}
152
+ scope={handle.scope}
153
+ onScopeChange={handle.setScope}
154
+ allLabel={allScopeLabel}
155
+ />
156
+ )}
157
+ </div>
158
+
159
+ {merged ? (
160
+ <MergedSearchResults
161
+ handle={handle}
162
+ renderResult={renderResult}
163
+ emptyMessage={emptyMessages?.[handle.scope]}
164
+ />
165
+ ) : (
166
+ <>
167
+ {/* Aggregate count on its own row, directly above the results —
168
+ only in "All" scope, where the per-source sections don't each
169
+ carry a single meaningful total. */}
170
+ {showTotalResults && !handle.loading && (
171
+ <p className="text-sm text-muted-foreground" role="status">
172
+ {totalResults} total result{totalResults === 1 ? "" : "s"}
173
+ </p>
174
+ )}
175
+ <SearchResults
176
+ handle={handle}
177
+ renderResult={renderResult}
178
+ renderFilters={renderFilters}
179
+ emptyMessages={emptyMessages}
180
+ />
181
+ </>
182
+ )}
183
+ </div>
184
+ );
185
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Renders one SourceSection per configured source, in declaration order.
3
+ *
4
+ * Customization is optional — sensible defaults kick in for any source you
5
+ * don't override:
6
+ * - `renderResult[sourceKey]` — per-node renderer. Default: title from
7
+ * `displayFields[0]`, subtitle from the rest, optional `<Link>` driven
8
+ * by `routePattern`.
9
+ * - `renderFilters[sourceKey]` — sidebar filter UI. Default: one input per
10
+ * `filterBy` entry, with picklist options resolved from inline
11
+ * config or auto-fetched from the GraphQL aggregate API.
12
+ *
13
+ * Pass `false` for either entry to suppress the default for that source.
14
+ *
15
+ * Filter chrome (sidebar + active-filter chips + sort dropdown) is
16
+ * automatically hidden when `handle.scope === "all"` — per-source controls
17
+ * would be unreachable with every source on screen, and dangling chips
18
+ * would be confusing. Pre-existing filter / sort selections stay in state,
19
+ * so narrowing the scope brings them back unchanged.
20
+ */
21
+
22
+ import type { ReactNode } from "react";
23
+ import { SourceSection } from "./SourceSection";
24
+ import { DefaultResultRow } from "./results/DefaultResultRow";
25
+ import { DefaultFilterPanel } from "./filters/DefaultFilterPanel";
26
+ import { ALL_SCOPE, type SearchHandle } from "../types";
27
+
28
+ type ResultRenderer = ((node: unknown) => ReactNode) | false;
29
+ type FilterRenderer = (() => ReactNode) | false;
30
+
31
+ interface SearchResultsProps {
32
+ handle: SearchHandle;
33
+ renderResult?: Record<string, ResultRenderer>;
34
+ renderFilters?: Record<string, FilterRenderer>;
35
+ emptyMessages?: Record<string, string>;
36
+ }
37
+
38
+ export function SearchResults({
39
+ handle,
40
+ renderResult,
41
+ renderFilters,
42
+ emptyMessages,
43
+ }: SearchResultsProps) {
44
+ const filtersHidden = handle.scope === ALL_SCOPE;
45
+ return (
46
+ <div className="space-y-10">
47
+ {Object.entries(handle.sources).map(([key, controller]) => {
48
+ if (handle.scope !== ALL_SCOPE && handle.scope !== key) return null;
49
+ const overrideResult = renderResult?.[key];
50
+ if (overrideResult === false) return null;
51
+ const resolvedRenderResult: (node: unknown) => ReactNode =
52
+ overrideResult ?? ((node) => <DefaultResultRow node={node} source={controller.config} />);
53
+
54
+ const overrideFilters = renderFilters?.[key];
55
+ const hasFilterFields = (controller.config.filterBy?.length ?? 0) > 0;
56
+ const resolvedRenderFilters: (() => ReactNode) | undefined =
57
+ overrideFilters === false
58
+ ? undefined
59
+ : (overrideFilters ??
60
+ (hasFilterFields
61
+ ? () => <DefaultFilterPanel source={controller.config} />
62
+ : undefined));
63
+
64
+ return (
65
+ <SourceSection
66
+ key={key}
67
+ controller={controller}
68
+ renderResult={resolvedRenderResult}
69
+ renderFilters={resolvedRenderFilters}
70
+ hideFilterChrome={filtersHidden}
71
+ emptyMessage={emptyMessages?.[key]}
72
+ />
73
+ );
74
+ })}
75
+ </div>
76
+ );
77
+ }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Renders one source's slice of a search result: optional filter
3
+ * panel, sort dropdown, active-filter chips, the result list (delegated to
4
+ * the caller via `renderResult`), and pagination controls.
5
+ *
6
+ * The component is "smart" only about orchestration. The actual list-item UI
7
+ * is owned by the application via the `renderResult(node)` callback —
8
+ * search never decides how an Account or Contact card looks.
9
+ */
10
+
11
+ import type { ReactNode } from "react";
12
+ import { AlertCircle, SearchX } from "lucide-react";
13
+ import { Alert, AlertDescription, AlertTitle } from "../../../components/ui/alert";
14
+ import { Skeleton } from "../../../components/ui/skeleton";
15
+ import { ActiveFilters } from "./filters/ActiveFilters";
16
+ import { FilterProvider, FilterResetButton } from "./filters/FilterContext";
17
+ import { PaginationControls } from "./controls/PaginationControls";
18
+ import { SortControl } from "./controls/SortControl";
19
+ import type { SourceController } from "../types";
20
+
21
+ interface SourceSectionProps {
22
+ controller: SourceController;
23
+ renderResult: (node: unknown) => ReactNode;
24
+ /** Optional filter UI (TextFilter, SelectFilter, ...) rendered above the results. */
25
+ renderFilters?: () => ReactNode;
26
+ /**
27
+ * Suppress the filter panel, the active-filter chips, and the sort
28
+ * dropdown. Pre-existing filter / sort selections stay in state (and
29
+ * apply to the GraphQL query) so the caller can restore the chrome
30
+ * without losing the user's selections, but no chrome surface is rendered.
31
+ *
32
+ * Set by `<SearchResults>` when the current scope is "all" — per-source
33
+ * controls would be unreachable while every source is on screen, and
34
+ * dangling chips would be confusing.
35
+ */
36
+ hideFilterChrome?: boolean;
37
+ emptyMessage?: string;
38
+ className?: string;
39
+ }
40
+
41
+ export function SourceSection({
42
+ controller,
43
+ renderResult,
44
+ renderFilters,
45
+ hideFilterChrome,
46
+ emptyMessage,
47
+ className,
48
+ }: SourceSectionProps) {
49
+ const { config, result, loading, error, filters, sort, pagination } = controller;
50
+ const nodes = result?.nodes ?? [];
51
+ const totalCount = result?.totalCount;
52
+ const showResults = !loading && !error && nodes.length > 0;
53
+ const showEmpty = !loading && !error && nodes.length === 0;
54
+
55
+ const pageSizeOptions = pagination.pageSizeOptions ?? [pagination.pageSize];
56
+
57
+ const showFilterPanel = !!renderFilters && !hideFilterChrome;
58
+ const showActiveChips = !hideFilterChrome && filters.active.length > 0;
59
+ const showSortControl = !hideFilterChrome && (config.sortBy?.length ?? 0) > 0;
60
+
61
+ return (
62
+ <section className={`flex flex-col gap-6 ${className ?? ""}`}>
63
+ {showFilterPanel && (
64
+ <FilterProvider
65
+ filters={filters.active}
66
+ onFilterChange={filters.set}
67
+ onFilterRemove={filters.remove}
68
+ onReset={() => filters.active.forEach((f) => filters.remove(f.field))}
69
+ >
70
+ <div className="rounded-md border p-3 space-y-3">
71
+ <div className="flex items-center justify-between">
72
+ <h3 className="text-sm font-semibold">Filters</h3>
73
+ <FilterResetButton size="sm" />
74
+ </div>
75
+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3">
76
+ {renderFilters!()}
77
+ </div>
78
+ </div>
79
+ </FilterProvider>
80
+ )}
81
+
82
+ <div className="min-w-0">
83
+ <header className="flex flex-wrap items-baseline gap-3 mb-3">
84
+ <h2 className="text-lg font-semibold">{config.label}</h2>
85
+ </header>
86
+
87
+ {(showSortControl || showActiveChips) && (
88
+ <div className="flex flex-wrap items-center gap-2 mb-3">
89
+ {showSortControl && config.sortBy ? (
90
+ <SortControl configs={config.sortBy} sort={sort.current} onSortChange={sort.set} />
91
+ ) : null}
92
+ {showActiveChips && (
93
+ <ActiveFilters filters={filters.active} onRemove={filters.remove} />
94
+ )}
95
+ </div>
96
+ )}
97
+
98
+ {/* Result count, shown just above the results in every scope —
99
+ including a locked / single-source (restrictTo) view. */}
100
+ {!loading && !error && totalCount != null && (
101
+ <p className="text-sm text-muted-foreground mb-3">
102
+ {totalCount} {totalCount === 1 ? "result" : "results"}
103
+ </p>
104
+ )}
105
+
106
+ <div className="min-h-32">
107
+ {loading && (
108
+ <div className="space-y-2">
109
+ {Array.from({ length: Math.min(pagination.pageSize, 5) }, (_, i) => (
110
+ <Skeleton key={i} className="h-12 w-full" />
111
+ ))}
112
+ </div>
113
+ )}
114
+
115
+ {error && (
116
+ <Alert variant="destructive" role="alert">
117
+ <AlertCircle />
118
+ <AlertTitle>Failed to load {config.label}</AlertTitle>
119
+ <AlertDescription>{error}</AlertDescription>
120
+ </Alert>
121
+ )}
122
+
123
+ {showResults && (
124
+ <ul className="divide-y">
125
+ {nodes.map((node, i) => (
126
+ <li key={getNodeKey(node, config.idField, i)} className="py-3">
127
+ {renderResult(node)}
128
+ </li>
129
+ ))}
130
+ </ul>
131
+ )}
132
+
133
+ {showEmpty && (
134
+ <div className="flex flex-col items-center justify-center py-10 text-center">
135
+ <SearchX className="size-10 text-muted-foreground mb-3" />
136
+ <p className="text-sm text-muted-foreground">
137
+ {emptyMessage ?? `No ${config.label.toLowerCase()} found.`}
138
+ </p>
139
+ </div>
140
+ )}
141
+ </div>
142
+
143
+ <PaginationControls
144
+ pageIndex={pagination.pageIndex}
145
+ hasNextPage={pagination.hasNextPage}
146
+ hasPreviousPage={pagination.hasPreviousPage}
147
+ pageSize={pagination.pageSize}
148
+ pageSizeOptions={pageSizeOptions}
149
+ onNextPage={pagination.goToNextPage}
150
+ onPreviousPage={pagination.goToPreviousPage}
151
+ onPageSizeChange={pagination.setPageSize}
152
+ disabled={loading || !!error}
153
+ idPrefix={`page-size-${config.key}`}
154
+ />
155
+ </div>
156
+ </section>
157
+ );
158
+ }
159
+
160
+ function getNodeKey(node: unknown, idField: string | undefined, fallback: number): string | number {
161
+ if (node && typeof node === "object") {
162
+ const id = (node as Record<string, unknown>)[idField ?? "Id"];
163
+ if (typeof id === "string" || typeof id === "number") return id;
164
+ }
165
+ return fallback;
166
+ }