import { useLocation, useNavigate, useParams } from "react-router-dom"; import { useReducer, useEffect } from "react"; import { tabsReducer } from "../reducers/tabsReducer"; import { AlgorithmContent } from "../components/algorithm/AlgorithmContent"; import { DatasetContent } from "../components/dataset/DatasetContent"; import { AlgorithmTable, DatasetTable, } from "../components/tables/DatasetAlgorithmTables"; import { useBenchmarkChartData } from "../hooks/useBenchmarkChartData"; import { useTagFilter } from "../hooks/useTagFilter"; import { BenchmarkData } from "../types"; interface BenchmarkViewProps { benchmarkData: BenchmarkData | null; } export default function BenchmarkView({ benchmarkData }: BenchmarkViewProps) { const location = useLocation(); const navigate = useNavigate(); const { datasetName, algorithmName } = useParams<{ datasetName?: string; algorithmName?: string; }>(); const [tabsState, dispatch] = useReducer(tabsReducer, { tabs: [ { id: "datasets", label: "Datasets", route: "/datasets" }, { id: "algorithms", label: "Algorithms", route: "/algorithms" }, ], activeTabId: "datasets", }); // Effect to handle URL changes and update tabs useEffect(() => { if (datasetName) { dispatch({ type: "ADD_TAB", payload: { id: `dataset-${datasetName}`, label: datasetName, route: `/dataset/${datasetName}`, }, }); } else if (algorithmName) { dispatch({ type: "ADD_TAB", payload: { id: `algorithm-${algorithmName}`, label: algorithmName, route: `/algorithm/${algorithmName}`, }, }); } else if (location.pathname.includes("/algorithms")) { dispatch({ type: "SET_ACTIVE_TAB", payload: "algorithms" }); } else if (location.pathname.includes("/datasets")) { dispatch({ type: "SET_ACTIVE_TAB", payload: "datasets" }); } }, [datasetName, algorithmName, location.pathname]); // Handle tab click const handleTabClick = (tabId: string, route: string) => { dispatch({ type: "SET_ACTIVE_TAB", payload: tabId }); navigate(route); }; // Get specific dataset or algorithm if viewing one const dataset = datasetName ? benchmarkData?.datasets.find((d) => d.name === datasetName) : undefined; const algorithm = algorithmName ? benchmarkData?.algorithms.find((a) => a.name === algorithmName) : undefined; // Get chart data for specific dataset or algorithm view const chartData = useBenchmarkChartData( benchmarkData?.results || [], dataset?.name || null, algorithm?.name || null, ); // Get selected tags from URL const searchParams = new URLSearchParams(location.search); const selectedTags = searchParams.get("tag")?.split(",") || []; // Set up tag filtering for datasets and algorithms const { availableTags: availableDatasetTags, filteredItems: filteredDatasets, } = useTagFilter( benchmarkData?.datasets || [], location.pathname.includes("/datasets") ? selectedTags : [], ); const { availableTags: availableAlgorithmTags, filteredItems: filteredAlgorithms, } = useTagFilter( benchmarkData?.algorithms || [], location.pathname.includes("/algorithms") ? selectedTags : [], ); // Handle tag toggling by updating URL const handleTagToggle = (tag: string) => { const newTags = selectedTags.includes(tag) ? selectedTags.filter((t) => t !== tag) : [...selectedTags, tag]; const params = new URLSearchParams(); if (newTags.length > 0) { params.set("tag", newTags.join(",")); } navigate({ search: params.toString() }); }; return (
{tabsState.tabs.map((tab) => (
{tab.id !== "datasets" && tab.id !== "algorithms" && ( )}
))}
{dataset ? ( ) : algorithm ? ( ) : tabsState.activeTabId === "datasets" ? ( ) : ( )}
); }