import { useState, useEffect } from 'react' import { create } from 'react-modal-promise' import { toolTips, useStore } from '../../useStore' import { Value } from '../../lang/abstractSyntaxTree' import { getNodePathFromSourceRange, getNodeFromPath, findAllPreviousVariables, } from '../../lang/queryAst' import { TransformInfo, getTransformInfos, transformAstSketchLines, } from '../../lang/std/sketchcombos' import { SetAngleLengthModal } from '../SetAngleModal' import { createIdentifier, createVariableDeclaration, } from '../../lang/modifyAst' const getModalInfo = create(SetAngleLengthModal as any) export const SetAngleLength = ({ angleOrLength, }: { angleOrLength: 'setAngle' | 'setLength' }) => { const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore( (s) => ({ guiMode: s.guiMode, ast: s.ast, updateAst: s.updateAst, selectionRanges: s.selectionRanges, programMemory: s.programMemory, }) ) const [enableHorz, setEnableHorz] = useState(false) const [transformInfos, setTransformInfos] = useState() useEffect(() => { if (!ast) return const paths = selectionRanges.map((selectionRange) => getNodePathFromSourceRange(ast, selectionRange) ) const nodes = paths.map( (pathToNode) => getNodeFromPath(ast, pathToNode).node ) const isAllTooltips = nodes.every( (node) => node?.type === 'CallExpression' && toolTips.includes(node.callee.name as any) ) const theTransforms = getTransformInfos(selectionRanges, ast, angleOrLength) setTransformInfos(theTransforms) const _enableHorz = isAllTooltips && theTransforms.every(Boolean) setEnableHorz(_enableHorz) }, [guiMode, selectionRanges]) if (guiMode.mode !== 'sketch') return null return ( ) }