2021-06-03 12:06:39 +00:00
|
|
|
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
|
2021-07-10 20:39:29 +00:00
|
|
|
import styled from 'styles'
|
|
|
|
import {
|
|
|
|
breakpoints,
|
|
|
|
DropdownMenuButton,
|
|
|
|
DropdownMenuDivider,
|
|
|
|
RowButton,
|
|
|
|
MenuContent,
|
|
|
|
FloatingContainer,
|
|
|
|
IconWrapper,
|
|
|
|
} from 'components/shared'
|
2021-07-14 10:42:16 +00:00
|
|
|
import PageOptions from './page-options'
|
|
|
|
import { PlusIcon, CheckIcon } from '@radix-ui/react-icons'
|
2021-06-03 12:06:39 +00:00
|
|
|
import state, { useSelector } from 'state'
|
2021-06-03 13:10:54 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2021-06-03 12:06:39 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function PagePanel(): JSX.Element {
|
2021-06-03 13:10:54 +00:00
|
|
|
const rIsOpen = useRef(false)
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (rIsOpen.current !== isOpen) {
|
|
|
|
rIsOpen.current = isOpen
|
|
|
|
}
|
|
|
|
}, [isOpen])
|
|
|
|
|
2021-06-03 12:06:39 +00:00
|
|
|
const documentPages = useSelector((s) => s.data.document.pages)
|
2021-06-03 13:10:54 +00:00
|
|
|
const currentPageId = useSelector((s) => s.data.currentPageId)
|
|
|
|
|
|
|
|
if (!documentPages[currentPageId]) return null
|
2021-06-03 12:06:39 +00:00
|
|
|
|
|
|
|
const sorted = Object.values(documentPages).sort(
|
|
|
|
(a, b) => a.childIndex - b.childIndex
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2021-06-03 16:13:23 +00:00
|
|
|
<DropdownMenu.Root
|
2021-06-27 19:19:57 +00:00
|
|
|
dir="ltr"
|
2021-06-03 16:13:23 +00:00
|
|
|
open={isOpen}
|
|
|
|
onOpenChange={(isOpen) => {
|
|
|
|
if (rIsOpen.current !== isOpen) {
|
|
|
|
setIsOpen(isOpen)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2021-07-10 20:39:29 +00:00
|
|
|
<FloatingContainer>
|
|
|
|
<RowButton as={DropdownMenu.Trigger} bp={breakpoints}>
|
2021-06-03 16:13:23 +00:00
|
|
|
<span>{documentPages[currentPageId].name}</span>
|
2021-07-10 20:39:29 +00:00
|
|
|
</RowButton>
|
|
|
|
</FloatingContainer>
|
2021-07-14 10:42:16 +00:00
|
|
|
<MenuContent as={DropdownMenu.Content} sideOffset={8} align="start">
|
2021-07-10 20:39:29 +00:00
|
|
|
<DropdownMenu.RadioGroup
|
|
|
|
value={currentPageId}
|
2021-07-10 22:07:53 +00:00
|
|
|
onValueChange={(id) => {
|
2021-07-10 20:39:29 +00:00
|
|
|
setIsOpen(false)
|
|
|
|
state.send('CHANGED_PAGE', { id })
|
|
|
|
}}
|
|
|
|
>
|
2021-07-14 10:42:16 +00:00
|
|
|
{sorted.map((page) => (
|
|
|
|
<ButtonWithOptions key={page.id}>
|
2021-07-10 20:39:29 +00:00
|
|
|
<DropdownMenu.RadioItem
|
|
|
|
as={RowButton}
|
|
|
|
bp={breakpoints}
|
2021-07-14 10:42:16 +00:00
|
|
|
value={page.id}
|
2021-07-10 20:39:29 +00:00
|
|
|
variant="pageButton"
|
|
|
|
>
|
2021-07-14 10:42:16 +00:00
|
|
|
<span>{page.name}</span>
|
2021-07-10 20:39:29 +00:00
|
|
|
<DropdownMenu.ItemIndicator>
|
2021-07-14 10:42:16 +00:00
|
|
|
<IconWrapper size="small">
|
2021-07-10 20:39:29 +00:00
|
|
|
<CheckIcon />
|
|
|
|
</IconWrapper>
|
|
|
|
</DropdownMenu.ItemIndicator>
|
|
|
|
</DropdownMenu.RadioItem>
|
2021-07-14 10:42:16 +00:00
|
|
|
<PageOptions page={page} />
|
2021-07-10 20:39:29 +00:00
|
|
|
</ButtonWithOptions>
|
|
|
|
))}
|
|
|
|
</DropdownMenu.RadioGroup>
|
|
|
|
<DropdownMenuDivider />
|
|
|
|
<DropdownMenuButton onSelect={() => state.send('CREATED_PAGE')}>
|
|
|
|
<span>Create Page</span>
|
|
|
|
<IconWrapper size="small">
|
|
|
|
<PlusIcon />
|
|
|
|
</IconWrapper>
|
|
|
|
</DropdownMenuButton>
|
|
|
|
</MenuContent>
|
2021-06-03 16:13:23 +00:00
|
|
|
</DropdownMenu.Root>
|
2021-06-03 12:06:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-10 20:39:29 +00:00
|
|
|
const ButtonWithOptions = styled('div', {
|
|
|
|
display: 'grid',
|
|
|
|
gridTemplateColumns: '1fr auto',
|
|
|
|
gridAutoFlow: 'column',
|
2021-06-03 12:06:39 +00:00
|
|
|
|
2021-07-10 20:39:29 +00:00
|
|
|
'& > *[data-shy="true"]': {
|
|
|
|
opacity: 0,
|
2021-06-03 13:10:54 +00:00
|
|
|
},
|
|
|
|
|
2021-07-10 20:39:29 +00:00
|
|
|
'&:hover > *[data-shy="true"]': {
|
|
|
|
opacity: 1,
|
2021-06-03 13:10:54 +00:00
|
|
|
},
|
|
|
|
})
|