2022-07-19 23:00:29 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
2022-06-20 05:35:48 +00:00
|
|
|
// import { SquiggleChart } from "@quri/squiggle-components";
|
|
|
|
|
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
|
|
|
|
const SquiggleChart = dynamic(
|
|
|
|
() => import("@quri/squiggle-components").then((mod) => mod.SquiggleChart),
|
|
|
|
{
|
|
|
|
loading: () => <p>Loading...</p>,
|
|
|
|
ssr: false,
|
|
|
|
}
|
|
|
|
);
|
2022-07-19 23:00:29 +00:00
|
|
|
|
|
|
|
const SquiggleEditor = dynamic(
|
|
|
|
() => import("@quri/squiggle-components").then((mod) => mod.SquiggleEditor),
|
2022-06-20 05:35:48 +00:00
|
|
|
{
|
2022-07-19 23:00:29 +00:00
|
|
|
loading: () => <p>Loading...</p>,
|
2022-06-20 05:35:48 +00:00
|
|
|
ssr: false,
|
|
|
|
}
|
2022-07-19 23:00:29 +00:00
|
|
|
);
|
|
|
|
// ^ works, but updating the editor content from the outside would be tricky.
|
|
|
|
// and so instead we are hacking our own mini-editor.
|
2022-06-20 05:35:48 +00:00
|
|
|
|
|
|
|
const effectButtonStyle =
|
|
|
|
"bg-transparent m-2 hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded mt-5";
|
|
|
|
|
2022-07-19 23:00:29 +00:00
|
|
|
const countNumberOfLines = string => {
|
|
|
|
return string.split("\n").length
|
|
|
|
}
|
|
|
|
|
2022-06-25 01:38:07 +00:00
|
|
|
export function DynamicSquiggleChart({ element, stopShowing }) {
|
2022-07-19 23:00:29 +00:00
|
|
|
const initialEditorState = !!element ? element.fn : ""
|
|
|
|
const [editorState, setEditorState] = useState(initialEditorState)
|
|
|
|
useEffect(() => {
|
|
|
|
if (!!element && element.fn != "") {
|
|
|
|
setEditorState(element.fn)
|
|
|
|
}
|
|
|
|
}, [element]);
|
2022-06-25 01:38:07 +00:00
|
|
|
if (element == null) {
|
2022-06-20 05:47:11 +00:00
|
|
|
return "";
|
|
|
|
} else {
|
2022-06-25 01:38:07 +00:00
|
|
|
let usefulElement = {
|
|
|
|
name: element.id,
|
|
|
|
squiggleString: element.fn,
|
2022-07-19 21:53:33 +00:00
|
|
|
binding: element.binding || null
|
2022-06-20 05:47:11 +00:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<div className="">
|
2022-07-19 23:00:29 +00:00
|
|
|
<div className="bg-white p-8">
|
|
|
|
<h3 className="text-2xl font-bold mb-4">{usefulElement.name}</h3>
|
|
|
|
<textarea
|
|
|
|
value={editorState}
|
|
|
|
onChange={(event) => setEditorState(event.target.value)}
|
|
|
|
// disabled={true}
|
|
|
|
rows={countNumberOfLines(editorState) + 1}
|
|
|
|
cols={30}
|
|
|
|
className="text-left text-gray-600 bg-white rounded text-normal p-5 border-0 shadow outline-none focus:outline-none focus:ring"
|
|
|
|
/>
|
|
|
|
<SquiggleChart
|
|
|
|
squiggleString={editorState}
|
|
|
|
width={500}
|
|
|
|
height={200}
|
|
|
|
bindings={usefulElement.binding}
|
|
|
|
showSummary={true}
|
|
|
|
showTypes={true}
|
|
|
|
/>
|
2022-06-20 05:35:48 +00:00
|
|
|
|
2022-07-19 23:00:29 +00:00
|
|
|
</div>
|
2022-06-20 05:47:11 +00:00
|
|
|
<button className={effectButtonStyle} onClick={() => stopShowing()}>
|
|
|
|
Hide chart
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
2022-06-20 05:35:48 +00:00
|
|
|
}
|
|
|
|
}
|