-
Notifications
You must be signed in to change notification settings - Fork 3
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
Use Session Storage to hold temp state for DataGrid #1618
base: develop
Are you sure you want to change the base?
Changes from all commits
04597ce
9302418
7f2e4f5
a479c21
cf430c7
9bae058
9c8b06f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { GridApiPro, GridEventListener } from '@mui/x-data-grid-pro'; | ||
import { GridInitialStatePro } from '@mui/x-data-grid-pro/models/gridStatePro'; | ||
import { useDebounceFn, usePrevious, useSessionStorageState } from 'ahooks'; | ||
import { isEqual } from 'lodash'; | ||
import { MutableRefObject, useEffect, useRef } from 'react'; | ||
import { convertMuiFiltersToApi } from '~/components/Grid/convertMuiFiltersToApi'; | ||
|
||
interface UsePersistedGridStateOptions { | ||
key: string; | ||
apiRef: MutableRefObject<GridApiPro>; | ||
defaultValue: GridInitialStatePro; | ||
} | ||
|
||
export const usePersistedGridState = ({ | ||
key, | ||
apiRef, | ||
defaultValue, | ||
}: UsePersistedGridStateOptions) => { | ||
const isRestoringState = useRef(true); | ||
|
||
const [savedGridState, setSavedGridState] = useSessionStorageState(key, { | ||
defaultValue, | ||
}); | ||
|
||
const [persistedFilterModel, setPersistedFilterModel] = | ||
useSessionStorageState<Record<string, any>>(`${key}-api-filter`, {}); | ||
|
||
const prevGridState = usePrevious( | ||
savedGridState, | ||
(prev, next) => !isEqual(prev, next) | ||
); | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect usage of the compare function in The compare function in |
||
|
||
const { run: handleStateChange } = useDebounceFn( | ||
() => { | ||
const gridState = apiRef.current.exportState(); | ||
|
||
setPersistedFilterModel((prev) => | ||
isEqual(prev, gridState) | ||
? prev | ||
: convertMuiFiltersToApi( | ||
apiRef.current, | ||
gridState.filter?.filterModel | ||
) | ||
); | ||
setSavedGridState((prev) => | ||
isEqual(prev, gridState) ? prev || defaultValue : gridState | ||
); | ||
}, | ||
{ wait: 500, maxWait: 500 } | ||
); | ||
|
||
const onStateChange: GridEventListener<'stateChange'> = () => { | ||
handleStateChange(); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isRestoringState.current) { | ||
isRestoringState.current = false; | ||
} else if (savedGridState && !isEqual(savedGridState, prevGridState)) { | ||
apiRef.current.restoreState(savedGridState); | ||
} | ||
}, [savedGridState, apiRef, prevGridState]); | ||
|
||
return [savedGridState, onStateChange, persistedFilterModel] as const; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Remove redundant Boolean cast
As flagged by static analysis, the Boolean cast is redundant since the expression will already be coerced to a boolean.
Apply this change:
📝 Committable suggestion
🧰 Tools
🪛 Biome
[error] 292-292: Avoid redundant
Boolean
callIt is not necessary to use
Boolean
call when a value will already be coerced to a boolean.Unsafe fix: Remove redundant
Boolean
call(lint/complexity/noExtraBooleanCast)