mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-10 23:16:38 +00:00
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import type { VisibilityState } from "@tanstack/react-table";
|
|
import { useCallback, useState } from "react";
|
|
|
|
const STORAGE_KEYS = {
|
|
COLUMN_VISIBILITY: "datatable-column-visibility",
|
|
getTableColumnVisibility: (tableId: string) =>
|
|
`datatable-${tableId}-column-visibility`
|
|
};
|
|
|
|
const getStoredColumnVisibility = (
|
|
tableId: string,
|
|
defaultVisibility?: Record<string, boolean>
|
|
): Record<string, boolean> => {
|
|
if (typeof window === "undefined") return defaultVisibility || {};
|
|
|
|
try {
|
|
const key = STORAGE_KEYS.getTableColumnVisibility(tableId);
|
|
const stored = localStorage.getItem(key);
|
|
if (stored) {
|
|
const parsed = JSON.parse(stored);
|
|
// Validate that it's an object
|
|
if (typeof parsed === "object" && parsed !== null) {
|
|
return parsed;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.warn(
|
|
"Failed to read column visibility from localStorage:",
|
|
error
|
|
);
|
|
}
|
|
return defaultVisibility || {};
|
|
};
|
|
|
|
const setStoredColumnVisibility = (
|
|
visibility: Record<string, boolean>,
|
|
tableId: string
|
|
): void => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
try {
|
|
const key = STORAGE_KEYS.getTableColumnVisibility(tableId);
|
|
localStorage.setItem(key, JSON.stringify(visibility));
|
|
} catch (error) {
|
|
console.warn(
|
|
"Failed to save column visibility to localStorage:",
|
|
error
|
|
);
|
|
}
|
|
};
|
|
|
|
export function useStoredColumnVisibility(
|
|
tableId: string,
|
|
defaultColumnVisibility?: Record<string, boolean>
|
|
) {
|
|
const [columnVisibility, setVisibility] = useState<VisibilityState>(() =>
|
|
getStoredColumnVisibility(tableId, defaultColumnVisibility)
|
|
);
|
|
|
|
const setColumnVisibility = useCallback(
|
|
(
|
|
updaterOrValue:
|
|
| VisibilityState
|
|
| ((old: VisibilityState) => VisibilityState)
|
|
) => {
|
|
if (typeof updaterOrValue === "function") {
|
|
setVisibility((oldValue) => {
|
|
const newValue = updaterOrValue(oldValue);
|
|
setStoredColumnVisibility(newValue, tableId);
|
|
return newValue;
|
|
});
|
|
} else {
|
|
setVisibility(updaterOrValue);
|
|
setStoredColumnVisibility(updaterOrValue, tableId);
|
|
}
|
|
},
|
|
[tableId]
|
|
);
|
|
|
|
return [columnVisibility, setColumnVisibility] as const;
|
|
}
|