Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track last state left and right panel #3477

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions web/containers/Providers/Responsive.tsx
Original file line number Diff line number Diff line change
@@ -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<boolean>(true)
const lastRightPanelState = useRef<boolean>(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 <Fragment>{children}</Fragment>
Expand Down
Loading