-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add an
EvalBoundedInterp
component
This is more or less a drop-in replacement for `EvalFull` and is ready to go, save for hackworthltd/primer#1247. Therefore, we don't yet use it in the `Edit` and `PictureInPicture` components. Signed-off-by: Drew Hess <[email protected]>
- Loading branch information
Showing
5 changed files
with
183 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/components/EvalBoundedInterp/EvalBoundedInterp.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { tree3 } from "../examples/trees"; | ||
|
||
import { EvalBoundedInterp } from "./"; | ||
|
||
const meta: Meta<typeof EvalBoundedInterp> = { | ||
title: "Application/Component Library/EvalBoundedInterp", | ||
component: EvalBoundedInterp, | ||
decorators: [ | ||
(Story) => ( | ||
<div className="h-screen"> | ||
<Story /> | ||
</div> | ||
), | ||
], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof EvalBoundedInterp>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
moduleName: ["Primer", "Examples"], | ||
evalBoundedInterp: { | ||
request: () => { | ||
return; | ||
}, | ||
result: { tag: "EvalBoundedInterpRespNormal", contents: tree3 }, | ||
}, | ||
level: "Expert", | ||
defs: ["footballGame", "whatsopposite"], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { useState } from "react"; | ||
import { NodeChange, ReactFlowProvider, useReactFlow } from "reactflow"; | ||
import { EvalBoundedInterpResp, GlobalName, Level } from "@/primer-api"; | ||
import { SelectMenu, TreeReactFlowOne } from "@/components"; | ||
import { | ||
TreeReactFlowOneProps, | ||
defaultTreeReactFlowProps, | ||
} from "../TreeReactFlow"; | ||
|
||
export type EvalBoundedInterpProps = { | ||
moduleName: string[]; | ||
evalBoundedInterp: { | ||
request: (baseName: string | undefined) => void; | ||
result?: EvalBoundedInterpResp; | ||
}; | ||
level: Level; | ||
defs: string[]; | ||
initialEvalDef: string | undefined; | ||
extraTreeProps: Partial<TreeReactFlowOneProps>; | ||
}; | ||
|
||
const Evaluated = (p: { | ||
defName: GlobalName; | ||
evaluated?: EvalBoundedInterpResp; | ||
level: Level; | ||
extraTreeProps: Partial<TreeReactFlowOneProps>; | ||
}) => { | ||
const padding = 1.0; | ||
const { fitView } = useReactFlow(); | ||
const onNodesChange = (_: NodeChange[]) => { | ||
fitView({ padding }); | ||
}; | ||
const resultTree = () => { | ||
switch (p?.evaluated?.tag) { | ||
case "EvalBoundedInterpRespNormal": | ||
return { tree: p.evaluated.contents }; | ||
default: | ||
// This should be some indication of an error having occurred, | ||
// but our UI is a bit too limited for that at the moment. | ||
return {}; | ||
} | ||
}; | ||
|
||
return ( | ||
<TreeReactFlowOne | ||
{...defaultTreeReactFlowProps} | ||
{...resultTree()} | ||
level={p.level} | ||
zoomBarProps={{ padding }} | ||
onNodesChange={onNodesChange} | ||
fitViewOptions={{ padding }} | ||
{...p.extraTreeProps} | ||
/> | ||
); | ||
}; | ||
|
||
const disableEval = "<disabled>"; | ||
|
||
// We only offer to evaluate the definitions in the "main" module | ||
export const EvalBoundedInterp = ({ | ||
defs, | ||
evalBoundedInterp, | ||
moduleName, | ||
level, | ||
initialEvalDef, | ||
extraTreeProps, | ||
}: EvalBoundedInterpProps): JSX.Element => { | ||
const [evalDef, setEvalDef0] = useState(initialEvalDef ?? disableEval); | ||
const setEvalDef = (e: string) => { | ||
setEvalDef0(e); | ||
evalBoundedInterp.request(e === disableEval ? undefined : e); | ||
}; | ||
return ( | ||
<div className="flex h-full flex-col overflow-auto"> | ||
<div className="mx-2"> | ||
<SelectMenu | ||
label="Definition" | ||
selected={evalDef} | ||
options={[disableEval].concat(defs)} | ||
optionType="code" | ||
onChange={(selection: string) => setEvalDef(selection)} | ||
/> | ||
</div> | ||
{evalDef !== disableEval && ( | ||
<> | ||
<div className="grow"> | ||
<ReactFlowProvider> | ||
<Evaluated | ||
key={evalDef} | ||
defName={{ qualifiedModule: moduleName, baseName: evalDef }} | ||
{...(evalBoundedInterp.result | ||
? { evaluated: evalBoundedInterp.result } | ||
: {})} | ||
level={level} | ||
extraTreeProps={extraTreeProps} | ||
/> | ||
</ReactFlowProvider> | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default EvalBoundedInterp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters