greatly simplify state + cleanup code

This commit is contained in:
Joeri Exelmans 2025-05-14 08:09:35 +02:00
parent 2d0deca127
commit 5964510036
11 changed files with 268 additions and 321 deletions

View file

@ -1,17 +1,18 @@
import { useContext, useEffect, useRef, useState } from "react";
import { getSymbol, getType, symbolFunction } from "dope2";
import { useContext, useEffect, useReducer, useRef, useState } from "react";
import { CallBlock, type CallBlockState } from "./CallBlock";
import { InputBlock, type InputBlockState } from "./InputBlock";
import { InputBlock, type InputBlockState, type SuggestionType } from "./InputBlock";
import { Type } from "./Type";
import { DeepError, type Dynamic, type State2Props } from "./types";
import "./Editor.css";
import { LetInBlock, type LetInBlockState } from "./LetInBlock";
import { focusNextElement, focusPrevElement } from "./util/dom_trickery";
import type { LambdaBlockState } from "./LambdaBlock";
import { initialEditorState } from "./configurations";
import { DeepError, evalEditorBlock } from "./eval";
import { CommandContext } from "./CommandContext";
import "./Editor.css";
import { EnvContext } from "./EnvContext";
import type { LambdaBlockState } from "./LambdaBlock";
import { LetInBlock, type LetInBlockState } from "./LetInBlock";
import { initialEditorState } from "./configurations";
import { focusNextElement, focusPrevElement } from "./util/dom_trickery";
export type EditorState =
InputBlockState
@ -19,8 +20,15 @@ export type EditorState =
| LetInBlockState
| LambdaBlockState;
export type SetStateFn<InType = EditorState, OutType = InType> = (state: InType) => OutType;
export interface State2Props<InType, OutType = InType> {
state: InType;
setState: (callback: SetStateFn<InType, OutType>) => void;
}
interface EditorProps extends State2Props<EditorState> {
filter: (suggestion: [string, Dynamic]) => boolean;
filter: (suggestion: SuggestionType) => boolean;
onCancel: () => void;
}
@ -52,6 +60,7 @@ function removeFocus(state: EditorState): EditorState {
}
export function Editor({state, setState, onCancel, filter}: EditorProps) {
const env = useContext(EnvContext);
const [needCommand, setNeedCommand] = useState(false);
const commandInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
@ -59,21 +68,6 @@ export function Editor({state, setState, onCancel, filter}: EditorProps) {
commandInputRef.current?.focus();
}
}, [needCommand]);
// const onMyResolve = (editorState: EditorState) => {
// setState(editorState);
// onResolve(editorState);
// return;
// if (editorState.resolved) {
// setNeedCommand(true);
// }
// else {
// // unresolved
// setNeedCommand(false);
// onResolve(editorState); // pass up the fact that we're unresolved
// }
// }
const globalContext = useContext(CommandContext);
const onCommand = (e: React.KeyboardEvent) => {
@ -167,12 +161,13 @@ export function Editor({state, setState, onCancel, filter}: EditorProps) {
return <></>;
}
}
const resolved = evalEditorBlock(state, env);
return <>
{renderBlock()}
{
(state.resolved && !(state.resolved instanceof DeepError))
(resolved && !(resolved instanceof DeepError))
? <div className="typeSignature">
:: <Type type={getType(state.resolved)} />
:: <Type type={getType(resolved)} />
</div>
: <></>
}