@cccsaurora/howler-ui 3.1.1-dev.1747.1 → 3.1.1-dev.1758.1

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.
@@ -9,9 +9,11 @@ export interface ParameterContextType {
9
9
  span?: string;
10
10
  indexes?: SearchIndex[];
11
11
  filters?: string[];
12
+ disabledFilterIndexes: number[];
12
13
  startDate?: string | null;
13
14
  endDate?: string | null;
14
15
  views?: string[];
16
+ disabledViewIndexes: number[];
15
17
  setSelected: (id: string | null) => void;
16
18
  setQuery: (id: string) => void;
17
19
  setOffset: (offset: string | number) => void;
@@ -20,6 +22,8 @@ export interface ParameterContextType {
20
22
  setCustomSpan: (startDate: string, endDate: string) => void;
21
23
  addFilter: (filter: string) => void;
22
24
  removeFilter: (filter: string) => void;
25
+ enableFilter: (index: number) => void;
26
+ disableFilter: (index: number) => void;
23
27
  setFilter: (index: number, filter: string) => void;
24
28
  resetFilters: () => void;
25
29
  addIndex: (index: SearchIndex) => void;
@@ -29,6 +33,8 @@ export interface ParameterContextType {
29
33
  resetIndexes: () => void;
30
34
  addView: (view: string) => void;
31
35
  removeView: (view: string) => void;
36
+ enableView: (index: number) => void;
37
+ disableView: (index: number) => void;
32
38
  setView: (index: number, view: string) => void;
33
39
  resetViews: () => void;
34
40
  }
@@ -39,7 +45,9 @@ interface SearchValues {
39
45
  span: string;
40
46
  indexes: SearchIndex[];
41
47
  filters: string[];
48
+ disabledFilterIndexes: number[];
42
49
  views: string[];
50
+ disabledViewIndexes: number[];
43
51
  startDate: string | null;
44
52
  endDate: string | null;
45
53
  offset: number;
@@ -1,10 +1,10 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { identity, isEmpty, isEqual, isNil, isUndefined, omitBy, uniq } from 'lodash-es';
3
- import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import { has, identity, isEmpty, isEqual, isNil, isUndefined, omitBy, uniq } from 'lodash-es';
3
+ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
4
4
  import { useLocation, useParams, useSearchParams } from 'react-router';
5
5
  import { createContext, useContextSelector } from 'use-context-selector';
6
6
  import { DEFAULT_QUERY } from '@cccsaurora/howler-ui/utils/constants';
7
- import Throttler from '@cccsaurora/howler-ui/utils/Throttler';
7
+ import { notNil } from '@cccsaurora/howler-ui/utils/utils';
8
8
  import { missingContext } from './contextUtils';
9
9
  const DEFAULT_VALUES = {
10
10
  query: DEFAULT_QUERY,
@@ -18,9 +18,11 @@ const DEFAULT_PARAMETER_CONTEXT = {
18
18
  offset: 0,
19
19
  trackTotalHits: false,
20
20
  filters: [],
21
+ disabledFilterIndexes: [],
21
22
  startDate: null,
22
23
  endDate: null,
23
24
  views: [],
25
+ disabledViewIndexes: [],
24
26
  setSelected: () => missingContext('ParameterContext'),
25
27
  setQuery: () => missingContext('ParameterContext'),
26
28
  setOffset: () => missingContext('ParameterContext'),
@@ -30,6 +32,8 @@ const DEFAULT_PARAMETER_CONTEXT = {
30
32
  addFilter: () => missingContext('ParameterContext'),
31
33
  removeFilter: () => missingContext('ParameterContext'),
32
34
  setFilter: () => missingContext('ParameterContext'),
35
+ enableFilter: () => missingContext('ParameterContext'),
36
+ disableFilter: () => missingContext('ParameterContext'),
33
37
  resetFilters: () => missingContext('ParameterContext'),
34
38
  addIndex: () => missingContext('ParameterContext'),
35
39
  removeIndex: () => missingContext('ParameterContext'),
@@ -38,6 +42,8 @@ const DEFAULT_PARAMETER_CONTEXT = {
38
42
  resetIndexes: () => missingContext('ParameterContext'),
39
43
  addView: () => missingContext('ParameterContext'),
40
44
  removeView: () => missingContext('ParameterContext'),
45
+ enableView: () => missingContext('ParameterContext'),
46
+ disableView: () => missingContext('ParameterContext'),
41
47
  setView: () => missingContext('ParameterContext'),
42
48
  resetViews: () => missingContext('ParameterContext')
43
49
  };
@@ -52,12 +58,12 @@ const PARAM_MAPPINGS = [
52
58
  ];
53
59
  /** Multi-value URL params that map to array state keys */
54
60
  const ARRAY_PARAMS = [
55
- { urlKey: 'filter', stateKey: 'filters' },
56
- { urlKey: 'view', stateKey: 'views' },
61
+ { urlKey: 'filter', stateKey: 'filters', disabled: { urlKey: 'disabled_filter', stateKey: 'disabledFilterIndexes' } },
62
+ { urlKey: 'view', stateKey: 'views', disabled: { urlKey: 'disabled_view', stateKey: 'disabledViewIndexes' } },
57
63
  { urlKey: 'index', stateKey: 'indexes' }
58
64
  ];
59
- const ARRAY_URL_KEYS = new Set(ARRAY_PARAMS.map(p => p.urlKey));
60
- const WRITE_THROTTLER = new Throttler(100);
65
+ const ARRAY_URL_KEYS = new Set(ARRAY_PARAMS.flatMap(p => [p.urlKey, p.disabled?.urlKey]).filter(notNil));
66
+ const WRITE_DELAY_MS = 100;
61
67
  /**
62
68
  * Helper function to convert a number/string representation of a number into a valid offset.
63
69
  * @returns
@@ -85,44 +91,110 @@ const getSelectedValue = (params, pathname, bundleId) => {
85
91
  return null;
86
92
  };
87
93
  /**
88
- * Returns stable add / remove / setAt / setAll / clear handlers for a list field in
89
- * SearchValues. All returned functions are memoized; since _setValues (from useState)
90
- * and key are both stable for the lifetime of the component, the deps array is empty.
94
+ * Returns stable list handlers, keeping disabled positions attached through edits,
95
+ * deduplication and removal. Lists without disabled metadata ignore enable/disable.
91
96
  */
92
97
  const useListHandlers = (key, _setValues) => {
93
- const add = useCallback((item) => _setValues(c => ({ ...c, [key]: uniq([...c[key], item]) })),
94
- // eslint-disable-next-line react-hooks/exhaustive-deps
95
- []);
98
+ const disabledKey = ARRAY_PARAMS.find(param => param.stateKey === key)?.disabled?.stateKey;
99
+ const update = useCallback((current, items, disabledIndexes = disabledKey ? current[disabledKey] : []) => {
100
+ if (!disabledKey) {
101
+ return { ...current, [key]: items };
102
+ }
103
+ const normalized = normalizeList(items, disabledIndexes);
104
+ return isEqual(normalized.items, current[key]) && isEqual(normalized.disabledIndexes, current[disabledKey])
105
+ ? current
106
+ : { ...current, [key]: normalized.items, [disabledKey]: normalized.disabledIndexes };
107
+ }, [key, disabledKey]);
108
+ const add = useCallback((item) => _setValues(c => update(c, uniq([...c[key], item]))), [_setValues, key, update]);
96
109
  const remove = useCallback((item) => _setValues(c => {
97
110
  const arr = c[key];
98
111
  const i = arr.indexOf(item);
99
- return i === -1 ? c : { ...c, [key]: arr.filter((_, idx) => idx !== i) };
100
- }),
101
- // eslint-disable-next-line react-hooks/exhaustive-deps
102
- []);
112
+ if (i === -1) {
113
+ return c;
114
+ }
115
+ const disabledIndexes = (disabledKey ? c[disabledKey] : []).flatMap(index => index === i ? [] : [index > i ? index - 1 : index]);
116
+ return update(c, arr.filter((_, idx) => idx !== i), disabledIndexes);
117
+ }), [_setValues, key, disabledKey, update]);
103
118
  const setAt = useCallback((pos, item) => _setValues(c => {
104
119
  const arr = c[key];
105
- if (pos < 0 || pos >= arr.length) {
120
+ if (!Number.isInteger(pos) || pos < 0 || pos >= arr.length) {
106
121
  return c;
107
122
  }
108
123
  const next = [...arr];
109
124
  next[pos] = item;
110
- return { ...c, [key]: next };
111
- }),
112
- // eslint-disable-next-line react-hooks/exhaustive-deps
113
- []);
114
- const setAll = useCallback((items) => _setValues(c => ({ ...c, [key]: uniq(items) })),
115
- // eslint-disable-next-line react-hooks/exhaustive-deps
116
- []);
117
- const reset = useCallback((defaultValue = []) => _setValues(c => ({ ...c, [key]: defaultValue })),
118
- // eslint-disable-next-line react-hooks/exhaustive-deps
119
- []);
120
- return { add, remove, setAt, setAll, reset };
125
+ return update(c, next);
126
+ }), [_setValues, key, update]);
127
+ const setAll = useCallback((items) => _setValues(c => update(c, uniq(items), [])), [_setValues, update]);
128
+ const reset = useCallback((defaultValue = []) => _setValues(c => update(c, defaultValue, [])), [_setValues, update]);
129
+ const setDisabled = useCallback((index, disabled) => {
130
+ if (!disabledKey) {
131
+ return;
132
+ }
133
+ _setValues(c => {
134
+ if (!Number.isInteger(index) || index < 0 || index >= c[key].length) {
135
+ return c;
136
+ }
137
+ const disabledIndexes = c[disabledKey];
138
+ if (disabledIndexes.includes(index) === disabled) {
139
+ return c;
140
+ }
141
+ return update(c, c[key], disabled ? [...disabledIndexes, index] : disabledIndexes.filter(position => position !== index));
142
+ });
143
+ }, [_setValues, key, disabledKey, update]);
144
+ const enable = useCallback((index) => setDisabled(index, false), [setDisabled]);
145
+ const disable = useCallback((index) => setDisabled(index, true), [setDisabled]);
146
+ return { add, remove, setAt, setAll, reset, enable, disable };
147
+ };
148
+ const normalizeList = (items, disabledIndexes) => {
149
+ const indexesByItem = new Map();
150
+ const normalizedItems = [];
151
+ const normalizedDisabled = [];
152
+ items.forEach((item, index) => {
153
+ const normalizedIndex = indexesByItem.get(item);
154
+ const isDisabled = disabledIndexes.includes(index);
155
+ if (normalizedIndex === undefined) {
156
+ const nextIndex = normalizedItems.length;
157
+ indexesByItem.set(item, nextIndex);
158
+ normalizedItems.push(item);
159
+ normalizedDisabled[nextIndex] = isDisabled;
160
+ return;
161
+ }
162
+ normalizedDisabled[normalizedIndex] = normalizedDisabled[normalizedIndex] && isDisabled;
163
+ });
164
+ return {
165
+ items: normalizedItems,
166
+ disabledIndexes: normalizedDisabled.flatMap((isDisabled, index) => (isDisabled ? [index] : []))
167
+ };
168
+ };
169
+ const getListFromUrl = (params, { urlKey, disabled }) => {
170
+ const rawItems = params.getAll(urlKey);
171
+ const disabledCounts = new Map();
172
+ const itemCounts = new Map();
173
+ (disabled ? params.getAll(disabled.urlKey) : []).forEach(item => {
174
+ disabledCounts.set(item, (disabledCounts.get(item) ?? 0) + 1);
175
+ });
176
+ rawItems.forEach(item => {
177
+ itemCounts.set(item, (itemCounts.get(item) ?? 0) + 1);
178
+ });
179
+ const items = uniq(rawItems);
180
+ return {
181
+ items,
182
+ disabledIndexes: items.flatMap((item, index) => (disabledCounts.get(item) ?? 0) >= (itemCounts.get(item) ?? 0) ? [index] : [])
183
+ };
121
184
  };
185
+ const getListStateFromUrl = (params, defaults) => Object.fromEntries(ARRAY_PARAMS.flatMap(descriptor => {
186
+ const { items, disabledIndexes } = getListFromUrl(params, descriptor);
187
+ const resolved = descriptor.stateKey === 'indexes' && isEmpty(items) ? defaults.indexes : items;
188
+ return [
189
+ [descriptor.stateKey, resolved],
190
+ ...(descriptor.disabled ? [[descriptor.disabled.stateKey, disabledIndexes]] : [])
191
+ ];
192
+ }));
122
193
  /**
123
194
  * Synchronizes SearchValues state with the URL search string, and vice-versa.
124
195
  */
125
196
  const useUrlSync = (values, defaults, _setValues, params, setParams, pathname, search, routeId) => {
197
+ const lastProcessedLocation = useRef({ pathname, search, routeId });
126
198
  const getUrlFromState = useCallback(() => {
127
199
  const changes = {};
128
200
  // Scalar params: write if changed from URL, remove if back to default
@@ -140,19 +212,27 @@ const useUrlSync = (values, defaults, _setValues, params, setParams, pathname, s
140
212
  }
141
213
  });
142
214
  // Array params: skip when state equals default and URL is already empty
143
- ARRAY_PARAMS.forEach(({ urlKey, stateKey }) => {
144
- const stateArr = values[stateKey];
215
+ ARRAY_PARAMS.forEach(({ urlKey, stateKey, disabled }) => {
216
+ const { items: stateArr, disabledIndexes } = disabled
217
+ ? normalizeList(values[stateKey], values[disabled.stateKey])
218
+ : { items: values[stateKey], disabledIndexes: [] };
145
219
  const urlArr = params.getAll(urlKey);
146
220
  const defaultValue = stateKey === 'indexes' ? defaults.indexes : undefined;
147
- if (isEqual(stateArr, urlArr)) {
148
- return;
149
- }
150
- const isDefault = defaultValue ? isEqual(stateArr, defaultValue) : stateArr.length === 0;
151
- if (!isDefault) {
152
- changes[urlKey] = stateArr.length === 0 ? null : stateArr;
221
+ if (!isEqual(stateArr, urlArr)) {
222
+ const isDefault = defaultValue ? isEqual(stateArr, defaultValue) : stateArr.length === 0;
223
+ if (!isDefault) {
224
+ changes[urlKey] = stateArr.length === 0 ? null : stateArr;
225
+ }
226
+ else if (urlArr.length > 0) {
227
+ changes[urlKey] = null; // state is default but URL isn't — remove
228
+ }
153
229
  }
154
- else if (urlArr.length > 0) {
155
- changes[urlKey] = null; // state is default but URL isn't — remove
230
+ // Keep every definition in the URL, with disabled entries marked separately.
231
+ if (disabled) {
232
+ const disabledParams = disabledIndexes.map(index => stateArr[index]);
233
+ if (!isEqual(disabledParams, params.getAll(disabled.urlKey))) {
234
+ changes[disabled.urlKey] = disabledParams.length === 0 ? null : disabledParams;
235
+ }
156
236
  }
157
237
  });
158
238
  // selected
@@ -171,6 +251,7 @@ const useUrlSync = (values, defaults, _setValues, params, setParams, pathname, s
171
251
  }, [values, defaults, params, pathname]);
172
252
  const getStateFromUrl = useCallback(() => {
173
253
  const changes = {};
254
+ const listState = getListStateFromUrl(params, defaults);
174
255
  // Scalar params: fall back to default when absent from URL
175
256
  PARAM_MAPPINGS.forEach(([urlKey, stateKey]) => {
176
257
  const urlValue = params.has(urlKey)
@@ -183,12 +264,9 @@ const useUrlSync = (values, defaults, _setValues, params, setParams, pathname, s
183
264
  }
184
265
  });
185
266
  // Array params: fall back to their declared default when absent from URL
186
- ARRAY_PARAMS.forEach(({ urlKey, stateKey }) => {
187
- const raw = params.getAll(urlKey);
188
- const defaultValue = stateKey === 'indexes' ? defaults.indexes : undefined;
189
- const resolved = (isEmpty(raw) && defaultValue ? defaultValue : uniq(raw));
190
- if (!isEqual(resolved, values[stateKey])) {
191
- changes[stateKey] = resolved;
267
+ Object.entries(listState).forEach(([key, value]) => {
268
+ if (!isEqual(value, values[key])) {
269
+ changes[key] = value;
192
270
  }
193
271
  });
194
272
  // selected
@@ -205,6 +283,9 @@ const useUrlSync = (values, defaults, _setValues, params, setParams, pathname, s
205
283
  }, [values, defaults, params, pathname, routeId]);
206
284
  // State → URL
207
285
  useEffect(() => {
286
+ if (!isEqual(lastProcessedLocation.current, { pathname, search, routeId })) {
287
+ return;
288
+ }
208
289
  const changes = getUrlFromState();
209
290
  if (isEmpty(changes)) {
210
291
  return;
@@ -220,15 +301,17 @@ const useUrlSync = (values, defaults, _setValues, params, setParams, pathname, s
220
301
  newParams.delete(key);
221
302
  }
222
303
  else {
304
+ // oxlint-disable-next-line typescript/no-base-to-string
223
305
  newParams.set(key, String(value));
224
306
  }
225
307
  });
226
308
  return newParams;
227
- }, { replace: !changes.query && !Object.keys(changes).includes('offset') });
309
+ }, { replace: !changes.query && !has(changes, 'offset') });
228
310
  // eslint-disable-next-line react-hooks/exhaustive-deps
229
311
  }, [values]);
230
312
  // URL → State
231
313
  useEffect(() => {
314
+ lastProcessedLocation.current = { pathname, search, routeId };
232
315
  const changes = getStateFromUrl();
233
316
  if (isEmpty(changes)) {
234
317
  return;
@@ -246,19 +329,44 @@ const ParameterProvider = ({ children, defaults: _defaults = {} }) => {
246
329
  const [params, setParams] = useSearchParams();
247
330
  const defaults = useMemo(() => ({ ...DEFAULT_VALUES, ..._defaults }), [_defaults]);
248
331
  const pendingChanges = useRef({});
332
+ const writeTimeout = useRef(null);
249
333
  const [values, _setValues] = useState({
250
334
  selected: getSelectedValue(params, location.pathname, routeParams.id),
251
335
  query: params.get('query') ?? defaults.query,
252
336
  sort: params.get('sort') ?? defaults.sort,
253
337
  span: params.get('span') ?? defaults.span,
338
+ ...getListStateFromUrl(params, defaults),
254
339
  indexes: params.has('index') ? uniq(params.getAll('index')).filter(identity) : defaults.indexes,
255
- filters: params.getAll('filter'),
256
- views: params.getAll('view'),
257
340
  startDate: params.get('start_date'),
258
341
  endDate: params.get('end_date'),
259
342
  offset: parseOffset(params.get('offset')),
260
343
  trackTotalHits: (params.get('track_total_hits') ?? 'false') !== 'false'
261
344
  });
345
+ useLayoutEffect(() => {
346
+ if (writeTimeout.current !== null) {
347
+ clearTimeout(writeTimeout.current);
348
+ writeTimeout.current = null;
349
+ }
350
+ pendingChanges.current = {};
351
+ return () => {
352
+ if (writeTimeout.current !== null) {
353
+ clearTimeout(writeTimeout.current);
354
+ writeTimeout.current = null;
355
+ }
356
+ };
357
+ }, [location.pathname, location.search, routeParams.id]);
358
+ const setValuesImmediately = useCallback(update => {
359
+ if (writeTimeout.current !== null) {
360
+ clearTimeout(writeTimeout.current);
361
+ writeTimeout.current = null;
362
+ }
363
+ const pending = pendingChanges.current;
364
+ pendingChanges.current = {};
365
+ _setValues(current => {
366
+ const withPending = { ...current, ...pending };
367
+ return typeof update === 'function' ? update(withPending) : { ...withPending, ...update };
368
+ });
369
+ }, []);
262
370
  // TODO: SELECTING A BUNDLE STILL CAUSES A FREAKOUT
263
371
  useUrlSync(values, defaults, _setValues, params, setParams, location.pathname, location.search, routeParams.id);
264
372
  const set = useCallback((key) => (value) => {
@@ -275,16 +383,21 @@ const ParameterProvider = ({ children, defaults: _defaults = {} }) => {
275
383
  pendingChanges.current.startDate = null;
276
384
  pendingChanges.current.endDate = null;
277
385
  }
278
- WRITE_THROTTLER.debounce(() => {
279
- _setValues(c => ({ ...c, ...pendingChanges.current }));
386
+ if (writeTimeout.current !== null) {
387
+ clearTimeout(writeTimeout.current);
388
+ }
389
+ writeTimeout.current = setTimeout(() => {
390
+ writeTimeout.current = null;
391
+ const changes = pendingChanges.current;
280
392
  pendingChanges.current = {};
281
- });
393
+ _setValues(c => ({ ...c, ...changes }));
394
+ }, WRITE_DELAY_MS);
282
395
  }, [values, defaults]);
283
- const setOffset = useCallback((_offset) => _setValues(c => ({ ...c, offset: parseOffset(_offset) })), []);
284
- const setCustomSpan = useCallback((startDate, endDate) => _setValues(c => ({ ...c, startDate, endDate })), []);
285
- const filters = useListHandlers('filters', _setValues);
286
- const indexes = useListHandlers('indexes', _setValues);
287
- const views = useListHandlers('views', _setValues);
396
+ const setOffset = useCallback((_offset) => setValuesImmediately(c => ({ ...c, offset: parseOffset(_offset) })), [setValuesImmediately]);
397
+ const setCustomSpan = useCallback((startDate, endDate) => setValuesImmediately(c => ({ ...c, startDate, endDate })), [setValuesImmediately]);
398
+ const filters = useListHandlers('filters', setValuesImmediately);
399
+ const indexes = useListHandlers('indexes', setValuesImmediately);
400
+ const views = useListHandlers('views', setValuesImmediately);
288
401
  return (_jsx(ParameterContext.Provider, { value: {
289
402
  ...values,
290
403
  setOffset,
@@ -296,6 +409,8 @@ const ParameterProvider = ({ children, defaults: _defaults = {} }) => {
296
409
  addFilter: filters.add,
297
410
  removeFilter: filters.remove,
298
411
  setFilter: filters.setAt,
412
+ enableFilter: filters.enable,
413
+ disableFilter: filters.disable,
299
414
  resetFilters: filters.reset,
300
415
  addIndex: indexes.add,
301
416
  removeIndex: indexes.remove,
@@ -305,6 +420,8 @@ const ParameterProvider = ({ children, defaults: _defaults = {} }) => {
305
420
  addView: views.add,
306
421
  removeView: views.remove,
307
422
  setView: views.setAt,
423
+ enableView: views.enable,
424
+ disableView: views.disable,
308
425
  resetViews: views.reset
309
426
  }, children: children }));
310
427
  };