2025-09-19 23:01:44 +00:00
|
|
|
import { api } from "@fileone/convex/_generated/api"
|
2025-10-04 15:19:50 +00:00
|
|
|
import { type FileSystemItem, FileType } from "@fileone/convex/model/filesystem"
|
2025-09-19 23:01:44 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query"
|
|
|
|
|
import { useMutation as useContextMutation } from "convex/react"
|
|
|
|
|
import { useId } from "react"
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogClose,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
|
import { Input } from "@/components/ui/input"
|
|
|
|
|
|
2025-10-04 15:19:50 +00:00
|
|
|
type RenameFileDialogProps = {
|
|
|
|
|
item: FileSystemItem
|
|
|
|
|
onRenameSuccess: () => void
|
|
|
|
|
onClose: () => void
|
|
|
|
|
}
|
2025-09-19 23:01:44 +00:00
|
|
|
|
2025-10-04 15:19:50 +00:00
|
|
|
export function RenameFileDialog({
|
|
|
|
|
item,
|
|
|
|
|
onRenameSuccess,
|
|
|
|
|
onClose,
|
|
|
|
|
}: RenameFileDialogProps) {
|
2025-09-19 23:01:44 +00:00
|
|
|
const formId = useId()
|
|
|
|
|
|
|
|
|
|
const { mutate: renameFile, isPending: isRenaming } = useMutation({
|
|
|
|
|
mutationFn: useContextMutation(api.files.renameFile),
|
|
|
|
|
onSuccess: () => {
|
2025-10-04 15:19:50 +00:00
|
|
|
onRenameSuccess()
|
2025-09-19 23:01:44 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
2025-10-04 15:19:50 +00:00
|
|
|
const formData = new FormData(event.currentTarget)
|
|
|
|
|
const newName = formData.get("itemName") as string
|
2025-09-19 23:01:44 +00:00
|
|
|
|
2025-10-04 15:19:50 +00:00
|
|
|
if (newName) {
|
|
|
|
|
switch (item.kind) {
|
|
|
|
|
case FileType.File:
|
|
|
|
|
renameFile({
|
|
|
|
|
directoryId: item.doc.directoryId,
|
|
|
|
|
itemId: item.doc._id,
|
|
|
|
|
newName,
|
|
|
|
|
})
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
break
|
2025-09-19 23:01:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
2025-10-04 15:19:50 +00:00
|
|
|
open
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
if (!open) {
|
|
|
|
|
onClose()
|
|
|
|
|
}
|
|
|
|
|
}}
|
2025-09-19 23:01:44 +00:00
|
|
|
>
|
|
|
|
|
<DialogContent className="sm:max-w-md">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Rename File</DialogTitle>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
<form id={formId} onSubmit={onSubmit}>
|
2025-10-04 15:19:50 +00:00
|
|
|
<RenameFileInput initialValue={item.doc.name} />
|
2025-09-19 23:01:44 +00:00
|
|
|
</form>
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<DialogClose asChild>
|
|
|
|
|
<Button loading={isRenaming} variant="outline">
|
|
|
|
|
<span>Cancel</span>
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogClose>
|
|
|
|
|
<Button loading={isRenaming} type="submit" form={formId}>
|
|
|
|
|
<span>Rename</span>
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 15:19:50 +00:00
|
|
|
function RenameFileInput({ initialValue }: { initialValue: string }) {
|
|
|
|
|
return <Input defaultValue={initialValue} name="itemName" />
|
2025-09-19 23:01:44 +00:00
|
|
|
}
|