feat: removed swr caching

This commit is contained in:
TrèsAbhi committed 2023-11-13 23:00:57 -06:00
1 parent 9e6343a111
commit c742c0725d
3 files changed
+31 -24

No files matched your search

-1
View File
@@ -49,7 +49,6 @@
"react-dom": "^18.2.0",
"react-fuzzysort": "^1.0.1",
"satori": "^0.10.8",
"swr": "^2.2.4",
"vite-node": "^0.34.5",
"vitepress": "^1.0.0-rc.20",
"vitepress-plugin-auto-sidebar": "^1.1.0",
+29 -13
View File
@@ -13,10 +13,9 @@ import {
import { produce } from 'immer';
import { debounce, range } from 'lodash';
import { ChangeEvent, useEffect, useRef, useState } from 'react';
import useSWR from 'swr';
import { create } from 'zustand';
import { BlitzkriegRatingsLeaderboardEntry } from '../../../../scripts/buildRatingsLeaderboard';
import { RatingsPlayer } from '../../../commands/ratings';
import { RatingsInfo, RatingsPlayer } from '../../../commands/ratings';
import * as Leaderboard from '../../../components/Leaderboard';
import PageWrapper from '../../../components/PageWrapper';
import { LEAGUES } from '../../../constants/leagues';
@@ -50,6 +49,7 @@ type PlayersCache = Record<
Region,
Record<number, Record<number, BlitzkriegRatingsLeaderboardEntry>>
>;
type InfoCache = Record<Region, Record<number, RatingsInfo | undefined>>;
const useNameCache = create<NameCache>(() => ({
asia: {},
@@ -61,6 +61,11 @@ const usePlayersCache = create<PlayersCache>(() => ({
com: {},
eu: {},
}));
const useInfoCache = create<InfoCache>(() => ({
asia: {},
com: {},
eu: {},
}));
export default function Page() {
const [region, setRegion] = useState<Region>('com');
@@ -80,17 +85,10 @@ export default function Page() {
{ type: 'id'; id: number } | { type: 'position'; position: number } | null
>(null);
const { data: ratingsInfo } = useSWR(
`ratings-info-${region}-${season}`,
() =>
season === 0
? getRatingsInfo(region)
: getArchivedRatingsInfo(region, season),
);
const { data: latestArchivedSeasonNumber } = useSWR<number>(
getArchivedLatestSeasonNumber.name,
getArchivedLatestSeasonNumber,
);
const ratingsInfo = useInfoCache()[region][season];
const [latestArchivedSeasonNumber, setLatestArchivedSeasonNumber] = useState<
number | null
>(null);
const names = useNameCache();
const players = usePlayersCache();
@@ -107,6 +105,18 @@ export default function Page() {
// page reset on season or region change
useEffect(() => {
setPage(0);
(async () => {
const newInfo =
season === 0
? await getRatingsInfo(region)
: await getArchivedRatingsInfo(region, season);
useInfoCache.setState(
produce((draft: InfoCache) => {
draft[region][season] = newInfo;
}),
);
})();
}, [season, region]);
// initial leagues caching for current season
useEffect(() => {
@@ -235,6 +245,12 @@ export default function Page() {
ratingsInfo?.detail,
players[region][season] !== undefined && 0 in players[region][season],
]);
// latest archived season number caching
useEffect(() => {}, [
getArchivedLatestSeasonNumber().then((result) => {
setLatestArchivedSeasonNumber(result);
}),
]);
async function cacheNeighbors(
id: number,
+2 -10
View File
@@ -2518,7 +2518,7 @@ clean-stack@^2.0.0:
resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-2.2.0.tgz#ee8472dbb129e727b31e8a10a427dee9dfe4008b"
integrity sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==
[email protected], client-only@^0.0.1:
[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1"
integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==
@@ -4302,14 +4302,6 @@ supports-color@^7.1.0:
dependencies:
has-flag "^4.0.0"
swr@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/swr/-/swr-2.2.4.tgz#03ec4c56019902fbdc904d78544bd7a9a6fa3f07"
integrity sha512-njiZ/4RiIhoOlAaLYDqwz5qH/KZXVilRLvomrx83HjzCWTfa+InyfAjv05PSFxnmLzZkNO9ZfvgoqzAaEI4sGQ==
dependencies:
client-only "^0.0.1"
use-sync-external-store "^1.2.0"
tabbable@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/tabbable/-/tabbable-6.2.0.tgz#732fb62bc0175cfcec257330be187dcfba1f3b97"
@@ -4470,7 +4462,7 @@ use-sidecar@^1.1.2:
detect-node-es "^1.1.0"
tslib "^2.0.0"
[email protected], use-sync-external-store@^1.2.0:
[email protected]:
version "1.2.0"
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a"
integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==