-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(listing-details): add and implements query inconsistency invalid…
…ator hook add hook to cache bust in case of inconsistency between listing and details closes #833 ```
- Loading branch information
Showing
11 changed files
with
223 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import type { UseNavigateResult } from '@tanstack/react-router'; | ||
import { useEffect } from 'react'; | ||
|
||
import { deepCompare } from '@/utils/records'; | ||
|
||
type ReferenceTable = | ||
| Record<string, Record<string, number> | undefined> | ||
| undefined; | ||
|
||
type QueryInconsistencyInvalidatorArgs<T extends ReferenceTable> = { | ||
referenceData?: T; | ||
comparedData?: T; | ||
enabled?: boolean; | ||
navigate: UseNavigateResult<'/tree/$treeId' | '/hardware/$hardwareId'>; | ||
}; | ||
|
||
export const useQueryInconsistencyInvalidator = <T extends ReferenceTable>({ | ||
referenceData: referenceData, | ||
comparedData: comparedData, | ||
navigate, | ||
enabled = true, | ||
}: QueryInconsistencyInvalidatorArgs<T>): void => { | ||
const queryClient = useQueryClient(); | ||
useEffect(() => { | ||
if (!enabled || !referenceData || !comparedData) { | ||
return; | ||
} | ||
|
||
const shouldInvalidate = !deepCompare(referenceData, comparedData); | ||
|
||
if (shouldInvalidate) { | ||
queryClient.invalidateQueries().then(() => { | ||
navigate({ | ||
search: s => s, | ||
state: s => { | ||
return { | ||
...s, | ||
treeStatusCount: undefined, | ||
hardwareStatusCount: undefined, | ||
}; | ||
}, | ||
}); | ||
}); | ||
} | ||
}, [referenceData, comparedData, queryClient, navigate, enabled]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const isRecord = (obj: unknown): obj is Record<string, unknown> => { | ||
return typeof obj === 'object' && !Array.isArray(obj); | ||
}; | ||
|
||
export const deepCompare = ( | ||
obj1: Record<string, unknown>, | ||
obj2: Record<string, unknown>, | ||
): boolean => { | ||
if (Object.keys(obj1).length !== Object.keys(obj2).length) { | ||
return false; | ||
} | ||
|
||
for (const key in obj1) { | ||
if (isRecord(obj1[key]) && isRecord(obj2[key])) { | ||
if (!deepCompare(obj1[key], obj2[key])) { | ||
return false; | ||
} | ||
} else if (obj1[key] !== obj2[key]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; |