Skip to content

Commit

Permalink
Merge pull request #2525 from quantified-uncertainty/quick-calculator…
Browse files Browse the repository at this point in the history
…-fixes

Quick calculator fixes
  • Loading branch information
OAGr authored Nov 16, 2023
2 parents 3129979 + f4166b2 commit ae5df78
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 194 deletions.
6 changes: 6 additions & 0 deletions .changeset/tidy-scissors-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@quri/squiggle-lang": patch
"@quri/squiggle-components": patch
---

Calculator result functions no longer re-run on input changes. Calculator inputs can be empty, if the function doesn't take any arguments
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FC, useEffect, useMemo, useState, useCallback } from "react";
import { FC, useCallback, useEffect, useMemo, useState } from "react";

import { Env, SqValue } from "@quri/squiggle-lang";

import { Button } from "@quri/ui";
import { PlaygroundSettings } from "../PlaygroundSettings.js";
import { ValueResultViewer } from "./ValueResultViewer.js";
import {
Expand All @@ -10,7 +11,6 @@ import {
SqValueResult,
} from "./types.js";
import { useSavedCalculatorState } from "./useSavedCalculatorState.js";
import { Button } from "@quri/ui";

type Props = {
valueWithContext: SqCalculatorValueWithContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { FC } from "react";
import { memo } from "react";

import { valueHasContext } from "../../lib/utility.js";
import { PlaygroundSettings } from "../PlaygroundSettings.js";
import { getSqValueWidget } from "../SquiggleViewer/getSqValueWidget.js";
import { SqValueResult } from "./types.js";

// Unlike ValueViewer/ValueWithContextViewer, this just renders the raw widget; TODO - better name?
export const ValueResultViewer: FC<{
export const ValueResultViewer = memo(function ValueResultViewer({
result,
settings,
}: {
result: SqValueResult;
settings: PlaygroundSettings;
}> = ({ result, settings }) => {
}) {
if (result.ok) {
const value = result.value;
if (valueHasContext(value)) {
Expand All @@ -24,4 +27,4 @@ export const ValueResultViewer: FC<{
</div>
);
}
};
});
33 changes: 20 additions & 13 deletions packages/components/src/components/Calculator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,26 @@ export const Calculator: FC<Props> = ({
const { calculator, form, inputResults, processAllFieldCodes } =
useCalculator(valueWithContext, _environment);

const inputResultSettings: PlaygroundSettings = {
...settings,
distributionChartSettings: {
...settings.distributionChartSettings,
showSummary: false,
},
chartHeight: 30,
};

const calculatorResultSettings: PlaygroundSettings = {
...settings,
chartHeight: 200,
};
const inputResultSettings: PlaygroundSettings = useMemo(
() => ({
...settings,
distributionChartSettings: {
...settings.distributionChartSettings,
showSummary: false,
},
chartHeight: 30,
}),
[settings]
);

//This memoization is useful to make sure that CalculatorResult ResultViewer doesn't get updated too frequently.
const calculatorResultSettings: PlaygroundSettings = useMemo(
() => ({
...settings,
chartHeight: 200,
}),
[settings]
);

const hasTitleOrDescription = !!calculator.title || !!calculator.description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useSetLocalItemState,
useViewerContext,
useResetStateSettings,
useMergedSettings,
} from "./ViewerProvider.js";
import { pathAsString } from "./utils.js";

Expand All @@ -38,11 +39,11 @@ const ItemSettingsModal: React.FC<
resetScroll,
}) => {
const setLocalItemState = useSetLocalItemState();
const { getLocalItemState, getMergedSettings } = useViewerContext();
const { getLocalItemState } = useViewerContext();

const { path } = value.context;

const mergedSettings = getMergedSettings({ path });
const mergedSettings = useMergedSettings(path);

const form = useForm({
resolver: zodResolver(viewSettingsSchema),
Expand Down
Loading

3 comments on commit ae5df78

@vercel
Copy link

@vercel vercel bot commented on ae5df78 Nov 16, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on ae5df78 Nov 16, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on ae5df78 Nov 16, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.