-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseModels.ts
81 lines (70 loc) · 1.72 KB
/
useModels.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { gql, useQuery } from '@apollo/client';
import { toast } from 'sonner';
import { LocalStore } from '@/lib/storage';
interface ModelsCache {
models: string[];
lastUpdate: number;
}
const CACHE_DURATION = 30 * 60 * 1000;
export const useModels = () => {
const shouldUpdateCache = (): boolean => {
try {
const cachedData = sessionStorage.getItem(LocalStore.models);
if (!cachedData) return true;
const { lastUpdate } = JSON.parse(cachedData) as ModelsCache;
const now = Date.now();
return now - lastUpdate > CACHE_DURATION;
} catch {
return true;
}
};
const getCachedModels = (): string[] => {
try {
const cachedData = sessionStorage.getItem(LocalStore.models);
if (!cachedData) return [];
const { models } = JSON.parse(cachedData) as ModelsCache;
return models;
} catch {
return [];
}
};
const updateCache = (models: string[]) => {
const cacheData: ModelsCache = {
models,
lastUpdate: Date.now(),
};
sessionStorage.setItem(LocalStore.models, JSON.stringify(cacheData));
};
const { data, loading, error } = useQuery<{
getAvailableModelTags: string[];
}>(
gql`
query {
getAvailableModelTags
}
`,
{
skip: !shouldUpdateCache(),
onCompleted: (data) => {
if (data?.getAvailableModelTags) {
updateCache(data.getAvailableModelTags);
}
},
}
);
if (error) {
toast.error('Failed to load models');
}
if (!shouldUpdateCache()) {
return {
models: getCachedModels(),
loading: false,
error: null,
};
}
return {
models: data?.getAvailableModelTags || getCachedModels(),
loading,
error,
};
};