@berekuk's CR

This commit is contained in:
Quinn Dougherty 2022-07-28 10:33:31 -04:00
parent 5f3dd12542
commit ff05685634
4 changed files with 24 additions and 25 deletions

View File

@ -2,9 +2,13 @@ import React from "react";
import { SquiggleEditor } from "./SquiggleEditor"; import { SquiggleEditor } from "./SquiggleEditor";
import type { SquiggleEditorProps } from "./SquiggleEditor"; import type { SquiggleEditorProps } from "./SquiggleEditor";
import { runPartial, defaultBindings } from "@quri/squiggle-lang"; import { runPartial, defaultBindings } from "@quri/squiggle-lang";
import type { result, errorValue, bindings } from "@quri/squiggle-lang"; import type {
result,
errorValue,
bindings as bindingsType,
} from "@quri/squiggle-lang";
function resultDefault(x: result<bindings, errorValue>): bindings { function resultDefault(x: result<bindingsType, errorValue>): bindings {
switch (x.tag) { switch (x.tag) {
case "Ok": case "Ok":
return x.value; return x.value;
@ -13,24 +17,18 @@ function resultDefault(x: result<bindings, errorValue>): bindings {
} }
} }
function replaceBindings( export type SquiggleEditorWithImportedBindingsProps = SquiggleEditorProps & {
props: SquiggleEditorProps, bindingsImportUrl: string;
newBindings: bindings
): SquiggleEditorProps {
return { ...props, bindings: newBindings };
}
export type SquiggleEditorImportedBindingsProps = SquiggleEditorProps & {
bindingsImportFile: string;
}; };
export const SquiggleEditorImportedBindings: React.FC< export const SquiggleEditorWithImportedBindings: React.FC<
SquiggleEditorImportedBindingsProps SquiggleEditorWithImportedBindingsProps
> = (props) => { > = (props) => {
const { bindingsImportUrl, ...editorProps } = props;
const [bindingsResult, setBindingsResult] = React.useState({ const [bindingsResult, setBindingsResult] = React.useState({
tag: "Ok", tag: "Ok",
value: defaultBindings, value: defaultBindings,
} as result<bindings, errorValue>); } as result<bindingsType, errorValue>);
React.useEffect(() => { React.useEffect(() => {
async function retrieveBindings(fileName: string) { async function retrieveBindings(fileName: string) {
let contents = await fetch(fileName).then((response) => { let contents = await fetch(fileName).then((response) => {
@ -38,9 +36,10 @@ export const SquiggleEditorImportedBindings: React.FC<
}); });
setBindingsResult(runPartial(contents)); setBindingsResult(runPartial(contents));
} }
retrieveBindings(props.bindingsImportFile); retrieveBindings(bindingsImportUrl);
}, []); }, [bindingsImportUrl]);
const deliveredBindings = resultDefault(bindingsResult); const deliveredBindings = resultDefault(bindingsResult);
const newProps = replaceBindings(props, deliveredBindings); return (
return <SquiggleEditor {...newProps} />; <SquiggleEditor {...{ ...editorProps, bindings: deliveredBindings }} />
);
}; };

View File

@ -3,7 +3,7 @@ title: How to import squiggle files into `.mdx` documents
sidebar_position: 5 sidebar_position: 5
--- ---
import { SquiggleEditorImportedBindings } from "../../src/components/SquiggleEditor"; import { SquiggleEditorWithImportedBindings } from "../../src/components/SquiggleEditor";
_Proof of concept_ _Proof of concept_
@ -21,17 +21,17 @@ z = y.b * y.a
We can call `f(z)` upon the assignments in `demo.squiggle` like so: We can call `f(z)` upon the assignments in `demo.squiggle` like so:
```jsx ```jsx
import { SquiggleEditorImportedBindings } from "../../src/components/SquiggleEditor"; import { SquiggleEditorWithImportedBindings } from "../../src/components/SquiggleEditor";
<SquiggleEditorImportedBindings <SquiggleEditorImportedBindings
defaultCode={"f(z)"} defaultCode={"f(z)"}
bindingsImportFile={"../../squiggle/demo.squiggle"} bindingsImportFile={"../../estimates/demo.squiggle"}
/>; />;
``` ```
Which would then look exactly like Which would then look exactly like
<SquiggleEditorImportedBindings <SquiggleEditorWithImportedBindings
defaultCode={"f(z)"} defaultCode={"f(z)"}
bindingsImportFile={"../../squiggle/demo.squiggle"} bindingsImportUrl={"../../estimates/demo.squiggle"}
/> />

View File

@ -13,12 +13,12 @@ export function SquiggleEditor(props) {
); );
} }
export function SquiggleEditorImportedBindings(props) { export function SquiggleEditorWithImportedBindings(props) {
return ( return (
<BrowserOnly fallback={<FallbackSpinner height={292} />}> <BrowserOnly fallback={<FallbackSpinner height={292} />}>
{() => { {() => {
const LibComponent = const LibComponent =
require("@quri/squiggle-components").SquiggleEditorImportedBindings; require("@quri/squiggle-components").SquiggleEditorWithImportedBindings;
return <LibComponent {...props} />; return <LibComponent {...props} />;
}} }}
</BrowserOnly> </BrowserOnly>