Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions libs/@hashintel/petrinaut/src/views/SDCPN/components/mini-map.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { css } from "@hashintel/ds-helpers/css";
import { use, useMemo } from "react";
import type { MiniMapNodeProps, MiniMapProps } from "reactflow";
import { MiniMap as ReactFlowMiniMap, useStore } from "reactflow";

import { PANEL_MARGIN } from "../../../constants/ui";
import { hexToHsl } from "../../../lib/hsl-color";
import { EditorContext } from "../../../state/editor-context";
import type { NodeType } from "../reactflow-types";

const miniMapClassName = css({
"& svg": {
borderRadius: "md.3",
},
});

/** Default colors for nodes without a type color */
const DEFAULT_PLACE_FILL = "#f8f8f8";
const DEFAULT_PLACE_STROKE = "#666666";
const DEFAULT_TRANSITION_FILL = "#6b7280";

const PLACE_STROKE_WIDTH = 2;

/**
* Custom node renderer for the MiniMap.
* Renders place nodes as circles and transition nodes as rectangles.
*/
const MiniMapNode: React.FC<MiniMapNodeProps> = ({
id,
x,
y,
width,
height,
}) => {
// MiniMapNodeProps doesn't include node data, so we look it up from the store
const node = useStore(
(state) => state.nodeInternals.get(id) as NodeType | undefined,
);

// Compute colors based on node type and type color
const { fill, stroke } = useMemo(() => {
if (node?.data.type === "place") {
const typeColor = node.data.typeColor;

if (typeColor) {
const hsl = hexToHsl(typeColor);
return {
fill: hsl.lighten(20).css(0.9),
stroke: hsl.lighten(-15).saturate(-20).css(1),
};
}

return { fill: DEFAULT_PLACE_FILL, stroke: DEFAULT_PLACE_STROKE };
}

// Transitions: solid grey with no stroke
return { fill: DEFAULT_TRANSITION_FILL, stroke: undefined };
}, [node?.data]);

if (node?.data.type === "place") {
const radius = Math.min(width, height) / 2 - PLACE_STROKE_WIDTH / 2;
return (
<circle
cx={x + width / 2}
cy={y + height / 2}
r={Math.max(radius, 1)}
fill={fill}
stroke={stroke}
strokeWidth={PLACE_STROKE_WIDTH}
/>
);
}

return <rect x={x} y={y} width={width} height={height} fill={fill} />;
};

/**
* A wrapper around ReactFlow's MiniMap with custom styling.
* Renders place nodes as circles and transition nodes as rectangles.
* Positions at top-right, offset by properties panel width when visible.
*/
export const MiniMap: React.FC<Omit<MiniMapProps, "style">> = (props) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type signature allows props that are silently ignored

Low Severity

The component type Omit<MiniMapProps, "style"> suggests all MiniMapProps except style can be customized, but props like className, maskColor, maskStrokeWidth, nodeComponent, and offsetScale are hardcoded after the spread and will be silently overwritten. This creates a misleading API where callers could pass these props expecting them to work, but they'll be ignored. The type should exclude all fixed props to accurately reflect the component's actual API.

Fix in CursorΒ Fix in Web

const { selectedResourceId, propertiesPanelWidth } = use(EditorContext);

const isPropertiesPanelVisible = selectedResourceId !== null;
const rightOffset = isPropertiesPanelVisible
? propertiesPanelWidth + PANEL_MARGIN * 2
: PANEL_MARGIN;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MiniMap offset ignores non-resource panel visibility

Low Severity

MiniMap decides whether the properties panel is visible using only selectedResourceId !== null. If the properties panel can be shown for selections that don’t set selectedResourceId (e.g., edge selection, pinned-open panel, or other UI flows), rightOffset stays small and the MiniMap can overlap the panel.

Fix in CursorΒ Fix in Web


return (
<ReactFlowMiniMap
{...props}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spreading {...props} before className means any caller-provided className will be ignored here. If callers are expected to customize this wrapper, that behavior may be surprising.

Fix This in Augment

πŸ€– Was this useful? React with πŸ‘ or πŸ‘Ž

ariaLabel=""
className={miniMapClassName}
style={{
top: 0,
right: rightOffset,
bottom: "auto",
left: "auto",
width: 130,
height: 73,
}}
maskColor="rgba(0, 0, 0, 0.15)"
maskStrokeWidth={0}
nodeComponent={MiniMapNode}
offsetScale={2}
/>
);
};
2 changes: 2 additions & 0 deletions libs/@hashintel/petrinaut/src/views/SDCPN/sdcpn-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EditorContext } from "../../state/editor-context";
import { SDCPNContext } from "../../state/sdcpn-context";
import { useIsReadOnly } from "../../state/use-is-read-only";
import { Arc } from "./components/arc";
import { MiniMap } from "./components/mini-map";
import { PlaceNode } from "./components/place-node";
import { TransitionNode } from "./components/transition-node";
import { useApplyNodeChanges } from "./hooks/use-apply-node-changes";
Expand Down Expand Up @@ -341,6 +342,7 @@ export const SDCPNView: React.FC = () => {
zoomOnScroll
>
<Background gap={SNAP_GRID_SIZE} size={1} />
<MiniMap pannable zoomable />
</ReactFlow>
</div>
);
Expand Down
Loading