-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/unable factory reset windows nitro running (janhq#2422)
* fix: unable to factory reset when nitro is running on windows --------- Signed-off-by: James <[email protected]>
- Loading branch information
Showing
9 changed files
with
119 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,68 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useCallback } from 'react' | ||
|
||
import { fs, AppConfiguration, joinPath, getUserHomePath } from '@janhq/core' | ||
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, setdefaultJanDataFolder] = useState('') | ||
|
||
useEffect(() => { | ||
async function getDefaultJanDataFolder() { | ||
const homePath = await getUserHomePath() | ||
const defaultJanDataFolder = await joinPath([homePath, 'jan']) | ||
setdefaultJanDataFolder(defaultJanDataFolder) | ||
} | ||
getDefaultJanDataFolder() | ||
}, []) | ||
|
||
const resetAll = async (keepCurrentFolder?: boolean) => { | ||
// 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') | ||
} | ||
|
||
console.debug('appConfiguration: ', appConfiguration) | ||
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, | ||
const defaultJanDataFolder = useAtomValue(defaultJanDataFolderAtom) | ||
const { activeModel, 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') | ||
} | ||
await window.core?.api?.updateAppConfiguration(configuration) | ||
} | ||
await fs.rmdirSync(janDataFolderPath, { recursive: true }) | ||
|
||
// reset the localStorage | ||
localStorage.clear() | ||
const janDataFolderPath = appConfiguration!.data_folder | ||
|
||
await window.core?.api?.relaunch() | ||
} | ||
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) | ||
} | ||
|
||
if (activeModel) { | ||
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, activeModel, stopModel, setFactoryResetState] | ||
) | ||
|
||
return { | ||
defaultJanDataFolder, | ||
resetAll, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
web/screens/Settings/Advanced/FactoryReset/ResettingModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Modal, ModalContent, ModalHeader, ModalTitle } from '@janhq/uikit' | ||
import { atom, useAtomValue } from 'jotai' | ||
|
||
import { | ||
FactoryResetState, | ||
factoryResetStateAtom, | ||
} from '@/hooks/useFactoryReset' | ||
|
||
const resetModalVisibilityAtom = atom((get) => { | ||
const visible = get(factoryResetStateAtom) !== FactoryResetState.Idle | ||
return visible | ||
}) | ||
|
||
const ResettingModal: React.FC = () => { | ||
const visibility = useAtomValue(resetModalVisibilityAtom) | ||
|
||
return ( | ||
<Modal open={visibility}> | ||
<ModalContent> | ||
<ModalHeader> | ||
<ModalTitle>Factory reset in progress..</ModalTitle> | ||
</ModalHeader> | ||
<p className="text-muted-foreground">Resetting..</p> | ||
</ModalContent> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default ResettingModal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters