-
Notifications
You must be signed in to change notification settings - Fork 0
/
useFactoryReset.ts
66 lines (51 loc) · 2 KB
/
useFactoryReset.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
import { useCallback } from 'react'
import { fs, AppConfiguration } from '@janhq/core'
import { atom, useAtomValue, useSetAtom } from 'jotai'
import { useActiveModel } from './useActiveModel'
import { defaultJanDataFolderAtom } from '@/helpers/atoms/App.atom'
export enum FactoryResetState {
Idle = 'idle',
Starting = 'starting',
StoppingModel = 'stopping_model',
DeletingData = 'deleting_data',
ClearLocalStorage = 'clear_local_storage',
}
export const factoryResetStateAtom = atom(FactoryResetState.Idle)
export default function useFactoryReset() {
const defaultJanDataFolder = useAtomValue(defaultJanDataFolderAtom)
const { stopModel } = useActiveModel()
const setFactoryResetState = useSetAtom(factoryResetStateAtom)
const resetAll = useCallback(
async (keepCurrentFolder?: boolean) => {
setFactoryResetState(FactoryResetState.Starting)
// read the place of jan data folder
const appConfiguration: AppConfiguration | undefined =
await window.core?.api?.getAppConfigurations()
if (!appConfiguration) {
console.debug('Failed to get app configuration')
}
const janDataFolderPath = appConfiguration!.data_folder
if (!keepCurrentFolder) {
// set the default jan data folder to user's home directory
const configuration: AppConfiguration = {
data_folder: defaultJanDataFolder,
quick_ask: appConfiguration?.quick_ask ?? false,
}
await window.core?.api?.updateAppConfiguration(configuration)
}
setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel()
await new Promise((resolve) => setTimeout(resolve, 4000))
setFactoryResetState(FactoryResetState.DeletingData)
await fs.rm(janDataFolderPath)
setFactoryResetState(FactoryResetState.ClearLocalStorage)
// reset the localStorage
localStorage.clear()
await window.core?.api?.relaunch()
},
[defaultJanDataFolder, stopModel, setFactoryResetState]
)
return {
resetAll,
}
}