feat: Add context menu component for file editor
This commit is contained in:
34
app/components/FilesEditor/MenuContext/index.tsx
Normal file
34
app/components/FilesEditor/MenuContext/index.tsx
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
|
interface ContextMenuProps {
|
||||||
|
items: string[];
|
||||||
|
onSelect: (item: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ContextMenu: React.FC<ContextMenuProps> = ({ items, onSelect }) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
position: "absolute",
|
||||||
|
border: "1px solid #ccc",
|
||||||
|
backgroundColor: "#fff",
|
||||||
|
boxShadow: "0px 0px 10px rgba(0, 0, 0, 0.1)",
|
||||||
|
zIndex: 1000,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ul style={{ margin: 0, padding: 10, listStyleType: "none" }}>
|
||||||
|
{items.map((item, index) => (
|
||||||
|
<li
|
||||||
|
key={index}
|
||||||
|
style={{ padding: "5px 10px", cursor: "pointer" }}
|
||||||
|
onClick={() => onSelect(item)}
|
||||||
|
>
|
||||||
|
{item}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ContextMenu;
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
|
|
||||||
const index = () => {
|
|
||||||
return <></>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default index;
|
|
||||||
@@ -1,15 +1,35 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { formatDistanceToNow } from "date-fns";
|
import { formatDistanceToNow } from "date-fns";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState, useRef } from "react";
|
||||||
import DocumentIcon from "@/components/Icons/Document";
|
import DocumentIcon from "@/components/Icons/Document";
|
||||||
import FolderIcon from "@/components/Icons/Folder";
|
import FolderIcon from "@/components/Icons/Folder";
|
||||||
import MenuIcon from "@/components/Icons/Menu";
|
import MenuIcon from "@/components/Icons/Menu";
|
||||||
|
import MenuContext from "@/components/FilesEditor/MenuContext";
|
||||||
|
|
||||||
const Index = () => {
|
const Index = () => {
|
||||||
const [apiKey, setApiKey] = useState("");
|
const [apiKey, setApiKey] = useState("");
|
||||||
const [fileList, setFileList] = useState([]);
|
const [fileList, setFileList] = useState([]);
|
||||||
|
|
||||||
|
const [menuVisible, setMenuVisible] = useState(false);
|
||||||
|
const [menuPosition, setMenuPosition] = useState({ x: 0, y: 0 });
|
||||||
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
||||||
|
|
||||||
|
const handleButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
|
||||||
|
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 handleSelect = (item: string) => {
|
||||||
|
console.log(`Wybrano: ${item}`);
|
||||||
|
setMenuVisible(false);
|
||||||
|
};
|
||||||
|
|
||||||
const setCredentials = async () => {
|
const setCredentials = async () => {
|
||||||
setApiKey("ptlc_aDGU4VHNQuN5t6dyxNzVon3UZLR5ehuySmdR7xsUbMm");
|
setApiKey("ptlc_aDGU4VHNQuN5t6dyxNzVon3UZLR5ehuySmdR7xsUbMm");
|
||||||
};
|
};
|
||||||
@@ -19,24 +39,6 @@ const Index = () => {
|
|||||||
return formatDistanceToNow(date, { addSuffix: true });
|
return formatDistanceToNow(date, { addSuffix: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
const sortFiles = (files: any) => {
|
|
||||||
const data = files;
|
|
||||||
|
|
||||||
const sortedData = data.sort((a, b) => {
|
|
||||||
const isADir = !a.attributes.is_file;
|
|
||||||
const isBDir = !b.attributes.is_file;
|
|
||||||
|
|
||||||
// Najpierw katalogi, potem pliki
|
|
||||||
if (isADir && !isBDir) return -1;
|
|
||||||
if (!isADir && isBDir) return 1;
|
|
||||||
|
|
||||||
// Sortowanie alfabetyczne
|
|
||||||
return a.attributes.name.localeCompare(b.attributes.name);
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(sortedData);
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchFiles = async (apiKey: string) => {
|
const fetchFiles = async (apiKey: string) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
@@ -68,6 +70,7 @@ const Index = () => {
|
|||||||
<>
|
<>
|
||||||
{fileList.map((file) => (
|
{fileList.map((file) => (
|
||||||
<div
|
<div
|
||||||
|
isFile={file.attributes.is_file}
|
||||||
className="flex justify-between gap-4 bg-slate-400 mb-1 hover:bg-slate-500 pl-4 pr-4 pt-1 pb-1 rounded-md"
|
className="flex justify-between gap-4 bg-slate-400 mb-1 hover:bg-slate-500 pl-4 pr-4 pt-1 pb-1 rounded-md"
|
||||||
key={file.attributes.name}
|
key={file.attributes.name}
|
||||||
>
|
>
|
||||||
@@ -76,12 +79,16 @@ const Index = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="w-48 text-left">{file.attributes.name}</div>
|
<div className="w-48 text-left">{file.attributes.name}</div>
|
||||||
<div className="w-48 text-right">
|
<div className="w-48 text-right">
|
||||||
{file.attributes.is_file ? file.attributes.size : ""}
|
{file.attributes.is_file ? file.attributes.size + " bytes" : ""}
|
||||||
</div>
|
</div>
|
||||||
<div title={file.attributes.modified_at} className="w-60 text-right">
|
<div title={file.attributes.modified_at} className="w-60 text-right">
|
||||||
{formateDate(file.attributes.modified_at)}
|
{formateDate(file.attributes.modified_at)}
|
||||||
</div>
|
</div>
|
||||||
<div className="w-6 justify-center text-center justify-items-center">
|
<div
|
||||||
|
ref={buttonRef}
|
||||||
|
onClick={handleButtonClick}
|
||||||
|
className="w-6 justify-center text-center justify-items-center"
|
||||||
|
>
|
||||||
<MenuIcon />
|
<MenuIcon />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user