dope2-webapp/src/ExprBlock.tsx

74 lines
2.6 KiB
TypeScript

import { useContext } from "react";
import { getType } from "dope2";
import { CallBlock, type CallBlockProps, type CallBlockState } from "./CallBlock";
import { EnvContext } from "./EnvContext";
import { GlobalContext } from "./GlobalContext";
import { InputBlock, type InputBlockProps, type InputBlockState } from "./InputBlock";
import { LambdaBlock, type LambdaBlockProps, type LambdaBlockState } from "./LambdaBlock";
import { LetInBlock, type LetInBlockProps, type LetInBlockState } from "./LetInBlock";
import { Type } from "./Type";
// import { evalExprBlock, type ResolvedType } from "./eval";
import "./ExprBlock.css";
import { Input } from "./Input";
import { getActions } from "./actions";
import { inferType } from "./infer_type";
export type ExprBlockState =
InputBlockState
| CallBlockState
| LetInBlockState
| LambdaBlockState;
export type SetStateFn<InType = ExprBlockState, OutType = InType> = (state: InType) => OutType;
export interface State2Props<InType, OutType = InType> {
state: InType;
setState: (callback: SetStateFn<InType, OutType>) => void;
score: (suggestion: ExprBlockState) => number;
}
interface ExprBlockProps extends State2Props<ExprBlockState> {
onCancel: () => void;
}
export function ExprBlock(props: ExprBlockProps) {
const env = useContext(EnvContext);
const globalContext = useContext(GlobalContext);
const renderBlock = {
input: () => <InputBlock {...props as InputBlockProps} />,
call: () => <CallBlock {...props as CallBlockProps} />,
let: () => <LetInBlock {...props as LetInBlockProps} />,
lambda: () => <LambdaBlock {...props as LambdaBlockProps} />,
};
// const [resolved] = evalExprBlock(props.state, env);
// const typeInfo = inferType(props.state, env);
const actions = getActions(globalContext, props.setState);
const extraHandlers = Object.fromEntries(Object.entries(actions).map(([shortcut, action]) =>
[shortcut, (e) => { e.preventDefault(); action(); }]))
// return <span className={"editor" + ((resolved.kind!=="value") ? " "+resolved.kind : "")}>
return <span className={"editor"}>
{renderBlock[props.state.kind]()}
{/* @ts-ignore */}
{/* <div className={"typeSignature" + (resolved.__debug ? ' gotDebug' : '')}> */}
{/* &nbsp;::&nbsp;<Type type={typeInfo.type} /> */}
{/* @ts-ignore */}
{/* {resolved.__debug && <div className="typeDebug">{resolved.__debug}</div>} */}
{/* </div> */}
<Input
placeholder="<c>"
text=""
suggestion=""
onEnter={() => {}}
onCancel={props.onCancel}
onTextChange={() => {}}
extraHandlers={extraHandlers}
/>
</span>;
}