From f4ea9c701791d0be55a8b38b608ef6257c5d5fdf Mon Sep 17 00:00:00 2001 From: raul melo Date: Fri, 1 Dec 2023 23:52:53 -0300 Subject: [PATCH] hotfix: bug loop with persist data --- src/Tool.tsx | 12 +++++++----- src/components/Dropdown.tsx | 22 ++++++++++++++++++---- src/components/styles.ts | 4 ++++ src/withGlobals.ts | 16 ++++++++++++++-- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/Tool.tsx b/src/Tool.tsx index 65f7720..762b1e4 100644 --- a/src/Tool.tsx +++ b/src/Tool.tsx @@ -5,17 +5,19 @@ import { PARAM_KEY, TOOL_ID } from "./constants"; import { DropdownTool } from "./components/Dropdown"; import { ThemeTokensType, ThemeType } from "./utils/types"; import { DisplayToolState } from "./utils/actions"; +import { GetDataStorage } from "./utils/persist"; export const Tool = memo(function MyAddonSelector() { - const [{ themeVariableCss }] = useGlobals(); + const [ global, updateGlobals] = useGlobals(); const tokensCss = useParameter(PARAM_KEY) + useEffect( () => { - if(tokensCss && themeVariableCss) { - const themeSelected = tokensCss?.themes.filter((_theme: ThemeType) => _theme.name === themeVariableCss?.name)[0]; - DisplayToolState('html', {isInDocs: false, themeVariableCss: themeVariableCss.name, themeSelected: themeSelected} ) + if(tokensCss && global.themeVariableCss) { + const themeSelected = tokensCss?.themes.filter((_theme: ThemeType) => _theme.name === global.themeVariableCss?.name)[0]; + DisplayToolState('html', {isInDocs: false, themeVariableCss: global.themeVariableCss.name, themeSelected: themeSelected} ) } - }, [themeVariableCss]) + }, [global.themeVariableCss]) if (!tokensCss && !tokensCss?.themes?.length) { return null diff --git a/src/components/Dropdown.tsx b/src/components/Dropdown.tsx index 6098672..822a9bc 100644 --- a/src/components/Dropdown.tsx +++ b/src/components/Dropdown.tsx @@ -6,6 +6,7 @@ import { MountedOptions, ValidatorArrayTheme } from "../utils"; import { OptionsThemeType, ThemeType } from "../utils/types"; import { ButtonItem, ItemSelected, ViewToolTip } from "./styles"; import { useGlobals } from "@storybook/manager-api"; +import { GetDataStorage } from "src/utils/persist"; interface propsDropDown { list: ThemeType[] @@ -19,13 +20,21 @@ export const DropdownTool = memo( function myDropdownMemo(props: propsDropDown) const [options, setOptions] = React.useState([]); React.useEffect(() => { + const dataLocal: any = GetDataStorage(); + if(dataLocal?.selected) { + updateGlobals({ + ...global, + themeVariableCss: dataLocal.selected + }) + } setListOptionsItem() }, []) + const LabelState = React.useCallback(() => { return <> {!!themeVariableCss && !!themeVariableCss?.name ? - + {!!themeVariableCss?.miniLogo && } @@ -74,10 +83,15 @@ export const DropdownTool = memo( function myDropdownMemo(props: propsDropDown) return ( toggleMyTool(item)} + > - {item.type === 'icon' && } - {item.type === 'image' && } + {item.type === 'icon' && } + {(item.type === 'image' && !!item.miniLogo) && } {item.name} ) @@ -88,7 +102,7 @@ export const DropdownTool = memo( function myDropdownMemo(props: propsDropDown) return ( ComponentItem() }> - {LabelState()} + ); }); \ No newline at end of file diff --git a/src/components/styles.ts b/src/components/styles.ts index bdaeb9a..de6ef43 100644 --- a/src/components/styles.ts +++ b/src/components/styles.ts @@ -34,6 +34,10 @@ export const ButtonItem = styled(Button)({ width: 16, height: 16, objectFit: 'contain', + }, + '&.selected': { + color: convert(themes.normal).color.secondary, + fontWeight: 'bold' } }); diff --git a/src/withGlobals.ts b/src/withGlobals.ts index 80c4e81..95e895a 100644 --- a/src/withGlobals.ts +++ b/src/withGlobals.ts @@ -8,6 +8,7 @@ import { GeneratorId } from "./utils"; import { DisplayToolState } from "./utils/actions"; import { GetDataStorage, SetDataStorage } from "./utils/persist"; import { ThemeType } from "./utils/types"; +import React from "react"; export const withGlobals = ( StoryFn: StoryFunction, @@ -19,15 +20,26 @@ export const withGlobals = ( const persist = context.parameters.designTokensCss?.persistData || false; const themes = context.parameters.designTokensCss?.themes || []; + console.log('context', context) + console.log('themeVariableCss', themeVariableCss) + console.log('themes', themes) + + useEffect(() => { const dataLocal: any = GetDataStorage(); if(dataLocal && dataLocal.themes && dataLocal.selected) { - updateGlobals({themeVariableCss: dataLocal.selected}); const selectorId = isInDocs ? `#anchor--${context.id} .docs-story` : `#root`; DisplayToolState(selectorId, { isInDocs, themeVariableCss: dataLocal.selected.name, themeSelected: dataLocal.selected }) } }, []) + // function setTheme(selected: any) { + // if(themeVariableCss) { + // console.log('selected', selected) + // updateGlobals({themeVariableCss: selected}); + // } + // } + useEffect(() => { let themeSelected = null if(!!themeVariableCss && !!themes) { @@ -38,7 +50,7 @@ export const withGlobals = ( } const selectorId = isInDocs ? `#anchor--${context.id} .docs-story` : `#root`; DisplayToolState(selectorId, { isInDocs, themeVariableCss, themeSelected}) - }, [themeVariableCss]); + }, [themeVariableCss]); return StoryFn(); }; \ No newline at end of file