import { useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useStompClient } from 'react-stomp-hooks'; import { RootState } from "../store"; import { useWsSubscription } from "../ws/subscriptions"; import { EventsObjectListResponse } from "../../types"; import IconRefresh from '@mui/icons-material/Refresh' import { Box, Button, Typography, useTheme } from "@mui/material"; import { Tree } from "react-d3-tree"; import { EventChain, EventGroup, EventGroups, set } from "../store/chained-events-slice"; import { toUnixTimestamp, UnixTimestamp } from "../features/UxTc"; import { TableCellCustomizer, TablePropetyConfig } from "../features/table/table"; import ExpandableTable, { ExpandableItem } from "../features/table/expandableTable"; import { CustomNodeElementProps } from 'react-d3-tree'; interface RawNodeDatum { name: string; attributes?: Record; children?: RawNodeDatum[]; fill?: string; } // Transformasjonsfunksjon for EventChain const transformEventChain = (eventChain: EventChain): RawNodeDatum => ({ name: eventChain.eventName, attributes: { //eventId: eventChain.eventId, created: toUnixTimestamp({ timestamp: eventChain.created }) ?? "", success: eventChain.success, skipped: eventChain.skipped, failure: eventChain.failure, childrens: eventChain.events.length }, fill: "white", children: eventChain.events.map(transformEventChain), }); // Transformasjonsfunksjon for EventGroups const transformEventGroups = (group: EventGroup): RawNodeDatum[] => { return group.events.map(transformEventChain); } interface EventGroupToTreeView extends EventGroup { underView: JSX.Element } export default function EventsChainPage() { const muiTheme = useTheme(); const dispatch = useDispatch(); const client = useStompClient(); const cursor = useSelector((state: RootState) => state.chained) const [useReferenceId, setUseReferenceId] = useState(null); const [treeData, setTreeData] = useState(null); useWsSubscription>("/topic/chained/all", (response) => { dispatch(set(response)) }); useEffect(() => { if (Object.keys(cursor).length === 0) { client?.publish({ destination: "/app/chained/all" }); } }, [cursor, client, dispatch]); const onRefresh = () => { client?.publish({ "destination": "/app/chained/all", "body": "Potato" }) } useEffect(() => { if (useReferenceId) { const eventGroup = cursor.groups.find((group) => group.referenceId === useReferenceId); if (eventGroup) { const data = transformEventGroups(eventGroup); console.log({ "info": "Tree data", "data": data }) setTreeData(data); } } }, [useReferenceId, cursor]) 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; } } default: return null; } }; const columns: Array = [ { label: "ReferenceId", accessor: "referenceId" }, { label: "File", accessor: "fileName" }, { label: "Created", accessor: "created" }, ]; function renderCustomNodeElement(nodeData: CustomNodeElementProps): JSX.Element { const attr = nodeData.nodeDatum.attributes; const nodeColor = attr?.success ? "green" : attr?.failure ? "red" : "yellow"; return (<> {nodeData.nodeDatum.name} {attr?.created && ( Created: {attr?.created} )} {attr?.success && ( Success )} {attr?.failure && ( Failure )} {attr?.skipped && ( Skipped )} {attr?.childrens && ( {attr?.childrens} derived {Number(attr?.childrens) > 1 ? "events" : "event"} )} ); } function renderExpandableItem(item: EventGroup): ExpandableItem | null { return { tag: item.referenceId, expandElement: (() => { const data = transformEventGroups(item); return ( <> {(data) ? (
) : Tree data not available} ); })() }; } return ( <> ) }