marshall the state back and forth (#819)

This commit is contained in:
Michael Quigley 2025-01-13 14:20:44 -05:00
parent 566bf56609
commit 3041e961c4
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 35 additions and 5 deletions

View File

@ -3,7 +3,9 @@ import useStore from "./model/store.ts";
import { import {
MaterialReactTable, MaterialReactTable,
type MRT_ColumnDef, type MRT_ColumnDef,
MRT_PaginationState,
MRT_RowSelectionState, MRT_RowSelectionState,
MRT_SortingState,
useMaterialReactTable useMaterialReactTable
} from "material-react-table"; } from "material-react-table";
import {useEffect, useMemo, useRef, useState} from "react"; import {useEffect, useMemo, useRef, useState} from "react";
@ -17,8 +19,14 @@ const TabularView = () => {
const updateNodes = useStore((state) => state.updateNodes); const updateNodes = useStore((state) => state.updateNodes);
const selectedNode = useStore((state) => state.selectedNode); const selectedNode = useStore((state) => state.selectedNode);
const updateSelectedNode = useStore((state) => state.updateSelectedNode); const updateSelectedNode = useStore((state) => state.updateSelectedNode);
const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
const sparkdata = useStore((state) => state.sparkdata); const sparkdata = useStore((state) => state.sparkdata);
const storedPagination = useStore((state) => state.pagination);
const updatePagination = useStore((state) => state.updatePagination);
const storedSorting = useStore((state) => state.sorting);
const updateSorting = useStore((state) => state.updateSorting);
const [pagination, setPagination] = useState<MRT_PaginationState>({} as MRT_PaginationState);
const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
const [sorting, setSorting] = useState<MRT_SortingState>([{id: "data.label", desc: false}] as MRT_SortingState);
const [combined, setCombined] = useState<Node[]>([]); const [combined, setCombined] = useState<Node[]>([]);
useEffect(() => { useEffect(() => {
@ -39,8 +47,19 @@ const TabularView = () => {
selection[selectedNode.id] = true; selection[selectedNode.id] = true;
setRowSelection(selection); setRowSelection(selection);
} }
setPagination(storedPagination);
setSorting(storedSorting);
}, []); }, []);
useEffect(() => {
console.log("pagination", pagination);
updatePagination(pagination);
}, [pagination]);
useEffect(() => {
updateSorting(sorting);
}, [sorting]);
useEffect(() => { useEffect(() => {
let sn = nodes.find(node => Object.keys(rowSelection).includes(node.id)); let sn = nodes.find(node => Object.keys(rowSelection).includes(node.id));
updateSelectedNode(sn); updateSelectedNode(sn);
@ -119,8 +138,10 @@ const TabularView = () => {
enableRowSelection: false, enableRowSelection: false,
enableMultiRowSelection: false, enableMultiRowSelection: false,
getRowId: r => r.id, getRowId: r => r.id,
onPaginationChange: setPagination,
onRowSelectionChange: setRowSelection, onRowSelectionChange: setRowSelection,
state: { rowSelection }, onSortingChange: setSorting,
state: { pagination, rowSelection, sorting },
muiTableBodyRowProps: ({ row }) => ({ muiTableBodyRowProps: ({ row }) => ({
onClick: () => { onClick: () => {
if(rowSelection[row.id]) { if(rowSelection[row.id]) {

View File

@ -3,6 +3,7 @@ import {Environment} from "../api";
import {Graph} from "./graph.ts"; import {Graph} from "./graph.ts";
import {Edge, Node, Viewport} from "@xyflow/react"; import {Edge, Node, Viewport} from "@xyflow/react";
import {User} from "./user.ts"; import {User} from "./user.ts";
import {MRT_PaginationState, MRT_SortingState} from "material-react-table";
type StoreState = { type StoreState = {
user: User; user: User;
@ -13,6 +14,8 @@ type StoreState = {
edges: Edge[]; edges: Edge[];
selectedNode: Node; selectedNode: Node;
viewport: Viewport; viewport: Viewport;
pagination: MRT_PaginationState;
sorting: MRT_SortingState;
}; };
type StoreAction = { type StoreAction = {
@ -24,17 +27,21 @@ type StoreAction = {
updateEdges: (edges: StoreState['edges']) => void, updateEdges: (edges: StoreState['edges']) => void,
updateSelectedNode: (selectedNode: StoreState['selectedNode']) => void, updateSelectedNode: (selectedNode: StoreState['selectedNode']) => void,
updateViewport: (viewport: StoreState['viewport']) => void, updateViewport: (viewport: StoreState['viewport']) => void,
}; updatePagination: (pagination: StoreState['pagination']) => void,
updateSorting: (sorting: StoreState['sorting']) => void,
};
const useStore = create<StoreState & StoreAction>((set) => ({ const useStore = create<StoreState & StoreAction>((set) => ({
user: null, user: null,
graph: new Graph(), graph: new Graph(),
environments: new Array<Environment>(), environments: new Array<Environment>(),
sparkdata: new Map<string, Sparkdata>(), sparkdata: new Map<string, Number[]>(),
nodes: [], nodes: [],
edges: [], edges: [],
selectedNode: null, selectedNode: null,
viewport: {x: 0, y: 0, zoom: 1}, viewport: {x: 0, y: 0, zoom: 1},
pagination: {pageIndex: 0, pageSize: 15},
sorting: [{id: "data.label", desc: false}] as MRT_SortingState,
updateUser: (user) => set({user: user}), updateUser: (user) => set({user: user}),
updateGraph: (vov) => set({overview: vov}), updateGraph: (vov) => set({overview: vov}),
updateEnvironments: (environments) => set({environments: environments}), updateEnvironments: (environments) => set({environments: environments}),
@ -42,7 +49,9 @@ const useStore = create<StoreState & StoreAction>((set) => ({
updateNodes: (nodes) => set({nodes: nodes}), updateNodes: (nodes) => set({nodes: nodes}),
updateEdges: (edges) => set({edges: edges}), updateEdges: (edges) => set({edges: edges}),
updateSelectedNode: (selectedNode) => set({selectedNode: selectedNode}), updateSelectedNode: (selectedNode) => set({selectedNode: selectedNode}),
updateViewport: (viewport) => set({viewport: viewport}) updateViewport: (viewport) => set({viewport: viewport}),
updatePagination: (pagination) => set({pagination: pagination}),
updateSorting: (sorting) => set({sorting: sorting})
})); }));
export default useStore; export default useStore;