store latest params in the store

This commit is contained in:
Kurt Hutten
2021-08-07 17:00:29 +10:00
parent 2d7df96ad9
commit 02160e1e8e
6 changed files with 43 additions and 35 deletions

View File

@@ -1,35 +1,33 @@
import { useRender } from 'src/components/IdeWrapper/useRender' import { useRender } from 'src/components/IdeWrapper/useRender'
import { useIdeContext } from 'src/helpers/hooks/useIdeContext' import { useIdeContext } from 'src/helpers/hooks/useIdeContext'
import { genParams, getParams } from 'src/helpers/cadPackages/jsCad/jscadParams' import { genParams } from 'src/helpers/cadPackages/jsCad/jscadParams'
const Customizer = () => { const Customizer = () => {
const [open, setOpen] = React.useState(true) const [open, setOpen] = React.useState(true)
const [checked, setChecked] = React.useState(false) const [checked, setChecked] = React.useState(false)
const ref = React.useRef() const ref = React.useRef()
const jsCadCustomizerElement = ref.current const jsCadCustomizerElement = ref.current
const { state } = useIdeContext() const { state, thunkDispatch } = useIdeContext()
const customizerParams = state?.objectData?.customizerParams const customizerParams = state?.customizerParams
const lastParameters = state?.objectData?.lastParameters const currentParameters = state?.currentParameters
const handleRender = useRender() const handleRender = useRender()
const handleRender2 = () => handleRender(getParams(ref.current))
React.useEffect(() => { React.useEffect(() => {
if (jsCadCustomizerElement && customizerParams) { if (jsCadCustomizerElement && customizerParams) {
genParams( genParams(
customizerParams, customizerParams,
jsCadCustomizerElement, jsCadCustomizerElement,
lastParameters || {}, currentParameters || {},
(values, source) => { (values, source) => {
if (source === 'group' || !checked) { thunkDispatch({ type: 'setCurrentCustomizerParams', payload: values })
// save to local storage but do not render if (checked) {
return handleRender()
} }
handleRender(values)
}, },
[] []
) )
} }
}, [jsCadCustomizerElement, customizerParams, lastParameters, checked]) }, [jsCadCustomizerElement, customizerParams, currentParameters, checked])
return ( return (
<div <div
className={`absolute inset-x-0 bottom-0 bg-ch-gray-600 bg-opacity-60 text-ch-gray-300 text-lg font-fira-sans ${ className={`absolute inset-x-0 bottom-0 bg-ch-gray-600 bg-opacity-60 text-ch-gray-300 text-lg font-fira-sans ${
@@ -48,15 +46,15 @@ const Customizer = () => {
className="mr-6" className="mr-6"
type="checkbox" type="checkbox"
checked={checked} checked={checked}
onChange={({ target }) => { onChange={() => {
const newValue = !checked const newValue = !checked
if (newValue) handleRender2() if (newValue) handleRender()
setChecked(newValue) setChecked(newValue)
}} }}
/> />
<button <button
className="px-4 py-1 rounded bg-ch-gray-300 text-ch-gray-800" className="px-4 py-1 rounded bg-ch-gray-300 text-ch-gray-800"
onClick={handleRender2} onClick={handleRender}
> >
Update Update
</button> </button>

View File

@@ -1,10 +1,9 @@
import { makeCodeStoreKey, requestRender } from 'src/helpers/hooks/useIdeState' import { makeCodeStoreKey, requestRender } from 'src/helpers/hooks/useIdeState'
import { useIdeContext } from 'src/helpers/hooks/useIdeContext' import { useIdeContext } from 'src/helpers/hooks/useIdeContext'
import type { RawCustomizerParams } from 'src/helpers/cadPackages/common'
export const useRender = () => { export const useRender = () => {
const { state, thunkDispatch } = useIdeContext() const { state, thunkDispatch } = useIdeContext()
return (parameters?: RawCustomizerParams) => { return () => {
thunkDispatch((dispatch, getState) => { thunkDispatch((dispatch, getState) => {
const state = getState() const state = getState()
dispatch({ type: 'setLoading' }) dispatch({ type: 'setLoading' })
@@ -14,7 +13,7 @@ export const useRender = () => {
code: state.code, code: state.code,
viewerSize: state.viewerSize, viewerSize: state.viewerSize,
camera: state.camera, camera: state.camera,
parameters, parameters: state.currentParameters,
}) })
}) })
localStorage.setItem(makeCodeStoreKey(state.ideType), state.code) localStorage.setItem(makeCodeStoreKey(state.ideType), state.code)

View File

@@ -31,8 +31,8 @@ export interface HealthyResponse {
data: any data: any
type: 'stl' | 'png' | 'geometry' type: 'stl' | 'png' | 'geometry'
} }
customizerParams?: any customizerParams?: any[]
lastParameters?: any currentParameters?: RawCustomizerParams
} }
export interface RawCustomizerParams { export interface RawCustomizerParams {
@@ -45,14 +45,14 @@ export function createHealthyResponse({
consoleMessage, consoleMessage,
type, type,
customizerParams, customizerParams,
lastParameters, currentParameters,
}: { }: {
date: Date date: Date
data: any data: any
consoleMessage: string consoleMessage: string
type: HealthyResponse['objectData']['type'] type: HealthyResponse['objectData']['type']
customizerParams?: any customizerParams?: any
lastParameters?: any currentParameters?: any
}): HealthyResponse { }): HealthyResponse {
return { return {
status: 'healthy', status: 'healthy',
@@ -66,7 +66,7 @@ export function createHealthyResponse({
time: date, time: date,
}, },
customizerParams, customizerParams,
lastParameters, currentParameters,
} }
} }

View File

@@ -70,7 +70,7 @@ function CSG2Object3D(obj) {
} }
let scriptWorker let scriptWorker
let lastParameters = {} let currentParameters = {}
const scriptUrl = '/demo-worker.js' const scriptUrl = '/demo-worker.js'
let resolveReference = null let resolveReference = null
let response = null let response = null
@@ -117,7 +117,7 @@ export const render: DefaultKernelExport['render'] = async ({
consoleMessage: data.scriptStats, consoleMessage: data.scriptStats,
date: new Date(), date: new Date(),
customizerParams: parameterDefinitions, customizerParams: parameterDefinitions,
lastParameters, currentParameters,
}) })
} }
callResolve() callResolve()
@@ -148,7 +148,7 @@ export const render: DefaultKernelExport['render'] = async ({
// we need this to keep the form filled with same data when new parameter definitions arrive // we need this to keep the form filled with same data when new parameter definitions arrive
// each render of the script could provide new paramaters. In case some of them are still rpesent // each render of the script could provide new paramaters. In case some of them are still rpesent
// it is expected for them to stay the same and not just reset // it is expected for them to stay the same and not just reset
lastParameters = parameters || {} currentParameters = parameters || {}
const waitResult = new Promise((resolve) => { const waitResult = new Promise((resolve) => {
resolveReference = resolve resolveReference = resolve

View File

@@ -28,7 +28,7 @@ export function genParams(
defs, defs,
target, target,
storedParams = {}, storedParams = {},
callback = undefined, callback: (values: RawCustomizerParams, source: any) => void = undefined,
buttons = ['reset', 'save', 'load', 'edit', 'link'] buttons = ['reset', 'save', 'load', 'edit', 'link']
) { ) {
const funcs = { const funcs = {
@@ -140,7 +140,7 @@ export function genParams(
if (missingKeys.length) console.log('missing param impl', missingKeys) if (missingKeys.length) console.log('missing param impl', missingKeys)
function _callback(source = 'change') { function _callback(source = 'change') {
if (callback) callback(getParams(target), source) if (callback && source !== 'group') callback(getParams(target), source)
} }
html += '<div class="jscad-param-buttons"><div>' html += '<div class="jscad-param-buttons"><div>'
@@ -179,7 +179,7 @@ export function genParams(
}) })
} }
export function getParams(target: HTMLElement): RawCustomizerParams { function getParams(target: HTMLElement): RawCustomizerParams {
const params = {} const params = {}
if (!target) return params if (!target) return params

View File

@@ -1,6 +1,7 @@
import { useReducer } from 'react' import { useReducer } from 'react'
import { cadPackages } from 'src/helpers/cadPackages' import { cadPackages } from 'src/helpers/cadPackages'
import type { RootState } from '@react-three/fiber' import type { RootState } from '@react-three/fiber'
import type { RawCustomizerParams } from 'src/helpers/cadPackages/common'
function withThunk(dispatch, getState) { function withThunk(dispatch, getState) {
return (actionOrThunk) => return (actionOrThunk) =>
@@ -114,9 +115,9 @@ export interface State {
type: 'INIT' | 'stl' | 'png' | 'geometry' type: 'INIT' | 'stl' | 'png' | 'geometry'
data: any data: any
quality: 'low' | 'high' quality: 'low' | 'high'
customizerParams?: any
lastParameters?: any
} }
customizerParams?: any[]
currentParameters?: RawCustomizerParams
layout: any layout: any
camera: { camera: {
dist?: number dist?: number
@@ -180,10 +181,9 @@ export const useIdeState = (): [State, (actionOrThunk: any) => any] => {
...state.objectData, ...state.objectData,
type: payload.objectData?.type, type: payload.objectData?.type,
data: payload.objectData?.data, data: payload.objectData?.data,
customizerParams:
payload.customizerParams || state.objectData.customizerParams,
lastParameters: payload.lastParameters,
}, },
customizerParams: payload.customizerParams || state.customizerParams,
currentParameters: payload.currentParameters,
consoleMessages: payload.message consoleMessages: payload.message
? [...state.consoleMessages, payload.message] ? [...state.consoleMessages, payload.message]
: payload.message, : payload.message,
@@ -197,6 +197,11 @@ export const useIdeState = (): [State, (actionOrThunk: any) => any] => {
: payload.message, : payload.message,
isLoading: false, isLoading: false,
} }
case 'setCurrentCustomizerParams':
return {
...state,
currentParameters: payload,
}
case 'setLayout': case 'setLayout':
return { return {
...state, ...state,
@@ -282,7 +287,13 @@ export const requestRender = ({
}, },
}) })
.then( .then(
({ objectData, message, status, customizerParams, lastParameters }) => { ({
objectData,
message,
status,
customizerParams,
currentParameters,
}) => {
if (status === 'error') { if (status === 'error') {
dispatch({ dispatch({
type: 'errorRender', type: 'errorRender',
@@ -296,7 +307,7 @@ export const requestRender = ({
message, message,
lastRunCode: code, lastRunCode: code,
customizerParams, customizerParams,
lastParameters, currentParameters,
}, },
}) })
return objectData return objectData