-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseModels.ts
86 lines (77 loc) · 2.25 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
82
83
84
85
86
import { gql, useQuery } from '@apollo/client';
import { toast } from 'sonner';
import { useState, useEffect } from 'react';
import { LocalStore } from '@/lib/storage';
import { GET_MODEL_TAGS } from '@/graphql/request';
import { useAuthContext } from '@/providers/AuthProvider';
import { logger } from '@/app/log/logger';
interface ModelsCache {
models: string[];
lastUpdate: number;
}
const CACHE_DURATION = 30 * 60 * 1000;
export const useModels = () => {
const { isAuthorized } = useAuthContext();
const [selectedModel, setSelectedModel] = useState<string | undefined>(
undefined
);
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[];
}>(GET_MODEL_TAGS, {
skip: !isAuthorized || !shouldUpdateCache(),
onCompleted: (data) => {
logger.info(data);
if (data?.getAvailableModelTags) {
updateCache(data.getAvailableModelTags);
}
},
});
if (error) {
logger.info(error);
toast.error('Failed to load models');
}
const currentModels = !shouldUpdateCache()
? getCachedModels()
: data?.getAvailableModelTags || getCachedModels();
// Update selectedModel when models are loaded
useEffect(() => {
if (currentModels.length > 0 && !selectedModel) {
setSelectedModel(currentModels[0]);
}
}, [currentModels, selectedModel]);
return {
models: currentModels,
loading,
error,
selectedModel,
setSelectedModel,
};
};