Merge pull request #457 from quantified-uncertainty/no-types-mode

A simple showTypes prop
This commit is contained in:
Sam Nolan 2022-05-02 13:07:46 -04:00 committed by GitHub
commit 0c4305948f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 26 deletions

View File

@ -33,10 +33,18 @@ const variableBox = {
`, `,
}; };
export const VariableBox: React.FC<{ interface VariableBoxProps {
heading: string; heading: string;
children: React.ReactNode; children: React.ReactNode;
}> = ({ heading = "Error", children }) => { showTypes?: boolean;
}
export const VariableBox: React.FC<VariableBoxProps> = ({
heading = "Error",
children,
showTypes = false,
}: VariableBoxProps) => {
if (showTypes) {
return ( return (
<variableBox.Component> <variableBox.Component>
<variableBox.Heading> <variableBox.Heading>
@ -45,6 +53,9 @@ export const VariableBox: React.FC<{
<variableBox.Body>{children}</variableBox.Body> <variableBox.Body>{children}</variableBox.Body>
</variableBox.Component> </variableBox.Component>
); );
} else {
return <>{children}</>;
}
}; };
let RecordKeyHeader = styled.h3``; let RecordKeyHeader = styled.h3``;
@ -54,25 +65,31 @@ export interface SquiggleItemProps {
expression: squiggleExpression; expression: squiggleExpression;
width: number; width: number;
height: number; height: number;
/** Whether to show type information */
showTypes?: boolean;
} }
const SquiggleItem: React.FC<SquiggleItemProps> = ({ const SquiggleItem: React.FC<SquiggleItemProps> = ({
expression, expression,
width, width,
height, height,
showTypes = false,
}: SquiggleItemProps) => { }: SquiggleItemProps) => {
switch (expression.tag) { switch (expression.tag) {
case "number": case "number":
return ( return (
<VariableBox heading="Number"> <VariableBox heading="Number" showTypes={showTypes}>
<NumberShower precision={3} number={expression.value} /> <NumberShower precision={3} number={expression.value} />
</VariableBox> </VariableBox>
); );
case "distribution": { case "distribution": {
let distType = expression.value.type(); let distType = expression.value.type();
return ( return (
<VariableBox heading={`Distribution (${distType})`}> <VariableBox
{distType === "Symbolic" ? ( heading={`Distribution (${distType})`}
showTypes={showTypes}
>
{distType === "Symbolic" && showTypes ? (
<> <>
<div>{expression.value.toString()}</div> <div>{expression.value.toString()}</div>
</> </>
@ -89,21 +106,32 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
} }
case "string": case "string":
return ( return (
<VariableBox heading="String">{`"${expression.value}"`}</VariableBox> <VariableBox
heading="String"
showTypes={showTypes}
>{`"${expression.value}"`}</VariableBox>
); );
case "boolean": case "boolean":
return ( return (
<VariableBox heading="Boolean"> <VariableBox heading="Boolean" showTypes={showTypes}>
{expression.value.toString()} {expression.value.toString()}
</VariableBox> </VariableBox>
); );
case "symbol": case "symbol":
return <VariableBox heading="Symbol">{expression.value}</VariableBox>; return (
<VariableBox heading="Symbol" showTypes={showTypes}>
{expression.value}
</VariableBox>
);
case "call": case "call":
return <VariableBox heading="Call">{expression.value}</VariableBox>; return (
<VariableBox heading="Call" showTypes={showTypes}>
{expression.value}
</VariableBox>
);
case "array": case "array":
return ( return (
<VariableBox heading="Array"> <VariableBox heading="Array" showTypes={showTypes}>
{expression.value.map((r) => ( {expression.value.map((r) => (
<SquiggleItem expression={r} width={width - 20} height={50} /> <SquiggleItem expression={r} width={width - 20} height={50} />
))} ))}
@ -111,7 +139,7 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
); );
case "record": case "record":
return ( return (
<VariableBox heading="Record"> <VariableBox heading="Record" showTypes={showTypes}>
{Object.entries(expression.value).map(([key, r]) => ( {Object.entries(expression.value).map(([key, r]) => (
<> <>
<RecordKeyHeader>{key}</RecordKeyHeader> <RecordKeyHeader>{key}</RecordKeyHeader>
@ -120,12 +148,6 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
))} ))}
</VariableBox> </VariableBox>
); );
default:
return (
<ErrorBox heading="No Viewer">
{"We don't currently have a working viewer for record types."}
</ErrorBox>
);
} }
}; };
@ -153,6 +175,8 @@ export interface SquiggleChartProps {
bindings?: bindings; bindings?: bindings;
/** JS imported parameters */ /** JS imported parameters */
jsImports?: jsImports; jsImports?: jsImports;
/** Whether to show type information about returns, default false */
showTypes?: boolean;
} }
const ChartWrapper = styled.div` const ChartWrapper = styled.div`
@ -170,6 +194,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
bindings = defaultBindings, bindings = defaultBindings,
jsImports = defaultImports, jsImports = defaultImports,
width = NaN, width = NaN,
showTypes = false,
}: SquiggleChartProps) => { }: SquiggleChartProps) => {
let samplingInputs: samplingParams = { let samplingInputs: samplingParams = {
sampleCount: sampleCount, sampleCount: sampleCount,
@ -186,7 +211,12 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
let expression = expressionResult.value; let expression = expressionResult.value;
onChange(expression); onChange(expression);
internal = ( internal = (
<SquiggleItem expression={expression} width={width} height={height} /> <SquiggleItem
expression={expression}
width={width}
height={height}
showTypes={showTypes}
/>
); );
} else { } else {
internal = ( internal = (

View File

@ -40,6 +40,8 @@ export interface SquiggleEditorProps {
bindings?: bindings; bindings?: bindings;
/** JS Imports */ /** JS Imports */
jsImports?: jsImports; jsImports?: jsImports;
/** Whether to show detail about types of the returns, default false */
showTypes?: boolean;
} }
const Input = styled.div` const Input = styled.div`
@ -61,6 +63,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
onChange, onChange,
bindings = defaultBindings, bindings = defaultBindings,
jsImports = defaultImports, jsImports = defaultImports,
showTypes = false,
}: SquiggleEditorProps) => { }: SquiggleEditorProps) => {
let [expression, setExpression] = React.useState(initialSquiggleString); let [expression, setExpression] = React.useState(initialSquiggleString);
return ( return (
@ -87,6 +90,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
onChange={onChange} onChange={onChange}
bindings={bindings} bindings={bindings}
jsImports={jsImports} jsImports={jsImports}
showTypes={showTypes}
/> />
</div> </div>
); );