import { useEffect, useState } from 'react'; import { UnixTimestamp } from '../features/UxTc'; import { Box, Button, IconButton, Typography, useTheme } from '@mui/material'; import { useDispatch, useSelector } from 'react-redux'; import { RootState } from '../store'; import { TableCellCustomizer, TablePropetyConfig, TableRowActionEvents } from '../features/table/table'; import { useStompClient } from 'react-stomp-hooks'; import { useWsSubscription } from '../ws/subscriptions'; import { updateItems } from '../store/explorer-slice'; import { setContextMenuPosition, setContextMenuVisible } from '../store/context-menu-slice'; import FolderIcon from '@mui/icons-material/Folder'; import IconForward from '@mui/icons-material/ArrowForwardIosRounded'; import IconHome from '@mui/icons-material/Home'; import { ExplorerItem, ExplorerCursor, ExplorerItemType } from '../../types'; import ContextMenu, { ContextMenuActionEvent, ContextMenuItem } from '../features/ContextMenu'; import { canConvert, canEncode, canExtract } from '../../fileUtil'; import SimpleTable from '../features/table/sortableTable'; import TagIcon from '@mui/icons-material/Tag'; const createTableCell: TableCellCustomizer = (accessor, data) => { switch (accessor) { case "created": { if (typeof data[accessor] === "number") { const timestampObject = { timestamp: data[accessor] as number }; // Opprett et objekt med riktig struktur return UnixTimestamp(timestampObject); } else { return null; } } case "extension": { if (data[accessor] === null) { return } else { return {data[accessor]} } } default: return null; } }; const columns: Array = [ { label: "Name", accessor: "name" }, { label: "Format", accessor: "extension" }, { label: "Created", accessor: "created" }, ]; type Segment = { name: string; absolutePath: string; }; function isWindowsPath(path: string): boolean { return /^[A-Za-z]:\\/.test(path); } function getPartFor(path: string, index: number): string | null { const separator = path.includes("/") ? "/" : "\\"; let parts: string[]; if (isWindowsPath(path)) { parts = [path.slice(0, 3), ...path.slice(3).split(separator)]; } else if (path.startsWith(separator)) { parts = [separator, ...path.slice(1).split(separator)]; } else { parts = path.split(separator); } if (index < parts.length) { // Unngå å legge til ekstra backslash for første element i Windows-path if (isWindowsPath(path) && index === 0) { return parts[0]; } return parts.slice(0, index + 1).join(separator); } return null; } function getSegments(absolutePath: string): Array { if (!absolutePath) return []; const isWindows = isWindowsPath(absolutePath); const separator = isWindows ? "\\" : "/"; let parts: string[]; if (isWindows) { parts = [absolutePath.slice(0, 3), ...absolutePath.slice(3).split(separator)]; } else if (absolutePath.startsWith(separator)) { parts = [separator, ...absolutePath.slice(1).split(separator)]; } else { parts = absolutePath.split(separator); } const segments = parts.map((value, index) => { const name = isWindows && index === 0 ? value[0] : value; return { name: name.replaceAll(":", ""), absolutePath: getPartFor(absolutePath, index)! }; }); console.log({ segments: segments, path: absolutePath }); return segments.filter((segment) => segment.name !== ""); } function getSegmentedNaviagatablePath(rootClick: () => void, navigateTo: (path: string | null) => void, path: string | null): JSX.Element { const segments = getSegments(path!) const utElements = segments.map((segment: Segment, index: number) => { return ( { segments.length > 1 && index < segments.length - 1 && } ) }); return ( {isWindowsPath(path!) && ( rootClick()} > )} {utElements} ) } function getContextMenuFileActionMenuItems(row: ExplorerItem | null): ContextMenuItem[] { const ext = row?.extension; const items: Array = []; if (!ext) {return items;} if (canEncode(ext) && canExtract(ext)) { items.push({ actionIndex: 0, icon: null, text: "All available" } as ContextMenuItem) } if (canEncode(ext)) { items.push({ actionIndex: 1, icon: null, text: "Encode" } as ContextMenuItem) } if (canExtract(ext)) { items.push({ actionIndex: 2, icon: null, text: "Extract" } as ContextMenuItem) } if (canConvert(ext)) { items.push({ actionIndex: 3, icon: null, text: "Convert" } as ContextMenuItem) } console.log(items); return items; } interface ExplorerOperationRequest { destination: string; file: string; source: string; mode: "FLOW" | "MANUAL"; } export default function ExplorePage() { const muiTheme = useTheme(); const dispatch = useDispatch(); const client = useStompClient(); const cursor = useSelector((state: RootState) => state.explorer) const [selectedRow, setSelectedRow] = useState(null); const [actionableItems, setActionableItems] = useState>([]); const navigateTo = (path: string | null) => { console.log(path) if (path) { client?.publish({ destination: "/app/explorer/navigate", body: path }) } } const onItemSelectedEvent: TableRowActionEvents = { click: (row: ExplorerItem) => { setSelectedRow(row) }, doubleClick: (row: ExplorerItem) => { console.log(row); if (row.type === "FOLDER") { navigateTo(row.path); } }, contextMenu: (row: ExplorerItem, x: number, y: number) => { if (row.type === "FOLDER") { return; } dispatch(setContextMenuVisible(true)) dispatch(setContextMenuPosition({x: x, y: y})) setActionableItems(getContextMenuFileActionMenuItems(row)) } } const onContextMenuItemClickedEvent: ContextMenuActionEvent = { selected:(actionIndex: number | null, value: ExplorerItem | null) => { if (!value) { return; } const payload = (() => { switch(actionIndex) { case 0: { return { destination: "request/all", file: value.path, source: `Web UI @ ${window.location.href}`, mode: "FLOW" } as ExplorerOperationRequest } case 1: { return { destination: "request/encode", file: value.path, source: `Web UI @ ${window.location.href}`, mode: "FLOW" } as ExplorerOperationRequest } case 2: { return { destination: "request/extract", file: value.path, source: `Web UI @ ${window.location.href}`, mode: "FLOW" } as ExplorerOperationRequest } case 3: { return { destination: "request/convert", file: value.path, source: `Web UI @ ${window.location.href}`, mode: "FLOW" } as ExplorerOperationRequest } default: { return null; } } })(); if (payload) { client?.publish({ destination: "/app/"+payload.destination, body: JSON.stringify(payload) }) } } } const onHomeClick = () => { client?.publish({ destination: "/app/explorer/home" }) } const onRootClick = () => { client?.publish({ destination: "/app/explorer/root" }) } useWsSubscription("/topic/explorer/go", (response) => { dispatch(updateItems(response)) }); useEffect(() => { // Kjør din funksjon her når komponenten lastes inn for første gang // Sjekk om cursor er null if (cursor.path === null && client !== null) { console.log(cursor) // Kjør din funksjon her når cursor er null og client ikke er null client?.publish({ destination: "/app/explorer/home" }); // Alternativt, du kan dispatche en Redux handling her // dispatch(fetchDataAction()); // Eksempel på å dispatche en handling } }, [cursor, client, dispatch]); return ( <> {getSegmentedNaviagatablePath(onRootClick, navigateTo, cursor?.path)} ) }