Remove re-render on customizer open/close in project profile

This commit is contained in:
Kurt Hutten
2021-09-28 06:03:20 +10:00
parent fc88a6e87a
commit d01e0fc342
4 changed files with 17 additions and 8 deletions

View File

@@ -21,9 +21,11 @@ const Customizer = () => {
const currentParameters = state?.currentParameters || {} const currentParameters = state?.currentParameters || {}
const handleRender = useRender() const handleRender = useRender()
const toggleOpen = () => { const toggleOpen = () => {
const newOpenState = !isOpen thunkDispatch({ type: 'setCustomizerOpenState', payload: !isOpen })
thunkDispatch({ type: 'setCustomizerOpenState', payload: newOpenState }) if (state.viewerContext === 'ide') {
setTimeout(() => handleRender()) // don't re-render on open/close in the project profile
setTimeout(() => handleRender())
}
} }
const handleReset = () => { const handleReset = () => {
thunkDispatch({ type: 'resetCustomizer' }) thunkDispatch({ type: 'resetCustomizer' })

View File

@@ -5,6 +5,7 @@ import { useIdeContext } from 'src/helpers/hooks/useIdeContext'
import { useRender } from 'src/components/IdeWrapper/useRender' import { useRender } from 'src/components/IdeWrapper/useRender'
import { encode, decode } from 'src/helpers/compress' import { encode, decode } from 'src/helpers/compress'
import { isBrowser } from '@redwoodjs/prerender/browserUtils' import { isBrowser } from '@redwoodjs/prerender/browserUtils'
import type { State } from 'src/helpers/hooks/useIdeState'
const scriptKey = 'encoded_script' const scriptKey = 'encoded_script'
const scriptKeyV2 = 'encoded_script_v2' const scriptKeyV2 = 'encoded_script_v2'
@@ -32,13 +33,17 @@ export function makeExternalUrl(resourceUrl: string): string {
}#${fetchText}=${prepareDecodedUrl(resourceUrl)}` }#${fetchText}=${prepareDecodedUrl(resourceUrl)}`
} }
export function useIdeInit(cadPackage: string, code = '') { export function useIdeInit(
cadPackage: State['ideType'],
code = '',
viewerContext: State['viewerContext'] = 'ide'
) {
const { thunkDispatch } = useIdeContext() const { thunkDispatch } = useIdeContext()
const handleRender = useRender() const handleRender = useRender()
useEffect(() => { useEffect(() => {
thunkDispatch({ thunkDispatch({
type: 'initIde', type: 'initIde',
payload: { cadPackage, code }, payload: { cadPackage, code, viewerContext },
}) })
if (code) { if (code) {
return return

View File

@@ -1,4 +1,4 @@
import { useState, useEffect, useRef, lazy, Suspense } from 'react' import { useState, useEffect, useRef } from 'react'
import { useAuth } from '@redwoodjs/auth' import { useAuth } from '@redwoodjs/auth'
import { Link, navigate, routes } from '@redwoodjs/router' import { Link, navigate, routes } from '@redwoodjs/router'
import Editor from 'rich-markdown-editor' import Editor from 'rich-markdown-editor'
@@ -15,7 +15,6 @@ import CadPackage from 'src/components/CadPackage/CadPackage'
import Gravatar from 'src/components/Gravatar/Gravatar' import Gravatar from 'src/components/Gravatar/Gravatar'
import { useIdeInit } from 'src/components/EncodedUrl/helpers' import { useIdeInit } from 'src/components/EncodedUrl/helpers'
import ProfileViewer from '../ProfileViewer/ProfileViewer' import ProfileViewer from '../ProfileViewer/ProfileViewer'
import Svg from 'src/components/Svg/Svg'
import OpenscadStaticImageMessage from 'src/components/OpenscadStaticImageMessage/OpenscadStaticImageMessage' import OpenscadStaticImageMessage from 'src/components/OpenscadStaticImageMessage/OpenscadStaticImageMessage'
import KeyValue from 'src/components/KeyValue/KeyValue' import KeyValue from 'src/components/KeyValue/KeyValue'
@@ -51,7 +50,7 @@ const ProjectProfile = ({
}) })
) )
}, [currentUser]) }, [currentUser])
useIdeInit(project?.cadPackage, project?.code) useIdeInit(project?.cadPackage, project?.code, 'viewer')
const [newDescription, setNewDescription] = useState(project?.description) const [newDescription, setNewDescription] = useState(project?.description)
const onDescriptionChange = (description) => setNewDescription(description()) const onDescriptionChange = (description) => setNewDescription(description())
const onEditSaveClick = () => { const onEditSaveClick = () => {

View File

@@ -33,6 +33,7 @@ interface EditorModel {
export interface State { export interface State {
ideType: 'INIT' | CadPackageType ideType: 'INIT' | CadPackageType
viewerContext: 'ide' | 'viewer'
ideGuide?: string ideGuide?: string
consoleMessages: { type: 'message' | 'error'; message: string; time: Date }[] consoleMessages: { type: 'message' | 'error'; message: string; time: Date }[]
code: string code: string
@@ -72,6 +73,7 @@ const initialLayout = {
export const initialState: State = { export const initialState: State = {
ideType: 'INIT', ideType: 'INIT',
viewerContext: 'ide',
consoleMessages: [ consoleMessages: [
{ type: 'message', message: 'Initialising', time: new Date() }, { type: 'message', message: 'Initialising', time: new Date() },
], ],
@@ -105,6 +107,7 @@ const reducer = (state: State, { type, payload }): State => {
'', '',
ideType: payload.cadPackage, ideType: payload.cadPackage,
ideGuide: initGuideMap[payload.cadPackage], ideGuide: initGuideMap[payload.cadPackage],
viewerContext: payload.viewerContext,
} }
case 'updateCode': case 'updateCode':
return { ...state, code: payload } return { ...state, code: payload }