import { useMemo } from 'react' import toast from 'react-hot-toast' import ReactJson from 'react-json-view' import type { ExtrudeSurface } from '@rust/kcl-lib/bindings/ExtrudeSurface' import type { Path } from '@rust/kcl-lib/bindings/Path' import { ActionButton } from '@src/components/ActionButton' import Tooltip from '@src/components/Tooltip' import { useModelingContext } from '@src/hooks/useModelingContext' import { useResolvedTheme } from '@src/hooks/useResolvedTheme' import { useKclContext } from '@src/lang/KclProvider' import type { VariableMap } from '@src/lang/wasm' import { sketchFromKclValueOptional } from '@src/lang/wasm' import { Reason, trap } from '@src/lib/trap' export const MemoryPaneMenu = () => { const { variables } = useKclContext() function copyProgramMemoryToClipboard() { if (globalThis && 'navigator' in globalThis) { navigator.clipboard .writeText(JSON.stringify(variables)) .then(() => toast.success('Program memory copied to clipboard')) .catch((e) => trap(new Error('Failed to copy program memory to clipboard')) ) } } return ( <> Copy to clipboard ) } export const MemoryPane = () => { const theme = useResolvedTheme() const { variables } = useKclContext() const { state } = useModelingContext() const ProcessedMemory = useMemo(() => processMemory(variables), [variables]) return (
{state.matches('Sketch') && (
)}
) } export const processMemory = (variables: VariableMap) => { const processedMemory: any = {} for (const [key, val] of Object.entries(variables)) { if (val === undefined) continue if ( val.type === 'Sketch' || // @ts-ignore val.type !== 'Function' ) { const sk = sketchFromKclValueOptional(val, key) if (val.type === 'Solid') { processedMemory[key] = val.value.value.map( ({ ...rest }: ExtrudeSurface) => { return rest } ) } else if (!(sk instanceof Reason)) { processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => { return rest }) } else { processedMemory[key] = val.value } //@ts-ignore } else if (val.type === 'Function') { processedMemory[key] = `__function__` } } return processedMemory }