jan/web/hooks/useSettings.ts
hiento09 1ec8174700
Feature GPU detection for Jan on Windows and Linux (#1242)
* Add js function to generate gpu and cuda detection

* inference nitro manage via json file instead of bash and bat script

* Add /usr/lib/x86_64-linux-gnu/ to linux check gpu

* chore: add CPU - GPU toggle

* correct file path

* fix: exist file sync check

* fix: get resources path

* Fix error jan/engines create existed error

* Seting sync to file

* Fix error show notification for GPU

* Set notify default to true

---------

Co-authored-by: Hien To <tominhhien97@gmail.com>
Co-authored-by: Louis <louis@jan.ai>
2023-12-29 15:56:36 +07:00

68 lines
1.8 KiB
TypeScript

import { useEffect, useState } from 'react'
import { fs, joinPath } from '@janhq/core'
import { atom, useAtom } from 'jotai'
export const isShowNotificationAtom = atom<boolean>(false)
export const useSettings = () => {
const [isGPUModeEnabled, setIsGPUModeEnabled] = useState(false) // New state for GPU mode
const [showNotification, setShowNotification] = useAtom(
isShowNotificationAtom
)
useEffect(() => {
setTimeout(() => validateSettings, 3000)
}, [])
const validateSettings = async () => {
readSettings().then((settings) => {
if (
settings &&
settings.notify &&
((settings.nvidia_driver?.exist && !settings.cuda?.exist) ||
!settings.nvidia_driver?.exist)
) {
setShowNotification(true)
}
// Check if run_mode is 'gpu' or 'cpu' and update state accordingly
setIsGPUModeEnabled(settings?.run_mode === 'gpu')
})
}
const readSettings = async () => {
if (!window?.core?.api) {
return
}
const settingsFile = await joinPath(['file://settings', 'settings.json'])
if (await fs.existsSync(settingsFile)) {
const settings = await fs.readFileSync(settingsFile, 'utf-8')
return typeof settings === 'object' ? settings : JSON.parse(settings)
}
return {}
}
const saveSettings = async ({
runMode,
notify,
}: {
runMode?: string | undefined
notify?: boolean | undefined
}) => {
const settingsFile = await joinPath(['file://settings', 'settings.json'])
const settings = await readSettings()
if (runMode != null) settings.run_mode = runMode
if (notify != null) settings.notify = notify
await fs.writeFileSync(settingsFile, JSON.stringify(settings))
}
return {
showNotification,
isGPUModeEnabled,
readSettings,
saveSettings,
setShowNotification,
validateSettings,
}
}