-
Notifications
You must be signed in to change notification settings - Fork 0
/
useAssistants.ts
39 lines (30 loc) · 950 Bytes
/
useAssistants.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
import { useCallback, useEffect } from 'react'
import {
Assistant,
AssistantEvent,
AssistantExtension,
ExtensionTypeEnum,
events,
} from '@janhq/core'
import { useSetAtom } from 'jotai'
import { extensionManager } from '@/extension'
import { assistantsAtom } from '@/helpers/atoms/Assistant.atom'
const useAssistants = () => {
const setAssistants = useSetAtom(assistantsAtom)
const getData = useCallback(async () => {
const assistants = await getLocalAssistants()
setAssistants(assistants)
}, [setAssistants])
useEffect(() => {
getData()
events.on(AssistantEvent.OnAssistantsUpdate, () => getData())
return () => {
events.off(AssistantEvent.OnAssistantsUpdate, () => getData())
}
}, [getData])
}
const getLocalAssistants = async (): Promise<Assistant[]> =>
extensionManager
.get<AssistantExtension>(ExtensionTypeEnum.Assistant)
?.getAssistants() ?? []
export default useAssistants