fix: rename dialog not working

This commit is contained in:
2025-10-04 15:19:50 +00:00
parent 9282e75bef
commit 57369d10fe
2 changed files with 50 additions and 54 deletions

View File

@@ -1,9 +1,8 @@
import { api } from "@fileone/convex/_generated/api" import { api } from "@fileone/convex/_generated/api"
import { type FileSystemItem, FileType } from "@fileone/convex/model/filesystem"
import { useMutation } from "@tanstack/react-query" import { useMutation } from "@tanstack/react-query"
import { useMutation as useContextMutation } from "convex/react" import { useMutation as useContextMutation } from "convex/react"
import { atom, useAtom, useStore } from "jotai"
import { useId } from "react" import { useId } from "react"
import { toast } from "sonner"
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import { import {
Dialog, Dialog,
@@ -14,50 +13,39 @@ import {
DialogTitle, DialogTitle,
} from "@/components/ui/dialog" } from "@/components/ui/dialog"
import { Input } from "@/components/ui/input" import { Input } from "@/components/ui/input"
import { itemBeingRenamedAtom } from "./state"
const fielNameAtom = atom( type RenameFileDialogProps = {
(get) => get(itemBeingRenamedAtom)?.name, item: FileSystemItem
(get, set, newName: string) => { onRenameSuccess: () => void
const current = get(itemBeingRenamedAtom) onClose: () => void
if (current) { }
set(itemBeingRenamedAtom, {
...current,
name: newName,
})
}
},
)
export function RenameFileDialog() { export function RenameFileDialog({
const [itemBeingRenamed, setItemBeingRenamed] = item,
useAtom(itemBeingRenamedAtom) onRenameSuccess,
const store = useStore() onClose,
}: RenameFileDialogProps) {
const formId = useId() const formId = useId()
const { mutate: renameFile, isPending: isRenaming } = useMutation({ const { mutate: renameFile, isPending: isRenaming } = useMutation({
mutationFn: useContextMutation(api.files.renameFile), mutationFn: useContextMutation(api.files.renameFile),
onSuccess: () => { onSuccess: () => {
setItemBeingRenamed(null) onRenameSuccess()
toast.success("File renamed successfully")
}, },
}) })
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => { const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault() event.preventDefault()
const itemBeingRenamed = store.get(itemBeingRenamedAtom)
if (itemBeingRenamed) {
const formData = new FormData(event.currentTarget) const formData = new FormData(event.currentTarget)
const newName = formData.get("itemName") as string const newName = formData.get("itemName") as string
if (newName) { if (newName) {
switch (itemBeingRenamed.originalItem.kind) { switch (item.kind) {
case "file": case FileType.File:
renameFile({ renameFile({
directoryId: directoryId: item.doc.directoryId,
itemBeingRenamed.originalItem.doc.directoryId, itemId: item.doc._id,
itemId: itemBeingRenamed.originalItem.doc._id,
newName, newName,
}) })
break break
@@ -66,14 +54,15 @@ export function RenameFileDialog() {
} }
} }
} }
}
return ( return (
<Dialog <Dialog
open={itemBeingRenamed !== null} open
onOpenChange={(open) => onOpenChange={(open) => {
setItemBeingRenamed(open ? itemBeingRenamed : null) if (!open) {
onClose()
} }
}}
> >
<DialogContent className="sm:max-w-md"> <DialogContent className="sm:max-w-md">
<DialogHeader> <DialogHeader>
@@ -81,7 +70,7 @@ export function RenameFileDialog() {
</DialogHeader> </DialogHeader>
<form id={formId} onSubmit={onSubmit}> <form id={formId} onSubmit={onSubmit}>
<RenameFileInput /> <RenameFileInput initialValue={item.doc.name} />
</form> </form>
<DialogFooter> <DialogFooter>
@@ -99,13 +88,6 @@ export function RenameFileDialog() {
) )
} }
function RenameFileInput() { function RenameFileInput({ initialValue }: { initialValue: string }) {
const [fileName, setFileName] = useAtom(fielNameAtom) return <Input defaultValue={initialValue} name="itemName" />
return (
<Input
value={fileName}
name="itemName"
onChange={(e) => setFileName(e.target.value)}
/>
)
} }

View File

@@ -23,7 +23,7 @@ import {
UploadCloudIcon, UploadCloudIcon,
} from "lucide-react" } from "lucide-react"
import { type ChangeEvent, useCallback, useContext, useRef } from "react" import { type ChangeEvent, useCallback, useContext, useRef } from "react"
import { toast } from "sonner" import { toast, toast } from "sonner"
import { DirectoryIcon } from "@/components/icons/directory-icon" import { DirectoryIcon } from "@/components/icons/directory-icon"
import { TextFileIcon } from "@/components/icons/text-file-icon" import { TextFileIcon } from "@/components/icons/text-file-icon"
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
@@ -154,7 +154,21 @@ function RouteComponent() {
)} )}
</WithAtom> </WithAtom>
<RenameFileDialog /> <WithAtom atom={itemBeingRenamedAtom}>
{(itemBeingRenamed, setItemBeingRenamed) => {
if (!itemBeingRenamed) return null
return (
<RenameFileDialog
item={itemBeingRenamed.originalItem}
onRenameSuccess={() => {
toast.success("File renamed successfully")
setItemBeingRenamed(null)
}}
onClose={() => setItemBeingRenamed(null)}
/>
)
}}
</WithAtom>
<WithAtom atom={openedFileAtom}> <WithAtom atom={openedFileAtom}>
{(openedFile, setOpenedFile) => { {(openedFile, setOpenedFile) => {