diff --git a/app/components/FilesEditor/ContextMenu/container.tsx b/app/components/FilesEditor/ContextMenu/container.tsx new file mode 100644 index 0000000..e2613d9 --- /dev/null +++ b/app/components/FilesEditor/ContextMenu/container.tsx @@ -0,0 +1,43 @@ +import { FC } from "react"; +import { useRef } from "react"; +import useOnclickOutside from "@/hooks/useOnClickOutside"; + +interface ContextMenuProps { + x: number; + y: number; + closeContextMenu: () => void; + file?: any; +} + +const ContextMenuContainer: FC = ({ + x, + y, + closeContextMenu, + file, +}) => { + const contextMenuRef = useRef(null); + useOnclickOutside(contextMenuRef, closeContextMenu); + + return ( + <> +
+
{ + console.log("Option ", file?.attributes.name); + }} + > + {file?.attributes.name} +
+
Option 2
+
Option 3
+
+ + ); +}; + +export default ContextMenuContainer; diff --git a/app/components/FilesEditor/MenuContext/index.tsx b/app/components/FilesEditor/MenuContext/index.tsx deleted file mode 100644 index 569de85..0000000 --- a/app/components/FilesEditor/MenuContext/index.tsx +++ /dev/null @@ -1,34 +0,0 @@ -import { useState } from "react"; - -interface ContextMenuProps { - items: string[]; - onSelect: (item: string) => void; -} - -const ContextMenu: React.FC = ({ items, onSelect }) => { - return ( -
-
    - {items.map((item, index) => ( -
  • onSelect(item)} - > - {item} -
  • - ))} -
-
- ); -}; - -export default ContextMenu; diff --git a/app/components/FilesEditor/index.tsx b/app/components/FilesEditor/index.tsx index 23d73e5..c08e1aa 100644 --- a/app/components/FilesEditor/index.tsx +++ b/app/components/FilesEditor/index.tsx @@ -5,29 +5,42 @@ import React, { useEffect, useState, useRef } from "react"; import DocumentIcon from "@/components/Icons/Document"; import FolderIcon from "@/components/Icons/Folder"; import MenuIcon from "@/components/Icons/Menu"; -import MenuContext from "@/components/FilesEditor/MenuContext"; +import ContextMenuContainer from "./ContextMenu/container"; + +const initialContextMenu = { + show: false, + x: 0, + y: 0, + file: null, +}; + +interface FileProps { + attributes: { + name: string; + modified_at: string; + size: number; + is_file: boolean; + }; +} const Index = () => { const [apiKey, setApiKey] = useState(""); const [fileList, setFileList] = useState([]); + const [contextMenu, setContextMenu] = useState(initialContextMenu); - const [menuVisible, setMenuVisible] = useState(false); - const [menuPosition, setMenuPosition] = useState({ x: 0, y: 0 }); - const buttonRef = useRef(null); + const handleClickContextMenu = ( + e: React.MouseEvent, + file: any + ) => { + e.preventDefault(); + console.log("Right Clicked", file, e); - const handleButtonClick = (event: React.MouseEvent) => { - event.preventDefault(); // zapobiega domyślnemu działaniu - const rect = buttonRef.current?.getBoundingClientRect(); - setMenuPosition({ - x: rect?.left || 0, - y: (rect?.bottom || 0) + window.scrollY, - }); - setMenuVisible(true); + const { pageX, pageY } = e; + setContextMenu({ show: true, x: pageX, y: pageY, file }); }; - const handleSelect = (item: string) => { - console.log(`Wybrano: ${item}`); - setMenuVisible(false); + const handleContextMenuClose = () => { + setContextMenu(initialContextMenu); }; const setCredentials = async () => { @@ -46,7 +59,7 @@ const Index = () => { { method: "GET", headers: { - Authorization: "Bearer " + apiKey.toString(), + Authorization: `Bearer ${apiKey.toString()}`, "Content-Type": "application/json", }, } @@ -62,15 +75,30 @@ const Index = () => { }; useEffect(() => { - setCredentials(); - fetchFiles(apiKey); + const fetchData = async () => { + await setCredentials(); + while (apiKey === "") { + await new Promise((r) => setTimeout(r, 200)); + } + await fetchFiles(apiKey); + }; + + fetchData(); }, [apiKey]); return ( <> - {fileList.map((file) => ( + {contextMenu.show && ( + + )} + {fileList.map((file: FileProps) => (
@@ -84,11 +112,7 @@ const Index = () => {
{formateDate(file.attributes.modified_at)}
-
+
handleClickContextMenu(e, file)}>
diff --git a/app/hooks/useOnClickOutside.ts b/app/hooks/useOnClickOutside.ts new file mode 100644 index 0000000..6b3988f --- /dev/null +++ b/app/hooks/useOnClickOutside.ts @@ -0,0 +1,28 @@ +import {RefObject, useEffect} from 'react'; + +type Event = MouseEvent | TouchEvent; + +export default function useOnClickOutside( + ref: RefObject, + handler: (event: Event) => void + ) { + useEffect(() => { + const listener = (event: Event) => { + const el = ref?.current; + + if (!el || el.contains((event?.target as Node) || null)) { + return; + } + + handler(event); + }; + + document.addEventListener('mousedown', listener); + document.addEventListener('touchstart', listener); + + return () => { + document.removeEventListener('mousedown', listener); + document.removeEventListener('touchstart', listener); + }; + }, [ref, handler]); + } \ No newline at end of file