forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseStarterScreen.ts
79 lines (67 loc) · 2.31 KB
/
useStarterScreen.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
import { useState, useEffect, useMemo } from 'react'
import { useAtomValue } from 'jotai'
import { isLocalEngine } from '@/utils/modelEngine'
import { extensionManager } from '@/extension'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import { threadsAtom } from '@/helpers/atoms/Thread.atom'
export function useStarterScreen() {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const threads = useAtomValue(threadsAtom)
const isDownloadALocalModel = useMemo(
() => downloadedModels.some((x) => isLocalEngine(x.engine)),
[downloadedModels]
)
const [extensionHasSettings, setExtensionHasSettings] = useState<
{ name?: string; setting: string; apiKey: string; provider: string }[]
>([])
useEffect(() => {
const getAllSettings = async () => {
const extensionsMenu: {
name?: string
setting: string
apiKey: string
provider: string
}[] = []
const extensions = extensionManager.getAll()
for (const extension of extensions) {
if (typeof extension.getSettings === 'function') {
const settings = await extension.getSettings()
if (
(settings && settings.length > 0) ||
(await extension.installationState()) !== 'NotRequired'
) {
extensionsMenu.push({
name: extension.productName,
setting: extension.name,
apiKey:
'apiKey' in extension && typeof extension.apiKey === 'string'
? extension.apiKey
: '',
provider:
'provider' in extension &&
typeof extension.provider === 'string'
? extension.provider
: '',
})
}
}
}
setExtensionHasSettings(extensionsMenu)
}
getAllSettings()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const isAnyRemoteModelConfigured = useMemo(
() => extensionHasSettings.some((x) => x.apiKey.length > 1),
[extensionHasSettings]
)
const isShowStarterScreen = useMemo(
() =>
!isAnyRemoteModelConfigured && !isDownloadALocalModel && !threads.length,
[isAnyRemoteModelConfigured, isDownloadALocalModel, threads]
)
return {
extensionHasSettings,
isShowStarterScreen,
}
}