From 49166d020980bf9d8fb93beb1d6adbacbe3fe458 Mon Sep 17 00:00:00 2001 From: Faisal Amir Date: Wed, 28 Aug 2024 20:20:12 +0700 Subject: [PATCH] feat: track last state left and right panel (#3477) --- web/containers/Providers/Responsive.tsx | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/web/containers/Providers/Responsive.tsx b/web/containers/Providers/Responsive.tsx index 4c9e87ab4..940cb68fb 100644 --- a/web/containers/Providers/Responsive.tsx +++ b/web/containers/Providers/Responsive.tsx @@ -1,24 +1,33 @@ -import { Fragment, PropsWithChildren, useEffect } from 'react' +import { Fragment, PropsWithChildren, useEffect, useRef } from 'react' import { useMediaQuery } from '@janhq/joi' - -import { useSetAtom } from 'jotai' +import { useAtom } from 'jotai' import { showLeftPanelAtom, showRightPanelAtom } from '@/helpers/atoms/App.atom' const Responsive = ({ children }: PropsWithChildren) => { const matches = useMediaQuery('(max-width: 880px)') - const setShowLeftPanel = useSetAtom(showLeftPanelAtom) - const setShowRightPanel = useSetAtom(showRightPanelAtom) + const [showLeftPanel, setShowLeftPanel] = useAtom(showLeftPanelAtom) + const [showRightPanel, setShowRightPanel] = useAtom(showRightPanelAtom) + + // Refs to store the last known state of the panels + const lastLeftPanelState = useRef(true) + const lastRightPanelState = useRef(true) useEffect(() => { if (matches) { + // Store the last known state before closing the panels + lastLeftPanelState.current = showLeftPanel + lastRightPanelState.current = showRightPanel + setShowLeftPanel(false) setShowRightPanel(false) } else { - setShowLeftPanel(true) - setShowRightPanel(true) + // Restore the last known state when the screen is resized back + setShowLeftPanel(lastLeftPanelState.current) + setShowRightPanel(lastRightPanelState.current) } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [matches, setShowLeftPanel, setShowRightPanel]) return {children}