Skip to content

Commit 4cd2e17

Browse files
authored
feat(archive): auto-locate destination to current path (#327)
1 parent 6ed0446 commit 4cd2e17

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

src/components/FolderTree.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export type ModalFolderChooseProps = {
340340
onClose: () => void
341341
onSubmit?: (text: string) => void
342342
type?: string
343-
defaultValue?: string
343+
defaultValue?: string | (() => string)
344344
loading?: boolean
345345
footerSlot?: JSXElement
346346
headerSlot?: (handler: FolderTreeHandler | undefined) => JSXElement
@@ -349,12 +349,19 @@ export type ModalFolderChooseProps = {
349349
}
350350
export const ModalFolderChoose = (props: ModalFolderChooseProps) => {
351351
const t = useT()
352-
const [value, setValue] = createSignal(props.defaultValue ?? "/")
352+
const [value, setValue] = createSignal("/")
353353
const [handler, setHandler] = createSignal<FolderTreeHandler>()
354354
createEffect(() => {
355355
if (!props.opened) return
356356
handler()?.setPath(value())
357357
})
358+
if (typeof props.defaultValue === "function") {
359+
createEffect(() => {
360+
setValue((props.defaultValue as () => string)())
361+
})
362+
} else if (typeof props.defaultValue === "string") {
363+
setValue(props.defaultValue)
364+
}
358365
return (
359366
<Modal
360367
size="xl"

src/pages/home/toolbar/CopyMove.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { Checkbox, createDisclosure, VStack, Button } from "@hope-ui/solid"
22
import { createSignal, onCleanup } from "solid-js"
33
import { ModalFolderChoose, FolderTreeHandler } from "~/components"
44
import { useFetch, usePath, useRouter, useT } from "~/hooks"
5-
import { selectedObjs } from "~/store"
5+
import { me, selectedObjs } from "~/store"
66
import { bus, fsCopy, fsMove, handleRespWithNotifySuccess } from "~/utils"
77
import { CgFolderAdd } from "solid-icons/cg"
8+
import { UserMethods, UserPermissions } from "~/types"
89

9-
const CreateFolderButton = (props: { handler?: FolderTreeHandler }) => {
10+
export const CreateFolderButton = (props: { handler?: FolderTreeHandler }) => {
11+
if (!UserMethods.can(me(), UserPermissions.indexOf("write"))) {
12+
return null
13+
}
1014
const t = useT()
1115
return (
1216
<Button

src/pages/home/toolbar/Decompress.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { bus, fsArchiveDecompress, handleRespWithNotifySuccess } from "~/utils"
1111
import { batch, createSignal, onCleanup } from "solid-js"
1212
import { ModalFolderChoose } from "~/components"
1313
import { selectedObjs } from "~/store"
14+
import { CreateFolderButton } from "./CopyMove"
1415

1516
export const Decompress = () => {
1617
const t = useT()
@@ -23,9 +24,14 @@ export const Decompress = () => {
2324
const [cacheFull, setCacheFull] = createSignal(true)
2425
const [putIntoNewDir, setPutIntoNewDir] = createSignal(false)
2526
const [overwrite, setOverwrite] = createSignal(false)
27+
const [srcPath, setSrcPath] = createSignal("")
28+
const [srcName, setSrcName] = createSignal<string[]>()
2629
const handler = (name: string) => {
2730
if (name === "decompress") {
31+
const path = pathname()
2832
batch(() => {
33+
setSrcPath(path)
34+
setSrcName(selectedObjs().map((o) => o.name))
2935
setCacheFull(true)
3036
setInnerPath("/")
3137
setArchivePass("")
@@ -35,7 +41,11 @@ export const Decompress = () => {
3541
}
3642
const extractHandler = (args: string) => {
3743
const { inner, pass } = JSON.parse(args)
44+
const path = pathname()
45+
const idx = path.lastIndexOf("/")
3846
batch(() => {
47+
setSrcPath(path.slice(0, idx))
48+
setSrcName([path.slice(idx + 1)])
3949
setCacheFull(false)
4050
setInnerPath(inner)
4151
setArchivePass(pass)
@@ -54,21 +64,14 @@ export const Decompress = () => {
5464
}
5565
return t("home.toolbar.archive.extract_header", { path: innerPath() })
5666
}
57-
const getPathAndName = () => {
58-
let path = pathname()
59-
if (innerPath() === "/") {
60-
return { path: path, name: selectedObjs().map((o) => o.name) }
61-
} else {
62-
let idx = path.lastIndexOf("/")
63-
return { path: path.slice(0, idx), name: [path.slice(idx + 1)] }
64-
}
65-
}
6667
return (
6768
<ModalFolderChoose
6869
header={header()}
6970
opened={isOpen()}
7071
onClose={onClose}
7172
loading={loading()}
73+
defaultValue={srcPath}
74+
headerSlot={(handler) => <CreateFolderButton handler={handler} />}
7275
footerSlot={
7376
<VStack w="$full" spacing="$2">
7477
<Checkbox
@@ -81,11 +84,10 @@ export const Decompress = () => {
8184
</VStack>
8285
}
8386
onSubmit={async (dst) => {
84-
const { path, name } = getPathAndName()
8587
const resp = await ok(
86-
path,
88+
srcPath(),
8789
dst,
88-
name,
90+
srcName(),
8991
archivePass(),
9092
innerPath(),
9193
cacheFull(),

src/pages/home/toolbar/RecursiveMove.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ModalFolderChoose } from "~/components"
1515
import { useFetch, usePath, useRouter, useT } from "~/hooks"
1616
import { bus, fsRecursiveMove, handleRespWithNotifySuccess } from "~/utils"
1717
import { createSignal, onCleanup } from "solid-js"
18+
import { CreateFolderButton } from "./CopyMove"
1819

1920
export const RecursiveMove = () => {
2021
const {
@@ -77,6 +78,7 @@ export const RecursiveMove = () => {
7778
opened={isOpen()}
7879
onClose={onClose}
7980
loading={loading()}
81+
headerSlot={(handler) => <CreateFolderButton handler={handler} />}
8082
footerSlot={
8183
<HStack mr="auto" flex="0.8" spacing="$1">
8284
<SimpleSelect

0 commit comments

Comments
 (0)