-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { EngineConfig, Engines } from '@janhq/core' | ||
Check warning on line 1 in web/helpers/atoms/Engines.atom.ts GitHub Actions / test-on-macos
Check warning on line 1 in web/helpers/atoms/Engines.atom.ts GitHub Actions / test-on-ubuntu
Check warning on line 1 in web/helpers/atoms/Engines.atom.ts GitHub Actions / coverage-check
|
||
import { atom } from 'jotai' | ||
|
||
/** | ||
* Store all of the installed engines including local and remote engines | ||
*/ | ||
export const installedEnginesAtom = atom<Engines>() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useCallback, useEffect } from 'react' | ||
|
||
import { | ||
ExtensionTypeEnum, | ||
Model, | ||
Check warning on line 5 in web/hooks/useEngines.ts GitHub Actions / test-on-macos
Check warning on line 5 in web/hooks/useEngines.ts GitHub Actions / test-on-ubuntu
Check warning on line 5 in web/hooks/useEngines.ts GitHub Actions / coverage-check
|
||
ModelEvent, | ||
Check warning on line 6 in web/hooks/useEngines.ts GitHub Actions / test-on-macos
Check warning on line 6 in web/hooks/useEngines.ts GitHub Actions / test-on-ubuntu
Check warning on line 6 in web/hooks/useEngines.ts GitHub Actions / coverage-check
|
||
ModelExtension, | ||
Check warning on line 7 in web/hooks/useEngines.ts GitHub Actions / test-on-macos
Check warning on line 7 in web/hooks/useEngines.ts GitHub Actions / test-on-ubuntu
Check warning on line 7 in web/hooks/useEngines.ts GitHub Actions / coverage-check
|
||
events, | ||
ModelManager, | ||
Check warning on line 9 in web/hooks/useEngines.ts GitHub Actions / test-on-macos
Check warning on line 9 in web/hooks/useEngines.ts GitHub Actions / test-on-ubuntu
Check warning on line 9 in web/hooks/useEngines.ts GitHub Actions / coverage-check
|
||
EngineEvent, | ||
EngineManagementExtension, | ||
Engines, | ||
} from '@janhq/core' | ||
|
||
import { useSetAtom } from 'jotai' | ||
|
||
import { useDebouncedCallback } from 'use-debounce' | ||
|
||
import { extensionManager } from '@/extension' | ||
|
||
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom' | ||
|
||
/** | ||
* useModels hook - Handles the state of models | ||
* It fetches the downloaded models, configured models and default model from Model Extension | ||
* and updates the atoms accordingly. | ||
*/ | ||
const useEngines = () => { | ||
const setInstalledEngines = useSetAtom(installedEnginesAtom) | ||
|
||
const getData = useCallback(() => { | ||
getEngines().then(setInstalledEngines) | ||
}, [setInstalledEngines]) | ||
|
||
const reloadData = useDebouncedCallback(() => getData(), 300) | ||
|
||
const getEngines = async (): Promise<Engines> => | ||
extensionManager | ||
.get<EngineManagementExtension>(ExtensionTypeEnum.Engine) | ||
?.getEngines() | ||
.catch(() => ({}) as Engines) ?? ({} as Engines) | ||
|
||
useEffect(() => { | ||
// Listen for engine updates | ||
events.on(EngineEvent.OnEngineUpdate, reloadData) | ||
return () => { | ||
// Remove listener on unmount | ||
events.off(EngineEvent.OnEngineUpdate, reloadData) | ||
} | ||
}, [reloadData]) | ||
|
||
return { | ||
getData, | ||
} | ||
} | ||
|
||
export default useEngines |