mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import useSWR from 'swr';
|
|
import { API } from '~/constants/api';
|
|
import { getLabStats, getLabUpdates } from '~/api/lab';
|
|
import { useLabStore } from '~/store/lab/useLabStore';
|
|
import { useCallback, useMemo } from 'react';
|
|
import { useAuth } from '~/hooks/auth/useAuth';
|
|
|
|
const refreshInterval = 1000 * 60 * 5; // 5 minutes
|
|
|
|
export const useGetLabStats = () => {
|
|
const lab = useLabStore();
|
|
const { isUser } = useAuth();
|
|
|
|
const { data: stats, isValidating: isValidatingStats } = useSWR(
|
|
isUser ? 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(
|
|
isUser ? API.LAB.UPDATES : null,
|
|
async () => {
|
|
const result = await getLabUpdates();
|
|
return result.nodes;
|
|
},
|
|
{
|
|
fallbackData: lab.updates,
|
|
onSuccess: data => {
|
|
lab.setUpdates(data);
|
|
},
|
|
refreshInterval,
|
|
}
|
|
);
|
|
|
|
const heroes = useMemo(() => stats?.heroes || [], [stats]);
|
|
const tags = useMemo(() => stats?.tags || [], [stats]);
|
|
const updates = useMemo(() => updatesData || [], [updatesData]);
|
|
|
|
const isLoading = (!stats || !updates) && (isValidatingStats || isValidatingUpdates);
|
|
const seenNode = useCallback(
|
|
async (nodeId: number) => {
|
|
await mutateUpdates(
|
|
updates.filter(it => it.id !== nodeId),
|
|
false
|
|
);
|
|
},
|
|
[mutateUpdates, updates]
|
|
);
|
|
|
|
return { heroes, tags, updates, isLoading, seenNode };
|
|
};
|