@salesforce/webapp-template-app-react-template-b2e-experimental 1.109.5 → 1.109.6

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/webapplications/appreacttemplateb2e/package.json +5 -6
  3. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/api/graphql-operations-types.ts +11260 -0
  4. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/components/ui/sonner.tsx +20 -0
  5. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/api/accountSearchService.ts +46 -0
  6. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/api/query/distinctAccountIndustries.graphql +19 -0
  7. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/api/query/distinctAccountTypes.graphql +19 -0
  8. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/api/query/getAccountDetail.graphql +121 -0
  9. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/api/query/searchAccounts.graphql +51 -0
  10. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/pages/AccountObjectDetailPage.tsx +357 -0
  11. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/pages/AccountSearch.tsx +275 -0
  12. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/__examples__/pages/Home.tsx +34 -0
  13. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/api/objectSearchService.ts +84 -0
  14. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/ActiveFilters.tsx +89 -0
  15. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/FilterPanel.tsx +127 -0
  16. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/ObjectBreadcrumb.tsx +66 -0
  17. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/PaginationControls.tsx +151 -0
  18. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/SearchBar.tsx +41 -0
  19. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/SortControl.tsx +143 -0
  20. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/BooleanFilter.tsx +94 -0
  21. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/DateFilter.tsx +138 -0
  22. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/DateRangeFilter.tsx +78 -0
  23. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/MultiSelectFilter.tsx +106 -0
  24. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/NumericRangeFilter.tsx +102 -0
  25. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/SearchFilter.tsx +40 -0
  26. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/SelectFilter.tsx +97 -0
  27. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/components/filters/TextFilter.tsx +77 -0
  28. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/hooks/useAsyncData.ts +53 -0
  29. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/hooks/useCachedAsyncData.ts +183 -0
  30. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/hooks/useObjectSearchParams.ts +225 -0
  31. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/utils/debounce.ts +22 -0
  32. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/utils/fieldUtils.ts +29 -0
  33. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/utils/filterUtils.ts +372 -0
  34. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/object-search/utils/sortUtils.ts +38 -0
  35. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/index.ts +3 -117
  36. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/pages/Home.tsx +10 -11
  37. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/routes.tsx +8 -20
  38. package/dist/package-lock.json +2 -2
  39. package/dist/package.json +1 -1
  40. package/package.json +1 -1
  41. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/api/objectDetailService.ts +0 -102
  42. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/api/objectInfoGraphQLService.ts +0 -137
  43. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/api/objectInfoService.ts +0 -95
  44. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/api/recordListGraphQLService.ts +0 -364
  45. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/DetailFields.tsx +0 -55
  46. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/DetailForm.tsx +0 -146
  47. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/DetailHeader.tsx +0 -34
  48. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/DetailLayoutSections.tsx +0 -80
  49. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/Section.tsx +0 -108
  50. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/SectionRow.tsx +0 -20
  51. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/UiApiDetailForm.tsx +0 -140
  52. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/formatted/FieldValueDisplay.tsx +0 -73
  53. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/formatted/FormattedAddress.tsx +0 -29
  54. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/formatted/FormattedEmail.tsx +0 -17
  55. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/formatted/FormattedPhone.tsx +0 -24
  56. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/formatted/FormattedText.tsx +0 -11
  57. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/detail/formatted/FormattedUrl.tsx +0 -29
  58. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/filters/FilterField.tsx +0 -54
  59. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/filters/FilterInput.tsx +0 -55
  60. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/filters/FilterSelect.tsx +0 -72
  61. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/filters/FiltersPanel.tsx +0 -380
  62. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/forms/filters-form.tsx +0 -114
  63. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/forms/submit-button.tsx +0 -47
  64. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/search/GlobalSearchInput.tsx +0 -114
  65. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/search/ResultCardFields.tsx +0 -71
  66. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/search/SearchHeader.tsx +0 -31
  67. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/search/SearchPagination.tsx +0 -144
  68. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/search/SearchResultCard.tsx +0 -138
  69. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/search/SearchResultsPanel.tsx +0 -197
  70. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/components/shared/LoadingFallback.tsx +0 -61
  71. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/constants.ts +0 -39
  72. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/filters/FilterInput.tsx +0 -55
  73. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/filters/FilterSelect.tsx +0 -72
  74. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/hooks/form.tsx +0 -209
  75. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/hooks/useObjectInfoBatch.ts +0 -72
  76. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/hooks/useObjectSearchData.ts +0 -174
  77. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/hooks/useRecordDetailLayout.ts +0 -137
  78. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/hooks/useRecordListGraphQL.ts +0 -135
  79. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/pages/DetailPage.tsx +0 -109
  80. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/pages/GlobalSearch.tsx +0 -235
  81. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/types/filters/filters.ts +0 -121
  82. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/types/filters/picklist.ts +0 -6
  83. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/types/objectInfo/objectInfo.ts +0 -49
  84. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/types/recordDetail/recordDetail.ts +0 -61
  85. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/types/schema.d.ts +0 -200
  86. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/types/search/searchResults.ts +0 -229
  87. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/apiUtils.ts +0 -59
  88. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/cacheUtils.ts +0 -76
  89. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/debounce.ts +0 -90
  90. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/fieldUtils.ts +0 -354
  91. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/fieldValueExtractor.ts +0 -67
  92. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/filterUtils.ts +0 -32
  93. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/formDataTransformUtils.ts +0 -260
  94. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/formUtils.ts +0 -142
  95. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/graphQLNodeFieldUtils.ts +0 -186
  96. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/graphQLObjectInfoAdapter.ts +0 -77
  97. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/graphQLRecordAdapter.ts +0 -90
  98. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/layoutTransformUtils.ts +0 -236
  99. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/linkUtils.ts +0 -14
  100. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/paginationUtils.ts +0 -49
  101. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/recordUtils.ts +0 -159
  102. package/dist/force-app/main/default/webapplications/appreacttemplateb2e/src/features/global-search/utils/sanitizationUtils.ts +0 -50
@@ -0,0 +1,225 @@
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
+ import { useSearchParams } from "react-router";
3
+ import type { FilterFieldConfig, ActiveFilterValue } from "../utils/filterUtils";
4
+ import type { SortFieldConfig, SortState } from "../utils/sortUtils";
5
+ import { filtersToSearchParams, searchParamsToFilters, buildFilter } from "../utils/filterUtils";
6
+ import { buildOrderBy } from "../utils/sortUtils";
7
+ import { debounce } from "../utils/debounce";
8
+
9
+ /** How long to wait before flushing local state changes to the URL. */
10
+ const URL_SYNC_DEBOUNCE_MS = 300;
11
+
12
+ export interface PaginationConfig {
13
+ defaultPageSize: number;
14
+ validPageSizes: number[];
15
+ }
16
+
17
+ /**
18
+ * Manages filter, sort, and cursor-based pagination state for an object search page.
19
+ *
20
+ * ## State model
21
+ * Local React state is the primary driver for instant UI updates.
22
+ * URL search params act as the durable source of truth so that a page
23
+ * refresh or shared link restores the same view. Changes are synced to
24
+ * the URL via a debounced write (300 ms) to avoid excessive history entries.
25
+ *
26
+ * ## Return shape
27
+ * Returns memoized groups so each group's reference is stable unless its
28
+ * contents change — safe to pass directly as props to `React.memo` children.
29
+ *
30
+ * - `filters` — active filter values + set/remove callbacks
31
+ * - `sort` — current sort state + set callback
32
+ * - `query` — derived `where` / `orderBy` objects ready for the API
33
+ * - `pagination` — page size, page index, cursor, and navigation callbacks
34
+ * - `resetAll` — clears all filters, sort, and pagination in one call
35
+ */
36
+ export function useObjectSearchParams<TFilter, TOrderBy>(
37
+ filterConfigs: FilterFieldConfig[],
38
+ _sortConfigs?: SortFieldConfig[],
39
+ paginationConfig?: PaginationConfig,
40
+ ) {
41
+ const defaultPageSize = paginationConfig?.defaultPageSize ?? 10;
42
+ const validPageSizes = paginationConfig?.validPageSizes ?? [defaultPageSize];
43
+ const [searchParams, setSearchParams] = useSearchParams();
44
+
45
+ // Seed local state from URL on initial load
46
+ const initial = useMemo(
47
+ () => searchParamsToFilters(searchParams, filterConfigs),
48
+ // Only run on mount — local state takes over after that, no deps needed
49
+ // eslint-disable-next-line react-hooks/exhaustive-deps
50
+ [],
51
+ );
52
+
53
+ const [filters, setFilters] = useState<ActiveFilterValue[]>(initial.filters);
54
+ const [sort, setLocalSort] = useState<SortState | null>(initial.sort);
55
+
56
+ // Pagination — cursor-based with a stack to support "previous page" navigation.
57
+ const getValidPageSize = (size: number) =>
58
+ validPageSizes.includes(size) ? size : defaultPageSize;
59
+
60
+ const [pageSize, setPageSizeState] = useState<number>(
61
+ getValidPageSize(initial.pageSize ?? defaultPageSize),
62
+ );
63
+ const [pageIndex, setPageIndex] = useState(initial.pageIndex);
64
+ const [afterCursor, setAfterCursor] = useState<string | undefined>(undefined);
65
+ const cursorStackRef = useRef<string[]>([]);
66
+
67
+ // Debounced URL sync — keeps URL in sync without blocking the UI
68
+ const syncToUrl = useCallback(
69
+ (
70
+ nextFilters: ActiveFilterValue[],
71
+ nextSort: SortState | null,
72
+ nextPageSize?: number,
73
+ nextPageIndex?: number,
74
+ ) => {
75
+ const params = filtersToSearchParams(nextFilters, nextSort, nextPageSize, nextPageIndex);
76
+ setSearchParams(params, { replace: true });
77
+ },
78
+ [setSearchParams],
79
+ );
80
+
81
+ const debouncedSyncRef = useRef(debounce(syncToUrl, URL_SYNC_DEBOUNCE_MS));
82
+ useEffect(() => {
83
+ debouncedSyncRef.current = debounce(syncToUrl, URL_SYNC_DEBOUNCE_MS);
84
+ }, [syncToUrl]);
85
+
86
+ // Snapshot ref — lets callbacks read the latest state without being
87
+ // recreated on every render (avoids infinite useCallback chains).
88
+ const stateRef = useRef({ filters, sort, pageSize, pageIndex });
89
+ stateRef.current = { filters, sort, pageSize, pageIndex };
90
+
91
+ // Any filter/sort change resets pagination to the first page.
92
+ const resetPagination = useCallback(() => {
93
+ setPageIndex(0);
94
+ setAfterCursor(undefined);
95
+ cursorStackRef.current = [];
96
+ }, []);
97
+
98
+ // -- Filter callbacks -------------------------------------------------------
99
+
100
+ const setFilter = useCallback(
101
+ (field: string, value: ActiveFilterValue | undefined) => {
102
+ const { sort: s, pageSize: ps } = stateRef.current;
103
+ setFilters((prev) => {
104
+ const next = prev.filter((f) => f.field !== field);
105
+ if (value) next.push(value);
106
+ debouncedSyncRef.current(next, s, ps);
107
+ return next;
108
+ });
109
+ resetPagination();
110
+ },
111
+ [resetPagination],
112
+ );
113
+
114
+ const removeFilter = useCallback(
115
+ (field: string) => {
116
+ const { sort: s, pageSize: ps } = stateRef.current;
117
+ setFilters((prev) => {
118
+ const next = prev.filter((f) => f.field !== field);
119
+ debouncedSyncRef.current(next, s, ps);
120
+ return next;
121
+ });
122
+ resetPagination();
123
+ },
124
+ [resetPagination],
125
+ );
126
+
127
+ // -- Sort callback ----------------------------------------------------------
128
+
129
+ const setSort = useCallback(
130
+ (nextSort: SortState | null) => {
131
+ const { filters: f, pageSize: ps } = stateRef.current;
132
+ setLocalSort(nextSort);
133
+ debouncedSyncRef.current(f, nextSort, ps);
134
+ resetPagination();
135
+ },
136
+ [resetPagination],
137
+ );
138
+
139
+ // -- Reset ------------------------------------------------------------------
140
+
141
+ const resetAll = useCallback(() => {
142
+ setFilters([]);
143
+ setLocalSort(null);
144
+ resetPagination();
145
+ syncToUrl([], null, defaultPageSize, 0);
146
+ setPageSizeState(defaultPageSize);
147
+ }, [syncToUrl, resetPagination]);
148
+
149
+ // -- Pagination callbacks ---------------------------------------------------
150
+ // Uses a cursor stack to track visited pages. "Next" pushes the current
151
+ // endCursor onto the stack; "Previous" pops it to restore the prior cursor.
152
+
153
+ const goToNextPage = useCallback((endCursor: string) => {
154
+ cursorStackRef.current = [...cursorStackRef.current, endCursor];
155
+ setAfterCursor(endCursor);
156
+ setPageIndex((prev) => {
157
+ const nextIndex = prev + 1;
158
+ const { filters: f, sort: s, pageSize: ps } = stateRef.current;
159
+ debouncedSyncRef.current(f, s, ps, nextIndex);
160
+ return nextIndex;
161
+ });
162
+ }, []);
163
+
164
+ const goToPreviousPage = useCallback(() => {
165
+ const stack = cursorStackRef.current;
166
+ const next = stack.slice(0, -1);
167
+ cursorStackRef.current = next;
168
+ setAfterCursor(next.length > 0 ? next[next.length - 1] : undefined);
169
+ setPageIndex((prev) => {
170
+ const nextIndex = Math.max(0, prev - 1);
171
+ const { filters: f, sort: s, pageSize: ps } = stateRef.current;
172
+ debouncedSyncRef.current(f, s, ps, nextIndex);
173
+ return nextIndex;
174
+ });
175
+ }, []);
176
+
177
+ const setPageSize = useCallback(
178
+ (newSize: number) => {
179
+ const validated = getValidPageSize(newSize);
180
+ const { filters: f, sort: s } = stateRef.current;
181
+ setPageSizeState(validated);
182
+ resetPagination();
183
+ debouncedSyncRef.current(f, s, validated);
184
+ },
185
+ [resetPagination],
186
+ );
187
+
188
+ // -- Derived query objects ---------------------------------------------------
189
+ // Translate local filter/sort state into API-ready `where` and `orderBy`.
190
+
191
+ const where = useMemo(
192
+ () => buildFilter<TFilter>(filters, filterConfigs),
193
+ [filters, filterConfigs],
194
+ );
195
+
196
+ const orderBy = useMemo(() => buildOrderBy<TOrderBy>(sort), [sort]);
197
+
198
+ // -- Memoized return groups -------------------------------------------------
199
+ // Each group is individually memoized so its object reference stays stable
200
+ // unless the contained values change. This makes it safe to pass a group
201
+ // (e.g. `pagination`) directly as props to a React.memo child without
202
+ // causing unnecessary re-renders.
203
+
204
+ const filtersGroup = useMemo(
205
+ () => ({ active: filters, set: setFilter, remove: removeFilter }),
206
+ [filters, setFilter, removeFilter],
207
+ );
208
+
209
+ const sortGroup = useMemo(() => ({ current: sort, set: setSort }), [sort, setSort]);
210
+
211
+ const query = useMemo(() => ({ where, orderBy }), [where, orderBy]);
212
+
213
+ const pagination = useMemo(
214
+ () => ({ pageSize, pageIndex, afterCursor, setPageSize, goToNextPage, goToPreviousPage }),
215
+ [pageSize, pageIndex, afterCursor, setPageSize, goToNextPage, goToPreviousPage],
216
+ );
217
+
218
+ return {
219
+ filters: filtersGroup,
220
+ sort: sortGroup,
221
+ query,
222
+ pagination,
223
+ resetAll,
224
+ };
225
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Creates a debounced version of the provided function.
3
+ *
4
+ * Each call to the returned function resets the internal timer. The wrapped
5
+ * function is only invoked once the timer expires without being reset. This
6
+ * makes it ideal for rate-limiting high-frequency events like input changes.
7
+ *
8
+ * @typeParam T - The function signature to debounce.
9
+ * @param fn - The function to debounce.
10
+ * @param ms - The debounce delay in milliseconds.
11
+ * @returns A new function with the same signature that delays execution.
12
+ */
13
+ export function debounce<T extends (...args: never[]) => void>(
14
+ fn: T,
15
+ ms: number,
16
+ ): (...args: Parameters<T>) => void {
17
+ let timer: ReturnType<typeof setTimeout> | undefined;
18
+ return (...args: Parameters<T>) => {
19
+ clearTimeout(timer);
20
+ timer = setTimeout(() => fn(...args), ms);
21
+ };
22
+ }
@@ -0,0 +1,29 @@
1
+ export function fieldValue(
2
+ field: { displayValue?: string | null; value?: unknown } | null | undefined,
3
+ ): string | null {
4
+ if (field?.displayValue != null) return field.displayValue;
5
+ if (field?.value != null) return String(field.value);
6
+ return null;
7
+ }
8
+
9
+ export function getAddressFieldLines(address: {
10
+ street?: string | null;
11
+ city?: string | null;
12
+ state?: string | null;
13
+ postalCode?: string | null;
14
+ country?: string | null;
15
+ }) {
16
+ const cityStateZip = [address.city, address.state].filter(Boolean).join(", ");
17
+ const cityStateZipLine = [cityStateZip, address.postalCode].filter(Boolean).join(" ");
18
+ const lines = [address.street, cityStateZipLine, address.country].filter(Boolean);
19
+ if (lines.length === 0) return null;
20
+ return lines;
21
+ }
22
+
23
+ export function formatDateTimeField(
24
+ value?: string | null,
25
+ ...args: Parameters<Date["toLocaleString"]>
26
+ ) {
27
+ if (!value) return null;
28
+ return new Date(value).toLocaleString(...args);
29
+ }
@@ -0,0 +1,372 @@
1
+ /**
2
+ * filterUtils.ts
3
+ *
4
+ * Centralizes all filter-related transformations for the object search feature.
5
+ * This module handles two distinct concerns:
6
+ *
7
+ * 1. **URL serialization** — Converting filter/sort state to and from
8
+ * URLSearchParams so that search criteria can be bookmarked, shared, and
9
+ * restored on page load.
10
+ *
11
+ * 2. **GraphQL query building** — Converting the same filter state into the
12
+ * `where` clause shape expected by the GraphQL API.
13
+ *
14
+ * Both concerns operate on the shared {@link ActiveFilterValue} type, which
15
+ * represents a single active filter with a field name, filter type, and one or
16
+ * more values (value, min, max).
17
+ */
18
+
19
+ import type { SortState } from "./sortUtils";
20
+
21
+ export type FilterFieldType =
22
+ | "text"
23
+ | "picklist"
24
+ | "numeric"
25
+ | "boolean"
26
+ | "date"
27
+ | "daterange"
28
+ | "multipicklist"
29
+ | "search";
30
+
31
+ export type FilterFieldConfig<TFieldName extends string = string> = {
32
+ field: TFieldName;
33
+ label: string;
34
+ type: FilterFieldType;
35
+ placeholder?: string;
36
+ /** Required for picklist type. */
37
+ options?: Array<{ value: string; label: string }>;
38
+ helpText?: string;
39
+ /** Required for search type — the fields to match against with `or`. */
40
+ searchFields?: string[];
41
+ };
42
+
43
+ export type ActiveFilterValue<TFieldName extends string = string> = {
44
+ field: TFieldName;
45
+ label: string;
46
+ type: FilterFieldType;
47
+ value?: string;
48
+ min?: string;
49
+ max?: string;
50
+ };
51
+
52
+ // ---------------------------------------------------------------------------
53
+ // URL Serialization
54
+ // ---------------------------------------------------------------------------
55
+
56
+ /**
57
+ * Prefix applied to all filter-related URL search params.
58
+ * This namespaces filter params so they don't collide with other query params
59
+ * (e.g. pagination, feature flags).
60
+ *
61
+ * @example "f.Industry=Technology" or "f.AnnualRevenue.min=1000000"
62
+ */
63
+ const FILTER_PREFIX = "f.";
64
+
65
+ /** URL param key for the multi-field search term. */
66
+ const SEARCH_KEY = "q";
67
+
68
+ /** URL param key for the currently sorted field name. */
69
+ const SORT_KEY = "sort";
70
+
71
+ /** URL param key for the sort direction (ASC or DESC). */
72
+ const DIR_KEY = "dir";
73
+
74
+ /** URL param key for the page size preference. */
75
+ const PAGE_SIZE_KEY = "ps";
76
+ const PAGE_KEY = "page";
77
+
78
+ /**
79
+ * Serializes filter and sort state into URLSearchParams.
80
+ *
81
+ * Encoding scheme:
82
+ * - Simple values (text, picklist, boolean, multipicklist):
83
+ * `f.<field>=<value>`
84
+ * - Range values (numeric, date, daterange):
85
+ * `f.<field>.min=<min>` and/or `f.<field>.max=<max>`
86
+ * - Sort: `sort=<field>&dir=ASC|DESC`
87
+ *
88
+ * @param filters - The currently active filters to serialize.
89
+ * @param sort - The current sort state, or null if no sort is applied.
90
+ * @returns A URLSearchParams instance representing the full search state.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * const params = filtersToSearchParams(
95
+ * [{ field: "Industry", type: "picklist", value: "Technology" }],
96
+ * { field: "Name", direction: "ASC" },
97
+ * );
98
+ * // params.toString() => "f.Industry=Technology&sort=Name&dir=ASC"
99
+ * ```
100
+ */
101
+ export function filtersToSearchParams(
102
+ filters: ActiveFilterValue[],
103
+ sort: SortState | null,
104
+ pageSize?: number,
105
+ pageIndex?: number,
106
+ ): URLSearchParams {
107
+ const params = new URLSearchParams();
108
+
109
+ for (const filter of filters) {
110
+ if (filter.type === "search") {
111
+ if (filter.value) params.set(SEARCH_KEY, filter.value);
112
+ continue;
113
+ }
114
+ if (filter.value !== undefined && filter.value !== "") {
115
+ params.set(`${FILTER_PREFIX}${filter.field}`, filter.value);
116
+ }
117
+ if (filter.min !== undefined && filter.min !== "") {
118
+ params.set(`${FILTER_PREFIX}${filter.field}.min`, filter.min);
119
+ }
120
+ if (filter.max !== undefined && filter.max !== "") {
121
+ params.set(`${FILTER_PREFIX}${filter.field}.max`, filter.max);
122
+ }
123
+ }
124
+
125
+ if (sort) {
126
+ params.set(SORT_KEY, sort.field);
127
+ params.set(DIR_KEY, sort.direction);
128
+ }
129
+
130
+ if (pageSize !== undefined) {
131
+ params.set(PAGE_SIZE_KEY, String(pageSize));
132
+ }
133
+
134
+ if (pageIndex !== undefined && pageIndex > 0) {
135
+ params.set(PAGE_KEY, String(pageIndex + 1));
136
+ }
137
+
138
+ return params;
139
+ }
140
+
141
+ /**
142
+ * Deserializes URLSearchParams back into filter and sort state.
143
+ *
144
+ * Requires the full list of filter configs so it knows which URL params to look
145
+ * for and what type each filter is. Params that don't match a known config are
146
+ * silently ignored, making this safe against stale or hand-edited URLs.
147
+ *
148
+ * @param params - The URLSearchParams to parse (typically from the browser URL).
149
+ * @param configs - The filter field configurations defining available filters.
150
+ * @returns An object containing the deserialized `filters` array and `sort` state.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * const url = new URLSearchParams("f.Industry=Technology&sort=Name&dir=ASC");
155
+ * const { filters, sort } = searchParamsToFilters(url, filterConfigs);
156
+ * // filters => [{ field: "Industry", type: "picklist", value: "Technology" }]
157
+ * // sort => { field: "Name", direction: "ASC" }
158
+ * ```
159
+ */
160
+ export function searchParamsToFilters(
161
+ params: URLSearchParams,
162
+ configs: FilterFieldConfig[],
163
+ ): {
164
+ filters: ActiveFilterValue[];
165
+ sort: SortState | null;
166
+ pageSize: number | undefined;
167
+ pageIndex: number;
168
+ } {
169
+ const filters: ActiveFilterValue[] = [];
170
+
171
+ for (const config of configs) {
172
+ const { field, label, type } = config;
173
+
174
+ if (type === "search") {
175
+ const q = params.get(SEARCH_KEY);
176
+ if (q) {
177
+ filters.push({ field, label, type: "search", value: q });
178
+ }
179
+ continue;
180
+ }
181
+
182
+ const value = params.get(`${FILTER_PREFIX}${field}`) ?? undefined;
183
+ const min = params.get(`${FILTER_PREFIX}${field}.min`) ?? undefined;
184
+ const max = params.get(`${FILTER_PREFIX}${field}.max`) ?? undefined;
185
+
186
+ const hasValue = value !== undefined && value !== "";
187
+ const hasRange = (min !== undefined && min !== "") || (max !== undefined && max !== "");
188
+
189
+ if (hasValue || hasRange) {
190
+ filters.push({ field, label, type, value, min, max });
191
+ }
192
+ }
193
+
194
+ let sort: SortState | null = null;
195
+ const sortField = params.get(SORT_KEY);
196
+ const sortDir = params.get(DIR_KEY);
197
+ if (sortField) {
198
+ sort = {
199
+ field: sortField,
200
+ direction: sortDir === "DESC" ? "DESC" : "ASC",
201
+ };
202
+ }
203
+
204
+ const pageSizeRaw = params.get(PAGE_SIZE_KEY);
205
+ const pageSize = pageSizeRaw ? parseInt(pageSizeRaw, 10) : undefined;
206
+
207
+ const pageRaw = params.get(PAGE_KEY);
208
+ const page = pageRaw ? parseInt(pageRaw, 10) : 1;
209
+ const pageIndex = !isNaN(page) && page > 1 ? page - 1 : 0;
210
+
211
+ return {
212
+ filters,
213
+ sort,
214
+ pageSize: pageSize && !isNaN(pageSize) ? pageSize : undefined,
215
+ pageIndex,
216
+ };
217
+ }
218
+
219
+ // ---------------------------------------------------------------------------
220
+ // GraphQL Filter Building
221
+ // ---------------------------------------------------------------------------
222
+
223
+ /**
224
+ * Converts an array of active filter values into a GraphQL `where` clause.
225
+ *
226
+ * Each filter is individually converted to a clause via {@link buildSingleFilter},
227
+ * then multiple clauses are combined with a top-level `and` operator. This ensures
228
+ * all active filters are applied simultaneously (intersection semantics).
229
+ *
230
+ * @typeParam TFilter - The GraphQL filter input type (e.g. `AccountFilterInput`).
231
+ * @param filters - The active filters to convert.
232
+ * @returns A filter object for the GraphQL `where` variable, or `undefined` if
233
+ * no filters are active (which tells the API to return unfiltered results).
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * const where = buildFilter<AccountFilterInput>([
238
+ * { field: "Industry", type: "picklist", value: "Technology" },
239
+ * { field: "AnnualRevenue", type: "numeric", min: "1000000" },
240
+ * ]);
241
+ * // where => { and: [
242
+ * // { Industry: { eq: "Technology" } },
243
+ * // { AnnualRevenue: { gte: 1000000 } },
244
+ * // ]}
245
+ * ```
246
+ */
247
+ export function buildFilter<TFilter>(
248
+ filters: ActiveFilterValue[],
249
+ configs: FilterFieldConfig[],
250
+ ): TFilter | undefined {
251
+ const configMap = new Map(configs.map((c) => [c.field, c]));
252
+ const clauses: TFilter[] = [];
253
+
254
+ for (const filter of filters) {
255
+ const clause = buildSingleFilter<TFilter>(filter, configMap.get(filter.field));
256
+ if (clause) clauses.push(clause);
257
+ }
258
+
259
+ if (clauses.length === 0) return undefined;
260
+ if (clauses.length === 1) return clauses[0];
261
+ return { and: clauses } as TFilter;
262
+ }
263
+
264
+ /**
265
+ * Converts a YYYY-MM-DD date string to a full ISO-8601 datetime at midnight UTC.
266
+ * Used as the inclusive lower bound for date range queries.
267
+ */
268
+ function toStartOfDay(dateStr: string): string {
269
+ return `${dateStr}T00:00:00.000Z`;
270
+ }
271
+
272
+ /**
273
+ * Converts a YYYY-MM-DD date string to a full ISO-8601 datetime at the last
274
+ * millisecond of the day in UTC. Used as the inclusive upper bound for date
275
+ * range queries.
276
+ */
277
+ function toEndOfDay(dateStr: string): string {
278
+ return `${dateStr}T23:59:59.999Z`;
279
+ }
280
+
281
+ /**
282
+ * Converts a single active filter value into a GraphQL filter clause.
283
+ *
284
+ * Supported filter types and their GraphQL mappings:
285
+ *
286
+ * | Type | GraphQL operator(s) | Example output |
287
+ * |-----------------|---------------------|----------------------------------------------------------|
288
+ * | `text` | `like` | `{ Name: { like: "%Acme%" } }` |
289
+ * | `picklist` | `eq` | `{ Industry: { eq: "Technology" } }` |
290
+ * | `multipicklist` | `eq` or `in` | `{ Type: { in: ["A", "B"] } }` |
291
+ * | `numeric` | `gte` / `lte` | `{ Revenue: { gte: 1000, lte: 5000 } }` |
292
+ * | `boolean` | `eq` | `{ IsActive: { eq: true } }` |
293
+ * | `date` | dynamic operator | `{ CreatedDate: { gte: { value: "..." } } }` |
294
+ * | `daterange` | `gte` + `lte` | Combined with `and` if both bounds set |
295
+ * | `search` | `like` + `or` | `{ or: [{ Name: { like: "%x%" } }, { Phone: { like: "%x%" } }] }` |
296
+ *
297
+ * The `search` type uses `config.searchFields` to build an `or` clause that
298
+ * matches the search term across multiple fields simultaneously (union semantics).
299
+ *
300
+ * @param filter - The active filter value to convert.
301
+ * @param config - The corresponding field config. Required for `search` type
302
+ * (provides `searchFields`); optional for all other types.
303
+ * @returns A single filter clause, or `null` if the filter has no meaningful value.
304
+ */
305
+ function buildSingleFilter<TFilter>(
306
+ filter: ActiveFilterValue,
307
+ config?: FilterFieldConfig,
308
+ ): TFilter | null {
309
+ const { field, type, value, min, max } = filter;
310
+
311
+ switch (type) {
312
+ case "text": {
313
+ if (!value) return null;
314
+ return { [field]: { like: `%${value}%` } } as TFilter;
315
+ }
316
+ case "picklist": {
317
+ if (!value) return null;
318
+ return { [field]: { eq: value } } as TFilter;
319
+ }
320
+ case "numeric": {
321
+ if (!min && !max) return null;
322
+ const ops: Record<string, number> = {};
323
+ if (min) ops.gte = Number(min);
324
+ if (max) ops.lte = Number(max);
325
+ return { [field]: ops } as TFilter;
326
+ }
327
+ case "boolean": {
328
+ if (value === undefined || value === "") return null;
329
+ return { [field]: { eq: value === "true" } } as TFilter;
330
+ }
331
+ case "multipicklist": {
332
+ if (!value) return null;
333
+ const values = value.split(",");
334
+ if (values.length === 1) {
335
+ return { [field]: { eq: values[0] } } as TFilter;
336
+ }
337
+ return { [field]: { in: values } } as TFilter;
338
+ }
339
+ case "date": {
340
+ if (!min && !max) return null;
341
+ const op = value ?? (min ? "gte" : "lte");
342
+ const dateStr = min ?? max;
343
+ const isoStr = op === "gte" || op === "gt" ? toStartOfDay(dateStr!) : toEndOfDay(dateStr!);
344
+ return { [field]: { [op]: { value: isoStr } } } as TFilter;
345
+ }
346
+ case "daterange": {
347
+ if (!min && !max) return null;
348
+ const clauses: TFilter[] = [];
349
+ if (min) {
350
+ clauses.push({
351
+ [field]: { gte: { value: toStartOfDay(min) } },
352
+ } as TFilter);
353
+ }
354
+ if (max) {
355
+ clauses.push({
356
+ [field]: { lte: { value: toEndOfDay(max) } },
357
+ } as TFilter);
358
+ }
359
+ return clauses.length === 1 ? clauses[0] : ({ and: clauses } as TFilter);
360
+ }
361
+ case "search": {
362
+ if (!value) return null;
363
+ const searchFields = config?.searchFields ?? [];
364
+ if (searchFields.length === 0) return null;
365
+ const clauses = searchFields.map((f) => ({ [f]: { like: `%${value}%` } }) as TFilter);
366
+ if (clauses.length === 1) return clauses[0];
367
+ return { or: clauses } as TFilter;
368
+ }
369
+ default:
370
+ return null;
371
+ }
372
+ }
@@ -0,0 +1,38 @@
1
+ import { ResultOrder, NullOrder } from "../../../api/graphql-operations-types";
2
+
3
+ export type SortFieldConfig<TFieldName extends string = string> = {
4
+ field: TFieldName;
5
+ label: string;
6
+ };
7
+
8
+ export type SortState<TFieldName extends string = string> = {
9
+ field: TFieldName;
10
+ direction: "ASC" | "DESC";
11
+ };
12
+
13
+ /**
14
+ * Converts a {@link SortState} into a GraphQL order-by object.
15
+ *
16
+ * @typeParam TOrderBy - The GraphQL order-by input type (e.g. `AccountOrderByInput`).
17
+ * @param sort - The current sort state from the UI, or `null` if no sort is applied.
18
+ * @returns An order-by object for the GraphQL query's `orderBy` variable, or
19
+ * `undefined` if no sort is active (which uses the API's default ordering).
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const orderBy = buildOrderBy<AccountOrderByInput>({
24
+ * field: "Name",
25
+ * direction: "ASC",
26
+ * });
27
+ * // orderBy => { Name: { order: ResultOrder.Asc, nulls: NullOrder.Last } }
28
+ * ```
29
+ */
30
+ export function buildOrderBy<TOrderBy>(sort: SortState | null): TOrderBy | undefined {
31
+ if (!sort) return undefined;
32
+ return {
33
+ [sort.field]: {
34
+ order: sort.direction === "ASC" ? ResultOrder.Asc : ResultOrder.Desc,
35
+ nulls: NullOrder.Last,
36
+ },
37
+ } as TOrderBy;
38
+ }