forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseProgressiveLoad.ts
More file actions
40 lines (31 loc) 路 1.18 KB
/
Copy pathuseProgressiveLoad.ts
File metadata and controls
40 lines (31 loc) 路 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useState, useEffect } from 'react';
import { progressiveFetchers } from '@/lib/api';
type Stage = 'kpis' | 'simple' | 'complex';
interface ProgressiveState {
kpis: Awaited<ReturnType<typeof progressiveFetchers.kpis>> | null;
simple: Awaited<ReturnType<typeof progressiveFetchers.simple>> | null;
complex: Awaited<ReturnType<typeof progressiveFetchers.complex>> | null;
loaded: Stage[];
}
export function useProgressiveLoad() {
const [state, setState] = useState<ProgressiveState>({
kpis: null, simple: null, complex: null, loaded: [],
});
useEffect(() => {
let cancelled = false;
async function load() {
const kpis = await progressiveFetchers.kpis();
if (cancelled) return;
setState(s => ({ ...s, kpis, loaded: [...s.loaded, 'kpis'] }));
const simple = await progressiveFetchers.simple();
if (cancelled) return;
setState(s => ({ ...s, simple, loaded: [...s.loaded, 'simple'] }));
const complex = await progressiveFetchers.complex();
if (cancelled) return;
setState(s => ({ ...s, complex, loaded: [...s.loaded, 'complex'] }));
}
load();
return () => { cancelled = true; };
}, []);
return state;
}