@trackunit/filters-filter-bar 1.0.64 → 1.0.68
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.
- package/index.cjs.js +61 -0
- package/index.esm.js +62 -2
- package/package.json +10 -10
- package/src/lib/hooks/useSearchParamAsFilter.d.ts +23 -0
- package/src/lib/index.d.ts +1 -0
package/index.cjs.js
CHANGED
|
@@ -1491,6 +1491,66 @@ const getKeysOfUnequalProperties = (obj1, obj2) => {
|
|
|
1491
1491
|
}, []);
|
|
1492
1492
|
};
|
|
1493
1493
|
|
|
1494
|
+
/**
|
|
1495
|
+
* Handles the application of a URL search parameter as a filter in the system.
|
|
1496
|
+
* The parameter is validated using the provided Zod schema.
|
|
1497
|
+
*
|
|
1498
|
+
* @template T - A Zod schema used for validation.
|
|
1499
|
+
* @param {string} searchParamName - The name of the search parameter in the URL.
|
|
1500
|
+
* @param {string} filterName - The name of the filter.
|
|
1501
|
+
* @param {object} search - The current search state, provided by `useSearch` from @tanstack/react-router.
|
|
1502
|
+
* @param {T} zodSchema - The Zod schema for validating the filter data.
|
|
1503
|
+
* @param {ErrorHandlingContextValue} errorHandler - Error handling context for capturing and reporting exceptions.
|
|
1504
|
+
* @returns {any} The parsed filter data if validation succeeds, otherwise `null`.
|
|
1505
|
+
*/
|
|
1506
|
+
const useSearchParamAsFilter = ({ searchParamName, filterName, search, zodSchema, errorHandler, }) => {
|
|
1507
|
+
return react.useMemo(() => {
|
|
1508
|
+
if (sharedUtils.objectKeys(search).includes(searchParamName)) {
|
|
1509
|
+
const foundParam = search[searchParamName];
|
|
1510
|
+
try {
|
|
1511
|
+
let jsonParsed;
|
|
1512
|
+
if (zodSchema._def.typeName === "ZodString") {
|
|
1513
|
+
jsonParsed = foundParam ? foundParam + "" : foundParam;
|
|
1514
|
+
}
|
|
1515
|
+
else {
|
|
1516
|
+
if (typeof search === "string" && typeof foundParam === "string") {
|
|
1517
|
+
jsonParsed = JSON.parse(foundParam);
|
|
1518
|
+
}
|
|
1519
|
+
else {
|
|
1520
|
+
jsonParsed = foundParam;
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
const zodParsed = zodSchema.safeParse(jsonParsed);
|
|
1524
|
+
if (zodParsed.success) {
|
|
1525
|
+
return zodParsed.data;
|
|
1526
|
+
}
|
|
1527
|
+
else {
|
|
1528
|
+
captureUrlParseException(errorHandler, {
|
|
1529
|
+
filterName,
|
|
1530
|
+
param: typeof foundParam === "string" ? foundParam : JSON.stringify(foundParam),
|
|
1531
|
+
jsonParsed: jsonParsed,
|
|
1532
|
+
zodParseError: zodParsed.error,
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
catch (e) {
|
|
1537
|
+
captureUrlParseException(errorHandler, {
|
|
1538
|
+
filterName,
|
|
1539
|
+
param: typeof foundParam === "string" ? foundParam : JSON.stringify(foundParam),
|
|
1540
|
+
jsonParsed: "parse json error",
|
|
1541
|
+
zodParseError: null,
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
return null;
|
|
1546
|
+
}, [search, searchParamName, zodSchema, errorHandler, filterName]);
|
|
1547
|
+
};
|
|
1548
|
+
const captureUrlParseException = (errorHandler, { filterName, param, jsonParsed: parsed, }) => errorHandler.captureException(new Error(JSON.stringify({
|
|
1549
|
+
info: `Received invalid values for ${filterName} from URL query params. Can't set ${filterName} filter with this. Please fix the URL query params (or schema).`,
|
|
1550
|
+
param,
|
|
1551
|
+
parsed,
|
|
1552
|
+
})));
|
|
1553
|
+
|
|
1494
1554
|
/**
|
|
1495
1555
|
* Merges additional filters into an existing filter bar definition.
|
|
1496
1556
|
*
|
|
@@ -1535,4 +1595,5 @@ exports.mergeFilters = mergeFilters;
|
|
|
1535
1595
|
exports.mockFilterBar = mockFilterBar;
|
|
1536
1596
|
exports.toggleFilterValue = toggleFilterValue;
|
|
1537
1597
|
exports.useFilterBar = useFilterBar;
|
|
1598
|
+
exports.useSearchParamAsFilter = useSearchParamAsFilter;
|
|
1538
1599
|
exports.validateFilter = validateFilter;
|
package/index.esm.js
CHANGED
|
@@ -8,7 +8,7 @@ import { capitalize } from 'string-ts';
|
|
|
8
8
|
import { createEvent } from '@trackunit/react-core-contexts-api';
|
|
9
9
|
import { Search, NumberField, RadioGroup, Toggle } from '@trackunit/react-form-components';
|
|
10
10
|
import { DayRangePicker } from '@trackunit/react-date-and-time-components';
|
|
11
|
-
import { nonNullable, capitalize as capitalize$1, objectValues, truthy } from '@trackunit/shared-utils';
|
|
11
|
+
import { nonNullable, capitalize as capitalize$1, objectValues, truthy, objectKeys } from '@trackunit/shared-utils';
|
|
12
12
|
import { twMerge } from 'tailwind-merge';
|
|
13
13
|
import { dequal } from 'dequal';
|
|
14
14
|
import isEqual from 'lodash/isEqual';
|
|
@@ -1489,6 +1489,66 @@ const getKeysOfUnequalProperties = (obj1, obj2) => {
|
|
|
1489
1489
|
}, []);
|
|
1490
1490
|
};
|
|
1491
1491
|
|
|
1492
|
+
/**
|
|
1493
|
+
* Handles the application of a URL search parameter as a filter in the system.
|
|
1494
|
+
* The parameter is validated using the provided Zod schema.
|
|
1495
|
+
*
|
|
1496
|
+
* @template T - A Zod schema used for validation.
|
|
1497
|
+
* @param {string} searchParamName - The name of the search parameter in the URL.
|
|
1498
|
+
* @param {string} filterName - The name of the filter.
|
|
1499
|
+
* @param {object} search - The current search state, provided by `useSearch` from @tanstack/react-router.
|
|
1500
|
+
* @param {T} zodSchema - The Zod schema for validating the filter data.
|
|
1501
|
+
* @param {ErrorHandlingContextValue} errorHandler - Error handling context for capturing and reporting exceptions.
|
|
1502
|
+
* @returns {any} The parsed filter data if validation succeeds, otherwise `null`.
|
|
1503
|
+
*/
|
|
1504
|
+
const useSearchParamAsFilter = ({ searchParamName, filterName, search, zodSchema, errorHandler, }) => {
|
|
1505
|
+
return useMemo(() => {
|
|
1506
|
+
if (objectKeys(search).includes(searchParamName)) {
|
|
1507
|
+
const foundParam = search[searchParamName];
|
|
1508
|
+
try {
|
|
1509
|
+
let jsonParsed;
|
|
1510
|
+
if (zodSchema._def.typeName === "ZodString") {
|
|
1511
|
+
jsonParsed = foundParam ? foundParam + "" : foundParam;
|
|
1512
|
+
}
|
|
1513
|
+
else {
|
|
1514
|
+
if (typeof search === "string" && typeof foundParam === "string") {
|
|
1515
|
+
jsonParsed = JSON.parse(foundParam);
|
|
1516
|
+
}
|
|
1517
|
+
else {
|
|
1518
|
+
jsonParsed = foundParam;
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
const zodParsed = zodSchema.safeParse(jsonParsed);
|
|
1522
|
+
if (zodParsed.success) {
|
|
1523
|
+
return zodParsed.data;
|
|
1524
|
+
}
|
|
1525
|
+
else {
|
|
1526
|
+
captureUrlParseException(errorHandler, {
|
|
1527
|
+
filterName,
|
|
1528
|
+
param: typeof foundParam === "string" ? foundParam : JSON.stringify(foundParam),
|
|
1529
|
+
jsonParsed: jsonParsed,
|
|
1530
|
+
zodParseError: zodParsed.error,
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
catch (e) {
|
|
1535
|
+
captureUrlParseException(errorHandler, {
|
|
1536
|
+
filterName,
|
|
1537
|
+
param: typeof foundParam === "string" ? foundParam : JSON.stringify(foundParam),
|
|
1538
|
+
jsonParsed: "parse json error",
|
|
1539
|
+
zodParseError: null,
|
|
1540
|
+
});
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
return null;
|
|
1544
|
+
}, [search, searchParamName, zodSchema, errorHandler, filterName]);
|
|
1545
|
+
};
|
|
1546
|
+
const captureUrlParseException = (errorHandler, { filterName, param, jsonParsed: parsed, }) => errorHandler.captureException(new Error(JSON.stringify({
|
|
1547
|
+
info: `Received invalid values for ${filterName} from URL query params. Can't set ${filterName} filter with this. Please fix the URL query params (or schema).`,
|
|
1548
|
+
param,
|
|
1549
|
+
parsed,
|
|
1550
|
+
})));
|
|
1551
|
+
|
|
1492
1552
|
/**
|
|
1493
1553
|
* Merges additional filters into an existing filter bar definition.
|
|
1494
1554
|
*
|
|
@@ -1510,4 +1570,4 @@ const mergeFilters = (filterBarDefinition, extraFilters) => {
|
|
|
1510
1570
|
*/
|
|
1511
1571
|
setupLibraryTranslations();
|
|
1512
1572
|
|
|
1513
|
-
export { DefaultCheckboxFilter, DefaultDateRangeFilter, DefaultMinMaxFilter, DefaultRadioFilter, DynamicFilterList, FilterBar, FilterEvents, FilterHeader, FilterResults, StarredFilters, areaFilterGeoJsonGeometrySchema, isAreaFilterValue, isArrayFilterValue, isBooleanValue, isDateRangeValue, isMinMaxFilterValue, isStringArrayFilterValue, isValueName, isValueNameArray, mergeFilters, mockFilterBar, toggleFilterValue, useFilterBar, validateFilter };
|
|
1573
|
+
export { DefaultCheckboxFilter, DefaultDateRangeFilter, DefaultMinMaxFilter, DefaultRadioFilter, DynamicFilterList, FilterBar, FilterEvents, FilterHeader, FilterResults, StarredFilters, areaFilterGeoJsonGeometrySchema, isAreaFilterValue, isArrayFilterValue, isBooleanValue, isDateRangeValue, isMinMaxFilterValue, isStringArrayFilterValue, isValueName, isValueNameArray, mergeFilters, mockFilterBar, toggleFilterValue, useFilterBar, useSearchParamAsFilter, validateFilter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/filters-filter-bar",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.68",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -14,15 +14,15 @@
|
|
|
14
14
|
"tailwind-merge": "^2.0.0",
|
|
15
15
|
"string-ts": "^2.0.0",
|
|
16
16
|
"zod": "3.22.4",
|
|
17
|
-
"@trackunit/react-components": "1.1.
|
|
18
|
-
"@trackunit/react-core-hooks": "1.0.
|
|
19
|
-
"@trackunit/react-filter-components": "1.0.
|
|
20
|
-
"@trackunit/react-date-and-time-components": "1.0.
|
|
21
|
-
"@trackunit/shared-utils": "1.2.
|
|
22
|
-
"@trackunit/react-form-components": "1.0.
|
|
23
|
-
"@trackunit/react-core-contexts-api": "1.0.
|
|
24
|
-
"@trackunit/geo-json-utils": "1.0.
|
|
25
|
-
"@trackunit/i18n-library-translation": "1.0.
|
|
17
|
+
"@trackunit/react-components": "1.1.47",
|
|
18
|
+
"@trackunit/react-core-hooks": "1.0.45",
|
|
19
|
+
"@trackunit/react-filter-components": "1.0.68",
|
|
20
|
+
"@trackunit/react-date-and-time-components": "1.0.66",
|
|
21
|
+
"@trackunit/shared-utils": "1.2.37",
|
|
22
|
+
"@trackunit/react-form-components": "1.0.66",
|
|
23
|
+
"@trackunit/react-core-contexts-api": "1.0.45",
|
|
24
|
+
"@trackunit/geo-json-utils": "1.0.41",
|
|
25
|
+
"@trackunit/i18n-library-translation": "1.0.50"
|
|
26
26
|
},
|
|
27
27
|
"module": "./index.esm.js",
|
|
28
28
|
"main": "./index.cjs.js",
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ErrorHandlingContextValue } from "@trackunit/react-core-contexts-api";
|
|
2
|
+
import { ZodTypeAny } from "zod";
|
|
3
|
+
interface SearchParamAsFilterProps<T> {
|
|
4
|
+
searchParamName: string;
|
|
5
|
+
filterName: string;
|
|
6
|
+
search: Record<PropertyKey, unknown>;
|
|
7
|
+
zodSchema: T;
|
|
8
|
+
errorHandler: ErrorHandlingContextValue;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Handles the application of a URL search parameter as a filter in the system.
|
|
12
|
+
* The parameter is validated using the provided Zod schema.
|
|
13
|
+
*
|
|
14
|
+
* @template T - A Zod schema used for validation.
|
|
15
|
+
* @param {string} searchParamName - The name of the search parameter in the URL.
|
|
16
|
+
* @param {string} filterName - The name of the filter.
|
|
17
|
+
* @param {object} search - The current search state, provided by `useSearch` from @tanstack/react-router.
|
|
18
|
+
* @param {T} zodSchema - The Zod schema for validating the filter data.
|
|
19
|
+
* @param {ErrorHandlingContextValue} errorHandler - Error handling context for capturing and reporting exceptions.
|
|
20
|
+
* @returns {any} The parsed filter data if validation succeeds, otherwise `null`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const useSearchParamAsFilter: <T extends ZodTypeAny>({ searchParamName, filterName, search, zodSchema, errorHandler, }: SearchParamAsFilterProps<T>) => any;
|
|
23
|
+
export {};
|
package/src/lib/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./components";
|
|
|
2
2
|
export * from "./FilterBar";
|
|
3
3
|
export * from "./hooks/mockFilterBar";
|
|
4
4
|
export * from "./hooks/useFilterBar";
|
|
5
|
+
export * from "./hooks/useSearchParamAsFilter";
|
|
5
6
|
export * from "./types/FilterTypes";
|
|
6
7
|
export * from "./utils/FilterEvents";
|
|
7
8
|
export * from "./utils/mergeFilters";
|