All files / src/pages/flowsheet-page/dynamics ResultSheet.tsx

100% Statements 28/28
100% Branches 7/7
100% Functions 9/9
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                          45x     840x 840x   840x   840x 840x 840x   840x   840x     1x 1x     19x 19x   19x   74x   820x   1x           19x     840x 840x     840x     2x       14x 2x                 840x                                                                     16x                
import { createColumns } from "@/ahuora-design-system/ui/data-table";
import { ScenarioRead, useCoreDataRowFullTableListQuery, useLazyMssDownloadResultsRetrieveQuery } from "@/api/apiStore.gen";
import { useSearchParam } from "@/hooks/searchParams";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo, useState } from "react";
import SpecificRow from "./SpecificRow";
import { PaginatedTable } from "@/ahuora-design-system/ui/paginated-table";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/ahuora-design-system/ui/tabs";
import ScenarioGraphView from "./ScenarioGraphView";
import { useLocalStorage } from "usehooks-ts";
import SelectPropertiesDialog from "../flowsheet/PropertiesSidebar/SelectPropertiesDialog";
import { SelectedPropertyInfo } from "../flowsheet/PropertiesSidebar/ObjectDetailsPanel";
 
const columnHelper = createColumnHelper();
 
export default function ResultSheet({ scenario }: { scenario: ScenarioRead }) {
    const [solve_index, setSolveIndex] = useSearchParam("solve_index");
    const [tab, setTab] = useLocalStorage<string>("result-sheet-tab", "graph");
 
    const isLookingAtFullTable = !solve_index;
 
    const [page, setPage] = useState(1)
    const { data: fullTableData } = useCoreDataRowFullTableListQuery({ scenario: scenario.id, page: page });
    const maxRow = fullTableData?.count - 1 || 0;
 
    const isDynamic = scenario.enable_dynamics;
 
    const fullInputTableColumns = useMemo(() => {
 
        function handleView(row) {
            const firstKey = Object.keys(row)[0];
            setSolveIndex(row[firstKey]);
        }
 
        const fullInputTable = fullTableData?.results || [];
        const columns = createColumns(fullInputTable)
 
        columns.push(
            columnHelper.accessor("Results", {
                header: () => "Full results",
                cell: ({ row }) => (
                    <p
                        className="cursor-pointer underline"
                        onClick={() => handleView(row.original)}
                    >View
                    </p>
                ),
            })
        )
        return columns;
    }, [fullTableData]);
 
    const [isDialogOpen, setIsDialogOpen] = useState(false);
    const [tempSelectedProperties, setTempSelectedProperties] = useState<
        SelectedPropertyInfo[]
    >([]);
    const [export_data] = useLazyMssDownloadResultsRetrieveQuery();
 
    function handleDownload() {
        setIsDialogOpen(true);
    }
 
    function handleSave() {
        const properties = JSON.stringify(tempSelectedProperties.map(prop => prop.propertyId))
        export_data({
            flowsheet: scenario.flowsheet,
            scenario: JSON.stringify({
                scenario: scenario.id,
                properties: properties,
            }),
        });
    }
 
    if (isLookingAtFullTable) return (
        <>
            <Tabs defaultValue="graph" className="flex flex-1 flex-col" value={tab} onValueChange={setTab}>
                <TabsList className="w-fit m-auto rounded my-4">
                    <TabsTrigger value="graph">Graph view</TabsTrigger>
                    <TabsTrigger value="table">Table view</TabsTrigger>
                </TabsList>
                <TabsContent value="table" className="overflow-auto">
                    <PaginatedTable
                        columns={fullInputTableColumns}
                        data={fullTableData}
                        page={page}
                        setPage={setPage}
                        className="pt-0"
                        title={isDynamic ? "Dynamic results" : "Multi-steady-state results"}
                        ariaLabel="mss-results-table"
                        handleDownload={handleDownload}
                    />
                </TabsContent>
                <TabsContent value="graph" className="overflow-auto">
                    <ScenarioGraphView scenario={scenario} />
                </TabsContent>
            </Tabs>
            
            <SelectPropertiesDialog
                isDialogOpen={isDialogOpen}
                setIsDialogOpen={setIsDialogOpen}
                tempSelectedProperties={tempSelectedProperties}
                setTempSelectedProperties={setTempSelectedProperties}
                handleSave={handleSave}
                description="Choose which properties you want to download for this result."
            />
        </>
    )
 
    return (
        <SpecificRow
            maxRow={maxRow}
            scenario={scenario}
            solve_index={solve_index}
            setSolveIndex={setSolveIndex}
        />
    )
}