analytica-frontend-lib 1.2.8 → 1.2.9
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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6512,6 +6512,75 @@ import {
|
|
|
6512
6512
|
} from "react";
|
|
6513
6513
|
import { CaretUp, CaretDown } from "phosphor-react";
|
|
6514
6514
|
import { jsx as jsx39, jsxs as jsxs28 } from "react/jsx-runtime";
|
|
6515
|
+
function useTableSort(data, options = {}) {
|
|
6516
|
+
const { syncWithUrl = false } = options;
|
|
6517
|
+
const getInitialState = () => {
|
|
6518
|
+
if (!syncWithUrl || globalThis.window === void 0) {
|
|
6519
|
+
return { column: null, direction: null };
|
|
6520
|
+
}
|
|
6521
|
+
const params = new URLSearchParams(globalThis.location.search);
|
|
6522
|
+
const sortBy = params.get("sortBy");
|
|
6523
|
+
const sort = params.get("sort");
|
|
6524
|
+
if (sortBy && sort && (sort === "ASC" || sort === "DESC")) {
|
|
6525
|
+
return {
|
|
6526
|
+
column: sortBy,
|
|
6527
|
+
direction: sort.toLowerCase()
|
|
6528
|
+
};
|
|
6529
|
+
}
|
|
6530
|
+
return { column: null, direction: null };
|
|
6531
|
+
};
|
|
6532
|
+
const initialState = getInitialState();
|
|
6533
|
+
const [sortColumn, setSortColumn] = useState13(
|
|
6534
|
+
initialState.column
|
|
6535
|
+
);
|
|
6536
|
+
const [sortDirection, setSortDirection] = useState13(
|
|
6537
|
+
initialState.direction
|
|
6538
|
+
);
|
|
6539
|
+
useEffect13(() => {
|
|
6540
|
+
if (!syncWithUrl || globalThis.window === void 0) return;
|
|
6541
|
+
const url = new URL(globalThis.location.href);
|
|
6542
|
+
const params = url.searchParams;
|
|
6543
|
+
if (sortColumn && sortDirection) {
|
|
6544
|
+
params.set("sortBy", sortColumn);
|
|
6545
|
+
params.set("sort", sortDirection.toUpperCase());
|
|
6546
|
+
} else {
|
|
6547
|
+
params.delete("sortBy");
|
|
6548
|
+
params.delete("sort");
|
|
6549
|
+
}
|
|
6550
|
+
globalThis.history.replaceState({}, "", url.toString());
|
|
6551
|
+
}, [sortColumn, sortDirection, syncWithUrl]);
|
|
6552
|
+
const handleSort = (column) => {
|
|
6553
|
+
if (sortColumn === column) {
|
|
6554
|
+
if (sortDirection === "asc") {
|
|
6555
|
+
setSortDirection("desc");
|
|
6556
|
+
} else if (sortDirection === "desc") {
|
|
6557
|
+
setSortColumn(null);
|
|
6558
|
+
setSortDirection(null);
|
|
6559
|
+
}
|
|
6560
|
+
} else {
|
|
6561
|
+
setSortColumn(column);
|
|
6562
|
+
setSortDirection("asc");
|
|
6563
|
+
}
|
|
6564
|
+
};
|
|
6565
|
+
const sortedData = useMemo5(() => {
|
|
6566
|
+
if (!sortColumn || !sortDirection) {
|
|
6567
|
+
return data;
|
|
6568
|
+
}
|
|
6569
|
+
return [...data].sort((a, b) => {
|
|
6570
|
+
const aValue = a[sortColumn];
|
|
6571
|
+
const bValue = b[sortColumn];
|
|
6572
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
6573
|
+
const comparison = aValue.localeCompare(bValue);
|
|
6574
|
+
return sortDirection === "asc" ? comparison : -comparison;
|
|
6575
|
+
}
|
|
6576
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
6577
|
+
return sortDirection === "asc" ? aValue - bValue : bValue - aValue;
|
|
6578
|
+
}
|
|
6579
|
+
return 0;
|
|
6580
|
+
});
|
|
6581
|
+
}, [data, sortColumn, sortDirection]);
|
|
6582
|
+
return { sortedData, sortColumn, sortDirection, handleSort };
|
|
6583
|
+
}
|
|
6515
6584
|
var Table = forwardRef14(
|
|
6516
6585
|
({ variant = "default", className, children, ...props }, ref) => /* @__PURE__ */ jsx39(
|
|
6517
6586
|
"div",
|
|
@@ -14149,6 +14218,7 @@ export {
|
|
|
14149
14218
|
useMobile,
|
|
14150
14219
|
useQuizStore,
|
|
14151
14220
|
useRouteAuth,
|
|
14221
|
+
useTableSort,
|
|
14152
14222
|
useTheme,
|
|
14153
14223
|
useThemeStore,
|
|
14154
14224
|
ToastStore_default as useToastStore,
|