import { useContext, useMemo, useState } from 'react'; import { firstBy } from 'thenby'; import classNames from 'classnames'; import { useEscapeKey } from 'components/hooks'; import { objectToArray } from 'lib/data'; import { ReportContext } from '../[reportId]/Report'; // eslint-disable-next-line css-modules/no-unused-class import styles from './JourneyView.module.css'; const NODE_HEIGHT = 60; const NODE_GAP = 10; const LINE_WIDTH = 3; export default function JourneyView() { const [selectedNode, setSelectedNode] = useState(null); const [activeNode, setActiveNode] = useState(null); const { report } = useContext(ReportContext); const { data, parameters } = report || {}; useEscapeKey(() => setSelectedNode(null)); const columns = useMemo(() => { if (!data) { return []; } return Array(Number(parameters.steps)) .fill(undefined) .map((column = {}, index) => { data.forEach(({ items, count }) => { const name = items[index]; const selectedNodes = selectedNode?.paths ?? []; if (name) { if (!column[name]) { const selected = !!selectedNodes.find(a => a.items[index] === name); const paths = data.filter((d, i) => { return i !== index && d.items[index] === name; }); const from = index > 0 && selected && paths.reduce((obj, path) => { const { items, count } = path; const name = items[index - 1]; if (!obj[name]) { obj[name] = { name, count: +count }; } else { obj[name].count += +count; } return obj; }, {}); column[name] = { name, total: +count, columnIndex: index, selected, paths, from: objectToArray(from), }; } else { column[name].total += +count; } } }); return { nodes: objectToArray(column).sort(firstBy('total', -1)), }; }); }, [data, selectedNode]); const handleClick = (item: string, index: number, paths: any[]) => { if (item !== selectedNode?.item || index !== selectedNode?.index) { setSelectedNode({ item, index, paths }); } else { setSelectedNode(null); } }; if (!data) { return null; } //console.log({ columns, selectedNode, activeNode }); return (