-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathuseFactoryReset.ts
84 lines (67 loc) · 2.52 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { useCallback } from 'react'
import { fs, AppConfiguration, EngineManager } 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
// 1: Stop running model
setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel()
await Promise.all(
EngineManager.instance()
.engines.values()
.map(async (engine) => {
await engine.onUnload()
})
)
await new Promise((resolve) => setTimeout(resolve, 4000))
// 2: Delete the old jan data folder
setFactoryResetState(FactoryResetState.DeletingData)
await fs.rm(janDataFolderPath)
// 3: Set the default jan 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,
distinct_id: appConfiguration?.distinct_id,
}
await window.core?.api?.updateAppConfiguration(configuration)
}
// Perform factory reset
await window.core?.api?.factoryReset()
// 4: Clear app local storage
setFactoryResetState(FactoryResetState.ClearLocalStorage)
// reset the localStorage
localStorage.clear()
window.core = undefined
// 5: Relaunch the app
window.location.reload()
},
[defaultJanDataFolder, stopModel, setFactoryResetState]
)
return {
resetAll,
}
}