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, TableRowGroupedItem } from "../features/table/table"; import ExpandableTable from "../features/table/expandableTable"; import { CustomNodeElementProps } from 'react-d3-tree'; export type ExpandableItemRow = TableRowGroupedItem & EventGroup 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: ExpandableItemRow): RawNodeDatum[] => { return group.items.map(transformEventChain); } 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 [tableItems, setTableItems] = useState>([]); useWsSubscription>("/topic/chained/all", (response) => { dispatch(set(response)) console.log(response) }); useEffect(() => { const items = cursor.groups.map((group: EventGroup) => { const created = group.events[0]?.created ?? 0; return { rowId: group.referenceId, title: group.fileName ?? group.referenceId, items: group.events, created: created, referenceId: group.referenceId, } as ExpandableItemRow }); setTableItems(items); console.log("tableItems", items) }, [cursor]); useEffect(() => { client?.publish({ destination: "/app/chained/all" }); }, [client, dispatch]); const onRefresh = () => { client?.publish({ "destination": "/app/chained/all", }) } const createTableCell: TableCellCustomizer = (accessor, data) => { console.log(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: "title" }, { 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: ExpandableItemRow): JSX.Element | null { console.log(item); const data = transformEventGroups(item); return ( <> {(data) ? (
) : Tree data not available} ); } return ( <> ) }