-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #921 from WatWowMap/available-more-up-to-date
feat: rework how often available is updated
- Loading branch information
Showing
10 changed files
with
141 additions
and
38 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
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,46 @@ | ||
// @ts-check | ||
import { useEffect, useMemo } from 'react' | ||
import { useQuery } from '@apollo/client' | ||
import * as queries from '@services/queries/available' | ||
import { capitalize } from '@mui/material' | ||
|
||
import { useMemory } from './useMemory' | ||
|
||
/** | ||
* @param {keyof import('packages/types/lib').Available} category | ||
* @returns {{available: string[], loading: boolean, error: import('@apollo/client').ApolloError}} | ||
*/ | ||
export default function useGetAvailable(category) { | ||
const capitalized = capitalize(category) | ||
const active = useMemory((s) => s.active) | ||
const online = useMemory((s) => s.online) | ||
|
||
/** @type {import('@apollo/client').QueryResult<{ [key: string]: string[] }>} */ | ||
const { data, previousData, loading, error } = useQuery( | ||
queries[`getAvailable${capitalized}`], | ||
{ | ||
fetchPolicy: active && online ? 'network-only' : 'cache-and-network', | ||
}, | ||
) | ||
|
||
useEffect(() => { | ||
if (data?.[`available${capitalized}`]) { | ||
useMemory.setState((prev) => ({ | ||
available: { | ||
...prev.available, | ||
[category]: data[`available${capitalized}`].some( | ||
(key, i) => key !== prev.available[category][i], | ||
) | ||
? data[`available${capitalized}`] | ||
: prev.available[category], | ||
// if it's the same, don't cause re-renders | ||
}, | ||
})) | ||
} | ||
}, [data]) | ||
|
||
return useMemo(() => { | ||
const available = (data || previousData)?.[`available${capitalized}`] || [] | ||
return { available, loading, error } | ||
}, [data, previousData, loading, error]) | ||
} |
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