Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Data Grids with the same name to overwrite each others pinned and column-visibility states #2574

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-goats-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/admin": patch
---

`usePersistentColumnState`: Prevent Data Grids with the same name to overwrite each others pinned and column-visibility states
13 changes: 9 additions & 4 deletions packages/admin/admin/src/dataGrid/usePersistentColumnState.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DataGridProps, GridColumnVisibilityModel, useGridApiRef } from "@mui/x-data-grid";
import { MutableRefObject, useCallback, useEffect, useMemo, useState } from "react";
import { useRouteMatch } from "react-router";

import { useStoredState } from "../hooks/useStoredState";
import { GridColDef } from "./GridColDef";
Expand Down Expand Up @@ -55,10 +56,13 @@ type GridProps = Omit<DataGridProps, "rows" | "columns"> & {
export function usePersistentColumnState(stateKey: string): GridProps {
const apiRef = useGridApiRef();
const columns = useGridColumns(apiRef);
const match = useRouteMatch();

const storageKeyPrefix = `${match.path}${stateKey}`;

const mediaQueryColumnVisibilityModel = useVisibilityModelFromColumnMediaQueries(columns);
const [storedColumnVisibilityModel, setStoredColumnVisibilityModel] = useStoredState<GridColumnVisibilityModel>(
`${stateKey}ColumnVisibility`,
`${storageKeyPrefix}ColumnVisibility`,
{},
);

Expand All @@ -80,7 +84,8 @@ export function usePersistentColumnState(stateKey: string): GridProps {
[mediaQueryColumnVisibilityModel, setStoredColumnVisibilityModel],
);

const [pinnedColumns, setPinnedColumns] = useStoredState<GridPinnedColumns>(`${stateKey}PinnedColumns`, {});
const [pinnedColumns, setPinnedColumns] = useStoredState<GridPinnedColumns>(`${storageKeyPrefix}PinnedColumns`, {});

const handlePinnedColumnsChange = useCallback(
(newModel: GridPinnedColumns) => {
setPinnedColumns(newModel);
Expand All @@ -103,7 +108,7 @@ export function usePersistentColumnState(stateKey: string): GridProps {
}, [columns, setPinnedColumns, pinnedColumns]);

//no API for column dimensions as controlled state, export on change instead
const columnDimensionsKey = `${stateKey}ColumnDimensions`;
const columnDimensionsKey = `${storageKeyPrefix}ColumnDimensions`;
const initialColumnDimensions = useMemo(() => {
const serializedState = window.localStorage.getItem(columnDimensionsKey);
return serializedState ? JSON.parse(serializedState) : undefined;
Expand All @@ -115,7 +120,7 @@ export function usePersistentColumnState(stateKey: string): GridProps {
}, [columnDimensionsKey, apiRef]);

//no API for column order as controlled state, export on change instead
const columnOrderKey = `${stateKey}ColumnOrder`;
const columnOrderKey = `${storageKeyPrefix}ColumnOrder`;
const initialColumnOrder = useMemo(() => {
const serializedState = window.localStorage.getItem(columnOrderKey);
return serializedState ? JSON.parse(serializedState) : undefined;
Expand Down
Loading