mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
removed lab reducer
This commit is contained in:
parent
e24ea4afeb
commit
2b7b756212
26 changed files with 242 additions and 369 deletions
72
src/hooks/lab/useGetLabStats.ts
Normal file
72
src/hooks/lab/useGetLabStats.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import useSWR from 'swr';
|
||||
import { API } from '~/constants/api';
|
||||
import { getLabStats, getLabUpdates } from '~/api/lab';
|
||||
import { useLabStore } from '~/store/lab/useLabStore';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { useUser } from '~/hooks/user/userUser';
|
||||
|
||||
const refreshInterval = 1000 * 60 * 5; // 5 minutes
|
||||
|
||||
export const useGetLabStats = () => {
|
||||
const lab = useLabStore();
|
||||
const { is_user } = useUser();
|
||||
|
||||
const { data: stats, isValidating: isValidatingStats } = useSWR(
|
||||
is_user ? API.LAB.STATS : null,
|
||||
async () => getLabStats(),
|
||||
{
|
||||
fallbackData: {
|
||||
heroes: lab.heroes,
|
||||
tags: lab.tags,
|
||||
},
|
||||
onSuccess: data => {
|
||||
lab.setHeroes(data.heroes);
|
||||
lab.setTags(data.tags);
|
||||
},
|
||||
refreshInterval,
|
||||
}
|
||||
);
|
||||
|
||||
const { data: updatesData, isValidating: isValidatingUpdates, mutate: mutateUpdates } = useSWR(
|
||||
is_user ? API.LAB.UPDATES : null,
|
||||
async () => {
|
||||
const result = await getLabUpdates();
|
||||
return result.nodes;
|
||||
},
|
||||
{
|
||||
fallbackData: lab.updates,
|
||||
onSuccess: data => {
|
||||
lab.setUpdates(data);
|
||||
},
|
||||
refreshInterval,
|
||||
}
|
||||
);
|
||||
|
||||
const heroes = stats?.heroes || [];
|
||||
const tags = stats?.tags || [];
|
||||
const updates = updatesData || [];
|
||||
|
||||
const isLoading = (!stats || !updates) && (isValidatingStats || isValidatingUpdates);
|
||||
const seenNode = useCallback(
|
||||
async (nodeId: number) => {
|
||||
await mutateUpdates(
|
||||
updates.filter(it => it.id !== nodeId),
|
||||
false
|
||||
);
|
||||
},
|
||||
[mutateUpdates, updates]
|
||||
);
|
||||
|
||||
/** purge cache on exit */
|
||||
useEffect(() => {
|
||||
if (is_user) {
|
||||
return;
|
||||
}
|
||||
|
||||
lab.setHeroes([]);
|
||||
lab.setTags([]);
|
||||
lab.setUpdates([]);
|
||||
}, [is_user, lab]);
|
||||
|
||||
return { heroes, tags, updates, isLoading, seenNode };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue