diff --git a/web/containers/Layout/index.tsx b/web/containers/Layout/index.tsx index 54a7845a4..033038bad 100644 --- a/web/containers/Layout/index.tsx +++ b/web/containers/Layout/index.tsx @@ -27,8 +27,9 @@ const BaseLayout = (props: PropsWithChildren) => { useEffect(() => { if (localStorage.getItem(SUCCESS_SET_NEW_DESTINATION) === 'true') { setMainViewState(MainViewState.Settings) + localStorage.removeItem(SUCCESS_SET_NEW_DESTINATION) } - }, []) + }, [setMainViewState]) return (
diff --git a/web/screens/Settings/Advanced/DataFolder/ModalChangeDirectory.tsx b/web/screens/Settings/Advanced/DataFolder/ModalChangeDirectory.tsx index f0fb7e12f..022db7260 100644 --- a/web/screens/Settings/Advanced/DataFolder/ModalChangeDirectory.tsx +++ b/web/screens/Settings/Advanced/DataFolder/ModalChangeDirectory.tsx @@ -11,20 +11,23 @@ import { Button, } from '@janhq/uikit' -import { useVaultDirectory } from '@/hooks/useVaultDirectory' +import { atom, useAtom } from 'jotai' + +export const showDirectoryConfirmModalAtom = atom(false) + +type Props = { + destinationPath: string + onUserConfirmed: () => void +} + +const ModalChangeDirectory: React.FC = ({ + destinationPath, + onUserConfirmed, +}) => { + const [show, setShow] = useAtom(showDirectoryConfirmModalAtom) -const ModalChangeDirectory = () => { - const { - isDirectoryConfirm, - setIsDirectoryConfirm, - applyNewDestination, - newDestinationPath, - } = useVaultDirectory() return ( - setIsDirectoryConfirm(false)} - > + @@ -32,18 +35,16 @@ const ModalChangeDirectory = () => {

Are you sure you want to relocate Jan data folder to{' '} - - {newDestinationPath} - + {destinationPath} ? A restart will be required afterward.

- setIsDirectoryConfirm(false)}> + setShow(false)}> - diff --git a/web/screens/Settings/Advanced/DataFolder/ModalErrorSetDestGlobal.tsx b/web/screens/Settings/Advanced/DataFolder/ModalErrorSetDestGlobal.tsx index 44a9db097..3729dc0d8 100644 --- a/web/screens/Settings/Advanced/DataFolder/ModalErrorSetDestGlobal.tsx +++ b/web/screens/Settings/Advanced/DataFolder/ModalErrorSetDestGlobal.tsx @@ -10,16 +10,15 @@ import { ModalClose, Button, } from '@janhq/uikit' +import { atom, useAtom } from 'jotai' -import { useVaultDirectory } from '@/hooks/useVaultDirectory' +export const showChangeFolderErrorAtom = atom(false) const ModalErrorSetDestGlobal = () => { - const { isErrorSetNewDest, setIsErrorSetNewDest } = useVaultDirectory() + const [show, setShow] = useAtom(showChangeFolderErrorAtom) + return ( - setIsErrorSetNewDest(false)} - > + @@ -31,7 +30,7 @@ const ModalErrorSetDestGlobal = () => {

- setIsErrorSetNewDest(false)}> + setShow(false)}>
diff --git a/web/screens/Settings/Advanced/DataFolder/ModalSameDirectory.tsx b/web/screens/Settings/Advanced/DataFolder/ModalSameDirectory.tsx index bd4d32e6a..8b2d90c61 100644 --- a/web/screens/Settings/Advanced/DataFolder/ModalSameDirectory.tsx +++ b/web/screens/Settings/Advanced/DataFolder/ModalSameDirectory.tsx @@ -11,16 +11,15 @@ import { Button, } from '@janhq/uikit' -import { useVaultDirectory } from '@/hooks/useVaultDirectory' +import { atom, useAtom } from 'jotai' + +export const showSamePathModalAtom = atom(false) const ModalSameDirectory = () => { - const { isSameDirectory, setIsSameDirectory, setNewDestination } = - useVaultDirectory() + const [show, setShow] = useAtom(showSamePathModalAtom) + return ( - setIsSameDirectory(false)} - > + @@ -31,11 +30,11 @@ const ModalSameDirectory = () => {

- setIsSameDirectory(false)}> + setShow(false)}> - diff --git a/web/screens/Settings/Advanced/DataFolder/index.tsx b/web/screens/Settings/Advanced/DataFolder/index.tsx index 936992c9d..90a0dd38b 100644 --- a/web/screens/Settings/Advanced/DataFolder/index.tsx +++ b/web/screens/Settings/Advanced/DataFolder/index.tsx @@ -1,17 +1,73 @@ +import { Fragment, useCallback, useEffect, useState } from 'react' + +import { fs, AppConfiguration } from '@janhq/core' import { Button, Input } from '@janhq/uikit' +import { useSetAtom } from 'jotai' import { PencilIcon, FolderOpenIcon } from 'lucide-react' -import { useVaultDirectory } from '@/hooks/useVaultDirectory' +import { SUCCESS_SET_NEW_DESTINATION } from '@/hooks/useVaultDirectory' -import ModalChangeDirectory from './ModalChangeDirectory' -import ModalErrorSetDestGlobal from './ModalErrorSetDestGlobal' -import ModalSameDirectory from './ModalSameDirectory' +import ModalChangeDirectory, { + showDirectoryConfirmModalAtom, +} from './ModalChangeDirectory' +import ModalErrorSetDestGlobal, { + showChangeFolderErrorAtom, +} from './ModalErrorSetDestGlobal' +import ModalSameDirectory, { showSamePathModalAtom } from './ModalSameDirectory' const DataFolder = () => { - const { currentPath, setNewDestination } = useVaultDirectory() + const [janDataFolderPath, setJanDataFolderPath] = useState('') + const setShowDirectoryConfirm = useSetAtom(showDirectoryConfirmModalAtom) + const setShowSameDirectory = useSetAtom(showSamePathModalAtom) + const setShowChangeFolderError = useSetAtom(showChangeFolderErrorAtom) + const [destinationPath, setDestinationPath] = useState(undefined) + + useEffect(() => { + window.core?.api + ?.getAppConfigurations() + ?.then((appConfig: AppConfiguration) => { + setJanDataFolderPath(appConfig.data_folder) + }) + }, []) + + const onChangeFolderClick = useCallback(async () => { + const destFolder = await window.core?.api?.selectDirectory() + if (!destFolder) return + + if (destFolder === janDataFolderPath) { + setShowSameDirectory(true) + return + } + + setDestinationPath(destFolder) + setShowDirectoryConfirm(true) + }, [janDataFolderPath, setShowSameDirectory, setShowDirectoryConfirm]) + + const onUserConfirmed = useCallback(async () => { + const destination = destinationPath + if (!destination) return + try { + const appConfiguration: AppConfiguration = + await window.core?.api?.getAppConfigurations() + const currentJanDataFolder = appConfiguration.data_folder + appConfiguration.data_folder = destination + await fs.syncFile(currentJanDataFolder, destination) + await window.core?.api?.updateAppConfiguration(appConfiguration) + + console.debug( + `File sync finished from ${currentJanDataFolder} to ${destination}` + ) + + localStorage.setItem(SUCCESS_SET_NEW_DESTINATION, 'true') + await window.core?.api?.relaunch() + } catch (e) { + console.error(`Error: ${e}`) + setShowChangeFolderError(true) + } + }, [destinationPath, setShowChangeFolderError]) return ( - <> +
@@ -26,7 +82,11 @@ const DataFolder = () => {
- + { size="sm" themes="outline" className="h-9 w-9 p-0" - onClick={setNewDestination} + onClick={onChangeFolderClick} >
- + - + ) }