From 1bbf3b164aaa4658fc5d8088113f20e9b19ab327 Mon Sep 17 00:00:00 2001 From: DrMint Date: Tue, 16 Aug 2022 01:25:01 +0200 Subject: [PATCH] Use localStorage hook --- src/components/AppLayout.tsx | 71 +++++++++--------- src/components/Inputs/Select.tsx | 11 +-- src/components/Library/PreviewCardCTAs.tsx | 4 +- src/contexts/AppLayoutContext.tsx | 83 ++++++++++++++-------- src/hooks/useDarkMode.ts | 18 ++--- src/hooks/useSmartLanguage.ts | 2 +- src/hooks/useStateWithLocalStorage.ts | 31 -------- src/hooks/useTernaryDarkMode.ts | 38 ---------- src/pages/library/index.tsx | 2 +- 9 files changed, 106 insertions(+), 154 deletions(-) delete mode 100644 src/hooks/useStateWithLocalStorage.ts delete mode 100644 src/hooks/useTernaryDarkMode.ts diff --git a/src/components/AppLayout.tsx b/src/components/AppLayout.tsx index cc06165..76ac09e 100644 --- a/src/components/AppLayout.tsx +++ b/src/components/AppLayout.tsx @@ -109,7 +109,7 @@ export const AppLayout = ({ onSwipedLeft: (SwipeEventData) => { if (menuGestures) { if (SwipeEventData.velocity < SENSIBILITY_SWIPE) return; - if (mainPanelOpen === true) { + if (mainPanelOpen) { setMainPanelOpen(false); } else if (isDefined(subPanel) && isDefined(contentPanel)) { setSubPanelOpen(true); @@ -119,7 +119,7 @@ export const AppLayout = ({ onSwipedRight: (SwipeEventData) => { if (menuGestures) { if (SwipeEventData.velocity < SENSIBILITY_SWIPE) return; - if (subPanelOpen === true) { + if (subPanelOpen) { setSubPanelOpen(false); } else { setMainPanelOpen(true); @@ -135,7 +135,7 @@ export const AppLayout = ({ useLayoutEffect(() => { document.getElementsByTagName("html")[0].style.fontSize = `${ - (fontSize ?? 1) * 100 + fontSize * 100 }%`; }, [fontSize]); @@ -159,18 +159,16 @@ export const AppLayout = ({ }, [currencyOptions, currencySelect, setCurrency]); useEffect(() => { - if (preferredLanguages) { - if (preferredLanguages.length === 0) { - if (isDefinedAndNotEmpty(router.locale) && router.locales) { - setPreferredLanguages( - getDefaultPreferredLanguages(router.locale, router.locales) - ); - } - } else if (router.locale !== preferredLanguages[0]) { - router.replace(router.asPath, router.asPath, { - locale: preferredLanguages[0], - }); + if (preferredLanguages.length === 0) { + if (isDefinedAndNotEmpty(router.locale) && router.locales) { + setPreferredLanguages( + getDefaultPreferredLanguages(router.locale, router.locales) + ); } + } else if (router.locale !== preferredLanguages[0]) { + router.replace(router.asPath, router.asPath, { + locale: preferredLanguages[0], + }); } }, [ preferredLanguages, @@ -182,11 +180,11 @@ export const AppLayout = ({ const gridCol = useMemo(() => { if (isDefined(subPanel)) { - if (mainPanelReduced === true) { + if (mainPanelReduced) { return "grid-cols-[6rem_20rem_1fr]"; } return "grid-cols-[20rem_20rem_1fr]"; - } else if (mainPanelReduced === true) { + } else if (mainPanelReduced) { return "grid-cols-[6rem_0px_1fr]"; } return "grid-cols-[20rem_0px_1fr]"; @@ -262,7 +260,7 @@ export const AppLayout = ({ `absolute inset-0 transition-[backdrop-filter] duration-500 [grid-area:content] mobile:z-10`, cIf( - (mainPanelOpen === true || subPanelOpen === true) && isMobile, + (mainPanelOpen || subPanelOpen) && isMobile, "[backdrop-filter:blur(2px)]", "pointer-events-none touch-none" ) @@ -272,7 +270,7 @@ export const AppLayout = ({ className={cJoin( "absolute inset-0 bg-shade transition-opacity duration-500", cIf( - (mainPanelOpen === true || subPanelOpen === true) && isMobile, + (mainPanelOpen || subPanelOpen) && isMobile, "opacity-60", "opacity-0" ) @@ -312,7 +310,7 @@ export const AppLayout = ({ mobile:border-r-0 mobile:border-l-[1px] mobile:[grid-area:content]`, turnSubIntoContent ? "mobile:w-full mobile:border-l-0" - : subPanelOpen === true + : subPanelOpen ? "" : "mobile:translate-x-[100vw]" )} @@ -328,7 +326,7 @@ export const AppLayout = ({ transition-transform duration-300 [grid-area:main] [scrollbar-width:none] webkit-scrollbar:w-0 mobile:z-10 mobile:w-[90%] mobile:justify-self-start mobile:[grid-area:content]`, - cIf(mainPanelOpen === false, "mobile:-translate-x-full") + cIf(!mainPanelOpen, "mobile:-translate-x-full") )} > @@ -340,7 +338,7 @@ export const AppLayout = ({ border-t-[1px] border-dotted border-black bg-light [grid-area:navbar] desktop:hidden" > { toggleMainPanelOpen(); @@ -365,7 +363,7 @@ export const AppLayout = ({

{isDefined(subPanel) && !turnSubIntoContent && ( { toggleSubPanelOpen(); @@ -404,7 +402,7 @@ export const AppLayout = ({ setConfigPanelOpen(false)} >

{langui.settings}

@@ -416,7 +414,7 @@ export const AppLayout = ({ {router.locales && (

{langui.languages}

- {preferredLanguages && preferredLanguages.length > 0 && ( + {preferredLanguages.length > 0 && ( { setSelectedThemeMode(false); }, - active: selectedThemeMode === false, + active: selectedThemeMode, text: langui.auto, }, { @@ -468,7 +466,7 @@ export const AppLayout = ({ setDarkMode(true); setSelectedThemeMode(true); }, - active: selectedThemeMode === true && darkMode === true, + active: selectedThemeMode && darkMode, text: langui.dark, }, ]} @@ -492,20 +490,17 @@ export const AppLayout = ({ setFontSize((fontSize ?? 1) / 1.05), + onClick: () => setFontSize(fontSize / 1.05), icon: Icon.TextDecrease, }, { onClick: () => setFontSize(1), - text: `${((fontSize ?? 1) * 100).toLocaleString( - undefined, - { - maximumFractionDigits: 0, - } - )}%`, + text: `${(fontSize * 100).toLocaleString(undefined, { + maximumFractionDigits: 0, + })}%`, }, { - onClick: () => setFontSize((fontSize ?? 1) * 1.05), + onClick: () => setFontSize(fontSize * 1.05), icon: Icon.TextIncrease, }, ]} @@ -516,13 +511,13 @@ export const AppLayout = ({

{langui.font}

diff --git a/src/components/Inputs/Select.tsx b/src/components/Inputs/Select.tsx index 203a6b2..2cd1279 100644 --- a/src/components/Inputs/Select.tsx +++ b/src/components/Inputs/Select.tsx @@ -1,5 +1,5 @@ -import { Fragment, useCallback } from "react"; -import { useBoolean } from "usehooks-ts"; +import { Fragment, useCallback, useRef } from "react"; +import { useBoolean, useOnClickOutside } from "usehooks-ts"; import { Ico, Icon } from "components/Ico"; import { cIf, cJoin } from "helpers/className"; @@ -37,11 +37,12 @@ export const Select = ({ if (optionCount > 1) toggleOpened(); }, [options.length, value, toggleOpened]); + const ref = useRef(null); + useOnClickOutside(ref, setClosed); + return (
{ - setClosed(); - }} + ref={ref} className={cJoin( "relative text-center transition-[filter]", cIf(isOpened, "z-10 drop-shadow-shade-lg"), diff --git a/src/components/Library/PreviewCardCTAs.tsx b/src/components/Library/PreviewCardCTAs.tsx index ad42f00..432ee48 100644 --- a/src/components/Library/PreviewCardCTAs.tsx +++ b/src/components/Library/PreviewCardCTAs.tsx @@ -35,7 +35,7 @@ export const PreviewCardCTAs = ({