fix: cannot change jan data folder (#1805)
Signed-off-by: James <james@jan.ai> Co-authored-by: James <james@jan.ai>
This commit is contained in:
parent
532a589354
commit
23dfb1c12f
@ -27,8 +27,9 @@ const BaseLayout = (props: PropsWithChildren) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (localStorage.getItem(SUCCESS_SET_NEW_DESTINATION) === 'true') {
|
if (localStorage.getItem(SUCCESS_SET_NEW_DESTINATION) === 'true') {
|
||||||
setMainViewState(MainViewState.Settings)
|
setMainViewState(MainViewState.Settings)
|
||||||
|
localStorage.removeItem(SUCCESS_SET_NEW_DESTINATION)
|
||||||
}
|
}
|
||||||
}, [])
|
}, [setMainViewState])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen w-screen flex-1 overflow-hidden">
|
<div className="flex h-screen w-screen flex-1 overflow-hidden">
|
||||||
|
|||||||
@ -11,20 +11,23 @@ import {
|
|||||||
Button,
|
Button,
|
||||||
} from '@janhq/uikit'
|
} 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<Props> = ({
|
||||||
|
destinationPath,
|
||||||
|
onUserConfirmed,
|
||||||
|
}) => {
|
||||||
|
const [show, setShow] = useAtom(showDirectoryConfirmModalAtom)
|
||||||
|
|
||||||
const ModalChangeDirectory = () => {
|
|
||||||
const {
|
|
||||||
isDirectoryConfirm,
|
|
||||||
setIsDirectoryConfirm,
|
|
||||||
applyNewDestination,
|
|
||||||
newDestinationPath,
|
|
||||||
} = useVaultDirectory()
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal open={show} onOpenChange={setShow}>
|
||||||
open={isDirectoryConfirm}
|
|
||||||
onOpenChange={() => setIsDirectoryConfirm(false)}
|
|
||||||
>
|
|
||||||
<ModalPortal />
|
<ModalPortal />
|
||||||
<ModalContent>
|
<ModalContent>
|
||||||
<ModalHeader>
|
<ModalHeader>
|
||||||
@ -32,18 +35,16 @@ const ModalChangeDirectory = () => {
|
|||||||
</ModalHeader>
|
</ModalHeader>
|
||||||
<p className="text-muted-foreground">
|
<p className="text-muted-foreground">
|
||||||
Are you sure you want to relocate Jan data folder to{' '}
|
Are you sure you want to relocate Jan data folder to{' '}
|
||||||
<span className="font-medium text-foreground">
|
<span className="font-medium text-foreground">{destinationPath}</span>
|
||||||
{newDestinationPath}
|
|
||||||
</span>
|
|
||||||
? A restart will be required afterward.
|
? A restart will be required afterward.
|
||||||
</p>
|
</p>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<div className="flex gap-x-2">
|
<div className="flex gap-x-2">
|
||||||
<ModalClose asChild onClick={() => setIsDirectoryConfirm(false)}>
|
<ModalClose asChild onClick={() => setShow(false)}>
|
||||||
<Button themes="ghost">Cancel</Button>
|
<Button themes="ghost">Cancel</Button>
|
||||||
</ModalClose>
|
</ModalClose>
|
||||||
<ModalClose asChild>
|
<ModalClose asChild>
|
||||||
<Button onClick={applyNewDestination} autoFocus>
|
<Button onClick={onUserConfirmed} autoFocus>
|
||||||
Yes, Proceed
|
Yes, Proceed
|
||||||
</Button>
|
</Button>
|
||||||
</ModalClose>
|
</ModalClose>
|
||||||
|
|||||||
@ -10,16 +10,15 @@ import {
|
|||||||
ModalClose,
|
ModalClose,
|
||||||
Button,
|
Button,
|
||||||
} from '@janhq/uikit'
|
} from '@janhq/uikit'
|
||||||
|
import { atom, useAtom } from 'jotai'
|
||||||
|
|
||||||
import { useVaultDirectory } from '@/hooks/useVaultDirectory'
|
export const showChangeFolderErrorAtom = atom(false)
|
||||||
|
|
||||||
const ModalErrorSetDestGlobal = () => {
|
const ModalErrorSetDestGlobal = () => {
|
||||||
const { isErrorSetNewDest, setIsErrorSetNewDest } = useVaultDirectory()
|
const [show, setShow] = useAtom(showChangeFolderErrorAtom)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal open={show} onOpenChange={setShow}>
|
||||||
open={isErrorSetNewDest}
|
|
||||||
onOpenChange={() => setIsErrorSetNewDest(false)}
|
|
||||||
>
|
|
||||||
<ModalPortal />
|
<ModalPortal />
|
||||||
<ModalContent>
|
<ModalContent>
|
||||||
<ModalHeader>
|
<ModalHeader>
|
||||||
@ -31,7 +30,7 @@ const ModalErrorSetDestGlobal = () => {
|
|||||||
</p>
|
</p>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<div className="flex gap-x-2">
|
<div className="flex gap-x-2">
|
||||||
<ModalClose asChild onClick={() => setIsErrorSetNewDest(false)}>
|
<ModalClose asChild onClick={() => setShow(false)}>
|
||||||
<Button themes="danger">Got it</Button>
|
<Button themes="danger">Got it</Button>
|
||||||
</ModalClose>
|
</ModalClose>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -11,16 +11,15 @@ import {
|
|||||||
Button,
|
Button,
|
||||||
} from '@janhq/uikit'
|
} from '@janhq/uikit'
|
||||||
|
|
||||||
import { useVaultDirectory } from '@/hooks/useVaultDirectory'
|
import { atom, useAtom } from 'jotai'
|
||||||
|
|
||||||
|
export const showSamePathModalAtom = atom(false)
|
||||||
|
|
||||||
const ModalSameDirectory = () => {
|
const ModalSameDirectory = () => {
|
||||||
const { isSameDirectory, setIsSameDirectory, setNewDestination } =
|
const [show, setShow] = useAtom(showSamePathModalAtom)
|
||||||
useVaultDirectory()
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal open={show} onOpenChange={setShow}>
|
||||||
open={isSameDirectory}
|
|
||||||
onOpenChange={() => setIsSameDirectory(false)}
|
|
||||||
>
|
|
||||||
<ModalPortal />
|
<ModalPortal />
|
||||||
<ModalContent>
|
<ModalContent>
|
||||||
<ModalHeader>
|
<ModalHeader>
|
||||||
@ -31,11 +30,11 @@ const ModalSameDirectory = () => {
|
|||||||
</p>
|
</p>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<div className="flex gap-x-2">
|
<div className="flex gap-x-2">
|
||||||
<ModalClose asChild onClick={() => setIsSameDirectory(false)}>
|
<ModalClose asChild onClick={() => setShow(false)}>
|
||||||
<Button themes="ghost">Cancel</Button>
|
<Button themes="ghost">Cancel</Button>
|
||||||
</ModalClose>
|
</ModalClose>
|
||||||
<ModalClose asChild>
|
<ModalClose asChild>
|
||||||
<Button themes="danger" onClick={setNewDestination} autoFocus>
|
<Button themes="danger" onClick={() => setShow(false)} autoFocus>
|
||||||
Choose a different folder
|
Choose a different folder
|
||||||
</Button>
|
</Button>
|
||||||
</ModalClose>
|
</ModalClose>
|
||||||
|
|||||||
@ -1,17 +1,73 @@
|
|||||||
|
import { Fragment, useCallback, useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
import { fs, AppConfiguration } from '@janhq/core'
|
||||||
import { Button, Input } from '@janhq/uikit'
|
import { Button, Input } from '@janhq/uikit'
|
||||||
|
import { useSetAtom } from 'jotai'
|
||||||
import { PencilIcon, FolderOpenIcon } from 'lucide-react'
|
import { PencilIcon, FolderOpenIcon } from 'lucide-react'
|
||||||
|
|
||||||
import { useVaultDirectory } from '@/hooks/useVaultDirectory'
|
import { SUCCESS_SET_NEW_DESTINATION } from '@/hooks/useVaultDirectory'
|
||||||
|
|
||||||
import ModalChangeDirectory from './ModalChangeDirectory'
|
import ModalChangeDirectory, {
|
||||||
import ModalErrorSetDestGlobal from './ModalErrorSetDestGlobal'
|
showDirectoryConfirmModalAtom,
|
||||||
import ModalSameDirectory from './ModalSameDirectory'
|
} from './ModalChangeDirectory'
|
||||||
|
import ModalErrorSetDestGlobal, {
|
||||||
|
showChangeFolderErrorAtom,
|
||||||
|
} from './ModalErrorSetDestGlobal'
|
||||||
|
import ModalSameDirectory, { showSamePathModalAtom } from './ModalSameDirectory'
|
||||||
|
|
||||||
const DataFolder = () => {
|
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 (
|
return (
|
||||||
<>
|
<Fragment>
|
||||||
<div className="flex w-full items-start justify-between border-b border-border py-4 first:pt-0 last:border-none">
|
<div className="flex w-full items-start justify-between border-b border-border py-4 first:pt-0 last:border-none">
|
||||||
<div className="flex-shrink-0 space-y-1.5">
|
<div className="flex-shrink-0 space-y-1.5">
|
||||||
<div className="flex gap-x-2">
|
<div className="flex gap-x-2">
|
||||||
@ -26,7 +82,11 @@ const DataFolder = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-x-3">
|
<div className="flex items-center gap-x-3">
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<Input value={currentPath} className="w-[240px] pr-8" disabled />
|
<Input
|
||||||
|
value={janDataFolderPath}
|
||||||
|
className="w-[240px] pr-8"
|
||||||
|
disabled
|
||||||
|
/>
|
||||||
<FolderOpenIcon
|
<FolderOpenIcon
|
||||||
size={16}
|
size={16}
|
||||||
className="absolute right-2 top-1/2 -translate-y-1/2"
|
className="absolute right-2 top-1/2 -translate-y-1/2"
|
||||||
@ -36,16 +96,19 @@ const DataFolder = () => {
|
|||||||
size="sm"
|
size="sm"
|
||||||
themes="outline"
|
themes="outline"
|
||||||
className="h-9 w-9 p-0"
|
className="h-9 w-9 p-0"
|
||||||
onClick={setNewDestination}
|
onClick={onChangeFolderClick}
|
||||||
>
|
>
|
||||||
<PencilIcon size={16} />
|
<PencilIcon size={16} />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<ModalSameDirectory />
|
<ModalSameDirectory />
|
||||||
<ModalChangeDirectory />
|
<ModalChangeDirectory
|
||||||
|
destinationPath={destinationPath ?? ''}
|
||||||
|
onUserConfirmed={onUserConfirmed}
|
||||||
|
/>
|
||||||
<ModalErrorSetDestGlobal />
|
<ModalErrorSetDestGlobal />
|
||||||
</>
|
</Fragment>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user