From 32d9bc96b5d93b93601b180843374be87f7af789 Mon Sep 17 00:00:00 2001 From: przeq piciel Date: Wed, 28 Aug 2024 20:44:29 +0000 Subject: [PATCH] refactor: Improve context menu functionality and add rename feature --- .../FilesEditor/ContextMenu/container.tsx | 62 ++++++++++++++----- .../FilesEditor/ContextMenu/rename.tsx | 59 ++++++++++++++++++ app/components/FilesEditor/index.tsx | 23 ++++++- 3 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 app/components/FilesEditor/ContextMenu/rename.tsx diff --git a/app/components/FilesEditor/ContextMenu/container.tsx b/app/components/FilesEditor/ContextMenu/container.tsx index 191bf6f..7a21153 100644 --- a/app/components/FilesEditor/ContextMenu/container.tsx +++ b/app/components/FilesEditor/ContextMenu/container.tsx @@ -1,11 +1,15 @@ -import { FC } from "react"; -import { useRef } from "react"; +import { FC, useRef } from "react"; import useOnclickOutside from "@/hooks/useOnClickOutside"; +import PencilIcon from "@/components/Icons/Pencil"; +import MoveIcon from "@/components/Icons/Move"; +import LockIcon from "@/components/Icons/Lock"; interface ContextMenuProps { x: number; y: number; closeContextMenu: () => void; + renameFunction: () => void; + setFile: (file: any) => void; file?: any; } @@ -13,31 +17,55 @@ const ContextMenuContainer: FC = ({ x, y, closeContextMenu, + renameFunction, + setFile, file, }) => { const contextMenuRef = useRef(null); useOnclickOutside(contextMenuRef, closeContextMenu); return ( - <> +
{ + setFile(file); + renameFunction(); + console.log("Renaming file:", file?.attributes.name); + }} > -
{ - console.log("Option ", file?.attributes.name); - }} - > - {file?.attributes.name} +
+
-
Option 2
-
Option 3
+ Rename
- +
{ + console.log("Moving file:", file?.attributes.name); + }} + > +
+ +
+ Move +
+
{ + console.log("Changing permissions for file:", file?.attributes.name); + }} + > +
+ +
+ Permissions +
+
); }; diff --git a/app/components/FilesEditor/ContextMenu/rename.tsx b/app/components/FilesEditor/ContextMenu/rename.tsx new file mode 100644 index 0000000..9fb7b86 --- /dev/null +++ b/app/components/FilesEditor/ContextMenu/rename.tsx @@ -0,0 +1,59 @@ +import React from "react"; + +interface Props { + closeRemapFunction: () => void; + file: any; +} + +const Rename = (props: Props) => { + const { closeRemapFunction, file } = props; + const [newName, setNewName] = React.useState(file.attributes.name); + + const handleKeyDown = (event: React.KeyboardEvent) => { + if (event.key === "Escape") { + console.log("esc"); + closeRemapFunction(); + } else if (event.key === "Enter") { + console.log("enter pressed"); + } + }; + + const handleRename = (event: React.MouseEvent) => { + // console.log("rename"); + console.log("Renaming file:", file?.attributes.name, "to", newName); + console.log(file); + }; + + const ref = React.useRef(null); + + React.useEffect(() => { + if (ref.current) { + ref.current.focus(); + } + }, []); + + return ( +
+
+
+ setNewName(e.target.value)} + /> + +
+
+ ); +}; + +export default Rename; diff --git a/app/components/FilesEditor/index.tsx b/app/components/FilesEditor/index.tsx index a0fecc4..8f1187b 100644 --- a/app/components/FilesEditor/index.tsx +++ b/app/components/FilesEditor/index.tsx @@ -6,6 +6,7 @@ import DocumentIcon from "@/components/Icons/Document"; import FolderIcon from "@/components/Icons/Folder"; import MenuIcon from "@/components/Icons/Menu"; import ContextMenuContainer from "./ContextMenu/container"; +import RenamePopup from "./ContextMenu/rename"; const initialContextMenu = { show: false, @@ -14,6 +15,10 @@ const initialContextMenu = { file: null, }; +const renamePopupProps = { + show: false, +}; + interface FileProps { attributes: { name: string; @@ -27,13 +32,24 @@ const Index = () => { const [apiKey, setApiKey] = useState(""); const [fileList, setFileList] = useState([]); const [contextMenu, setContextMenu] = useState(initialContextMenu); + const [renamePopup, setRenamePopup] = useState(renamePopupProps); + const [file, setFile] = useState(null); + + const showRenamePopup = () => { + console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + setContextMenu(initialContextMenu); + setRenamePopup({ show: true }); + }; + + const closeRenamePopup = () => { + setRenamePopup(renamePopupProps); + }; const handleClickContextMenu = ( e: React.MouseEvent, file: any ) => { e.preventDefault(); - console.log("Right Clicked", file, e); const { pageX, pageY } = e; setContextMenu({ show: true, x: pageX, y: pageY, file }); @@ -88,11 +104,16 @@ const Index = () => { return ( <> + {renamePopup.show && ( + + )} {contextMenu.show && ( )}