Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repro for React Flow issue: working version (React Flow v11.10.3) #1174

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 1 addition & 7 deletions src/components/EvalBoundedInterp/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from "react";
import { NodeChange, ReactFlowProvider, useReactFlow } from "reactflow";
import { ReactFlowProvider } from "reactflow";
import { EvalBoundedInterpResp, GlobalName, Level } from "@/primer-api";
import { SelectMenu, TreeReactFlowOne } from "@/components";
import {
Expand All @@ -26,10 +26,6 @@ const Evaluated = (p: {
extraTreeProps: Partial<TreeReactFlowOneProps>;
}) => {
const padding = 1.0;
const { fitView } = useReactFlow();
const onNodesChange = (_: NodeChange[]) => {
fitView({ padding });
};
const resultTree = () => {
switch (p?.evaluated?.tag) {
case "EvalBoundedInterpRespNormal":
Expand All @@ -47,8 +43,6 @@ const Evaluated = (p: {
{...resultTree()}
level={p.level}
zoomBarProps={{ padding }}
onNodesChange={onNodesChange}
fitViewOptions={{ padding }}
{...p.extraTreeProps}
/>
);
Expand Down
8 changes: 1 addition & 7 deletions src/components/EvalFull/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from "react";
import { NodeChange, ReactFlowProvider, useReactFlow } from "reactflow";
import { ReactFlowProvider } from "reactflow";
import { EvalFullResp, GlobalName, Level } from "@/primer-api";
import { SelectMenu, TreeReactFlowOne } from "@/components";
import {
Expand All @@ -26,19 +26,13 @@ const Evaluated = (p: {
extraTreeProps: Partial<TreeReactFlowOneProps>;
}) => {
const padding = 1.0;
const { fitView } = useReactFlow();
const onNodesChange = (_: NodeChange[]) => {
fitView({ padding });
};

return (
<TreeReactFlowOne
{...defaultTreeReactFlowProps}
{...(p?.evaluated ? { tree: p?.evaluated?.contents } : {})}
level={p.level}
zoomBarProps={{ padding }}
onNodesChange={onNodesChange}
fitViewOptions={{ padding }}
{...p.extraTreeProps}
/>
);
Expand Down
8 changes: 1 addition & 7 deletions src/components/SelectionInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeChange, ReactFlowProvider, useReactFlow } from "reactflow";
import { ReactFlowProvider } from "reactflow";
import { Tree, Level, TypeOrKind } from "@/primer-api";
import { TreeReactFlowOne } from "@/components";
import {
Expand All @@ -18,19 +18,13 @@ const TypeOrKindTree = (p: {
extraTreeProps: Partial<TreeReactFlowOneProps>;
}) => {
const padding = 1.0;
const { fitView } = useReactFlow();
const onNodesChange = (_: NodeChange[]) => {
fitView({ padding });
};

return (
<TreeReactFlowOne
{...defaultTreeReactFlowProps}
tree={p.typeOrKind}
level={p.level}
zoomBarProps={{ padding }}
onNodesChange={onNodesChange}
fitViewOptions={{ padding }}
{...p.extraTreeProps}
/>
);
Expand Down
77 changes: 3 additions & 74 deletions src/components/TreeReactFlow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Level,
TypeDef,
} from "@/primer-api";
import type { NodeChange } from "reactflow";
import {
ReactFlow,
Node as RFNode,
Expand All @@ -22,7 +21,6 @@ import {
EdgeProps,
getBezierPath,
EdgeTypes,
useReactFlow,
} from "reactflow";
import "./reactflow.css";
import { MutableRefObject, PropsWithChildren, useId } from "react";
Expand Down Expand Up @@ -74,27 +72,6 @@ import { mapSnd } from "fp-ts/lib/Tuple";

export type ScrollToDef = (defName: string) => void;

/**
* Only the `FitViewOptions` from `ReactFlow` that we want to expose.
*/
export type FitViewOptions = {
padding?: number;
};

/**
* A subset of the properties that `ReactFlow` supports and that we want to
* expose to users of `TreeReactFlow` and `TreeReactFlowOne`.
*/
export type OnNodesChange = (nodesChanges: NodeChange[]) => void;
type ReactFlowParams = {
onNodesChange?: OnNodesChange;

/**
* Options that are passed to the initial `fitView` call.
*/
fitViewOptions?: FitViewOptions;
};

/** These properties are needed to construct nodes, but are invariant across all nodes. */
export type NodeStyle = "corner" | "inline";
type NodeParams = {
Expand Down Expand Up @@ -126,8 +103,7 @@ export type TreeReactFlowProps = {
scrollToDefRef: MutableRefObject<ScrollToDef | undefined>;
scrollToTypeDefRef: MutableRefObject<ScrollToDef | undefined>;
zoomBarProps: ZoomBarProps;
} & NodeParams &
ReactFlowParams;
} & NodeParams;
export const defaultTreeReactFlowProps: Pick<
TreeReactFlowProps,
"treePadding" | "forestLayout" | "defParams" | "layout" | keyof NodeParams
Expand Down Expand Up @@ -1246,10 +1222,6 @@ export const TreeReactFlow = (p: PropsWithChildren<TreeReactFlowProps>) => {
}
zoomBarProps={p.zoomBarProps}
>
<SetTreeReactFlowCallbacks
scrollToDefRef={p.scrollToDefRef}
scrollToTypeDefRef={p.scrollToTypeDefRef}
/>
{p.children}
</Trees>
);
Expand All @@ -1261,8 +1233,7 @@ export type TreeReactFlowOneProps = {
onNodeClick?: (event: React.MouseEvent, node: Positioned<PrimerNode>) => void;
layout: LayoutParams;
zoomBarProps: ZoomBarProps;
} & NodeParams &
ReactFlowParams;
} & NodeParams;

/** Renders one `APITree` (e.g. one type or one term) on its own individual canvas.
* This is essentially a much simpler version of `TreeReactFlow`.
Expand Down Expand Up @@ -1301,8 +1272,7 @@ const Trees = <N,>(
node: Positioned<PrimerNode<N>>
) => void;
zoomBarProps: ZoomBarProps;
}> &
ReactFlowParams
}>
): JSX.Element => {
const trees = usePromise([], p.makeTrees);
const { nodes, edges } = combineGraphs([
Expand All @@ -1324,8 +1294,6 @@ const Trees = <N,>(
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
proOptions={{ hideAttribution: true, account: "paid-pro" }}
onNodesChange={p.onNodesChange}
fitViewOptions={p.fitViewOptions}
>
<Background gap={25} size={1.6} color="#81818a" />
<ZoomBar {...p.zoomBarProps} />
Expand All @@ -1334,45 +1302,6 @@ const Trees = <N,>(
);
};

// This component is rendered purely for side effects. We do this
// rather than wrap our `ReactFlow` components with
// `ReactFlowProvider`s.
const SetTreeReactFlowCallbacks = ({
scrollToDefRef,
scrollToTypeDefRef,
}: {
scrollToDefRef: MutableRefObject<ScrollToDef | undefined>;
scrollToTypeDefRef: MutableRefObject<ScrollToDef | undefined>;
}) => {
const { fitView, getZoom } = useReactFlow();

const scrollToDef: ScrollToDef = (defName: string) => {
// Don't change the current zoom level when scrolling to a
// definition.
const zoomLevel: number = getZoom();
fitView({
nodes: [{ id: defNameToNodeId(defName) }],
minZoom: zoomLevel,
maxZoom: zoomLevel,
});
};
scrollToDefRef.current = scrollToDef;

const scrollToTypeDef: ScrollToDef = (defName: string) => {
// Don't change the current zoom level when scrolling to a
// definition.
const zoomLevel: number = getZoom();
fitView({
nodes: [{ id: typeDefNameToNodeId(defName) }],
minZoom: zoomLevel,
maxZoom: zoomLevel,
});
};
scrollToTypeDefRef.current = scrollToTypeDef;

return <></>;
};

/** A more strongly-typed version of the `ReactFlow` component.
* This allows us to use a more refined node type,
* check that we register its subtypes correctly with ReactFlow,
Expand Down
Loading