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 | 45x 5390x 33436x 5390x 5390x 5390x 5390x 5390x 5390x 5872x 5872x 5390x 11x 11x 11x 11x 11x 11x 5390x 5872x 5871x 5871x 5871x 11x | import { GroupingRead } from "@/api/apiStore.gen";
import {
useFlowsheetTabFromGroupId,
useSwitchToGroup,
} from "@/hooks/projectTabs";
import { Group } from "lucide-react";
import {
useFlowsheetUnitOps,
useGetGroupFromSimobject,
useUpdateCoordinatesOnSwitchGroup as useSwitchGroup,
} from "../hooks/flowsheetObjects";
import { RegisterCommand } from "./CommandProvider";
import { defineCommand } from "just-search-it";
type GroupEntry = {
group?: GroupingRead;
simulationObjectName: string;
};
const SwitchGroup = defineCommand<[groupId: number], void>("switchGroup");
function SwitchGroupCommands({ setFlowsheetSurfaceState, zoomToFit }) {
const simulationObjects = useFlowsheetUnitOps()?.filter(
(obj) => obj.objectType === "group",
);
const getGroupFromSimulationObject = useGetGroupFromSimobject();
const switchGroup = useSwitchGroup(setFlowsheetSurfaceState, zoomToFit);
const openTab = useSwitchToGroup();
const getFlowsheetTabFromGroupId = useFlowsheetTabFromGroupId();
const groupEntries: GroupEntry[] = [];
simulationObjects?.forEach((obj) => {
const group = getGroupFromSimulationObject(obj.id);
groupEntries.push({
group: group,
simulationObjectName: obj.componentName!,
});
});
const action = (group: GroupingRead) => {
const flowsheetTab = getFlowsheetTabFromGroupId(group.id);
// Try-catch added to prevent firefox from crashing.
// This needs to be investigated further as this also happens on Develop (but doesn't crash the page).
// Crash happens when creating a new project, adding a pump, go back to home, then going back to the project.
// It seems like the error is related to the search params being added to the URL before the flowsheet loads.
// Console error: Too many calls to Location or History APIs within a short timeframe.
// Console error: Error switching group: DOMException: The operation is insecure.
try {
if (flowsheetTab) {
openTab(flowsheetTab);
if (group) {
switchGroup(group.id);
}
}
} catch (error) {
console.error("Error switching group:", error);
}
};
return (
<>
{groupEntries.map((entry, index) => {
if (!entry.group) return null;
// Check if tab exists for that group
const flowsheetTab = getFlowsheetTabFromGroupId(entry.group.id);
Iif (!flowsheetTab) return null;
return (
<RegisterCommand
key={entry.simulationObjectName}
command={SwitchGroup}
args={[entry.group.id]}
name={`Switch to ${entry.simulationObjectName}`}
description={`Switch to ${entry.simulationObjectName}`}
group="Flowsheet"
icon={<Group className="icon-ls" />}
action={() => action(entry.group!)}
/>
);
})}
</>
);
}
export { SwitchGroupCommands, SwitchGroup };
|