From 597de89826e4ad1af703db4d5c813ca9827c541b Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 21 Jun 2020 20:37:38 -0500 Subject: [PATCH] refactor: proper memo --- src/client/resources/entity/entity.jsx | 2 +- src/client/resources/entity/trait-pane.jsx | 87 +++++++++++++ src/client/resources/entity/trait-tab.jsx | 14 ++ src/client/resources/entity/traits.jsx | 137 +++----------------- src/client/resources/entity/traits.raw.scss | 5 + 5 files changed, 123 insertions(+), 122 deletions(-) create mode 100644 src/client/resources/entity/trait-pane.jsx create mode 100644 src/client/resources/entity/trait-tab.jsx diff --git a/src/client/resources/entity/entity.jsx b/src/client/resources/entity/entity.jsx index f49584e..7effb0e 100644 --- a/src/client/resources/entity/entity.jsx +++ b/src/client/resources/entity/entity.jsx @@ -19,7 +19,7 @@ const EntityComponent = (props) => { if (!json) { dispatch(fetchEntity(uri)); } - }, [json, uri]); + }, [dispatch, json, uri]); if (!json) { return null; } diff --git a/src/client/resources/entity/trait-pane.jsx b/src/client/resources/entity/trait-pane.jsx new file mode 100644 index 0000000..50a3415 --- /dev/null +++ b/src/client/resources/entity/trait-pane.jsx @@ -0,0 +1,87 @@ +import {mapObject} from '@avocado/core'; +import {lookupTrait} from '@avocado/entity'; +import {all as allTraitComponents} from '@avocado/entity/trait/trait-components.scwp'; +import React, {useMemo} from 'react'; +import {useDispatch} from 'react-redux'; + +import Value from '~/client/value'; + +import {setTraitProperty} from './state'; + +let TraitComponents; +const ensureTraitComponents = () => { + if (!TraitComponents) { + TraitComponents = Object.values(allTraitComponents()).reduce((r, M) => { + const {default: TraitComponent} = M; + return {...r, [TraitComponent.type]: TraitComponent}; + }, {}); + } +}; + +const makePane = (context, dispatch, target, type) => (trait) => { + ensureTraitComponents(); + const {params: paramsRaw, state: stateRaw} = trait; + const {[type]: TraitComponent} = TraitComponents; + const Trait = lookupTrait(type); + const params = Trait ? {...Trait.defaultParams(), ...paramsRaw} : paramsRaw; + const state = Trait ? {...Trait.defaultState(), ...stateRaw} : stateRaw; + if (TraitComponent) { + return ; + } + const paramsDescription = Trait?.describeParams() || {}; + const stateDescription = Trait?.describeState() || {}; + const renderComponents = (traitHalf, description, values) => ( + // eslint-disable-next-line no-shadow + Object.values(mapObject(description, (description, key) => { + const {label, options} = description; + return ( +