2021-07-14 10:42:16 +00:00
|
|
|
import * as Dialog from '@radix-ui/react-alert-dialog'
|
|
|
|
import { MixerVerticalIcon } from '@radix-ui/react-icons'
|
|
|
|
import {
|
|
|
|
breakpoints,
|
|
|
|
IconButton,
|
|
|
|
DialogOverlay,
|
|
|
|
DialogContent,
|
|
|
|
RowButton,
|
|
|
|
MenuTextInput,
|
|
|
|
DialogInputWrapper,
|
|
|
|
Divider,
|
|
|
|
} from 'components/shared'
|
2021-07-14 14:05:58 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2021-07-14 10:42:16 +00:00
|
|
|
import state, { useSelector } from 'state'
|
|
|
|
import { Page } from 'types'
|
|
|
|
|
|
|
|
export default function PageOptions({ page }: { page: Page }): JSX.Element {
|
2021-07-14 14:05:58 +00:00
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
|
|
|
|
const rInput = useRef<HTMLInputElement>(null)
|
|
|
|
|
2021-07-14 10:42:16 +00:00
|
|
|
const hasOnlyOnePage = useSelector(
|
|
|
|
(s) => Object.keys(s.data.document.pages).length <= 1
|
|
|
|
)
|
|
|
|
|
2021-07-14 11:39:34 +00:00
|
|
|
const [name, setName] = useState(page.name)
|
|
|
|
|
2021-07-14 10:42:16 +00:00
|
|
|
function handleNameChange(e: React.ChangeEvent<HTMLInputElement>) {
|
2021-07-14 11:39:34 +00:00
|
|
|
setName(e.currentTarget.value)
|
2021-07-14 10:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleDuplicate() {
|
|
|
|
state.send('DUPLICATED_PAGE', { id: page.id })
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDelete() {
|
|
|
|
state.send('DELETED_PAGE', { id: page.id })
|
|
|
|
}
|
|
|
|
|
2021-07-14 11:39:34 +00:00
|
|
|
function handleOpenChange(isOpen: boolean) {
|
2021-07-14 14:05:58 +00:00
|
|
|
setIsOpen(isOpen)
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
return
|
|
|
|
}
|
2021-07-14 11:39:34 +00:00
|
|
|
|
2021-07-14 10:42:16 +00:00
|
|
|
if (page.name.length === 0) {
|
2021-07-14 11:39:34 +00:00
|
|
|
state.send('RENAMED_PAGE', {
|
2021-07-14 10:42:16 +00:00
|
|
|
id: page.id,
|
|
|
|
name: 'Page',
|
|
|
|
})
|
|
|
|
}
|
2021-07-14 11:39:34 +00:00
|
|
|
|
|
|
|
state.send('SAVED_PAGE_RENAME', { id: page.id })
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSave() {
|
|
|
|
state.send('RENAMED_PAGE', {
|
|
|
|
id: page.id,
|
|
|
|
name,
|
|
|
|
})
|
2021-07-14 10:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function stopPropagation(e: React.KeyboardEvent<HTMLDivElement>) {
|
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:05:58 +00:00
|
|
|
function handleKeydown(e: React.KeyboardEvent<HTMLDivElement>) {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
handleSave()
|
|
|
|
setIsOpen(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isOpen) {
|
|
|
|
setTimeout(() => {
|
|
|
|
rInput.current?.focus()
|
|
|
|
rInput.current?.select()
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
}, [isOpen])
|
|
|
|
|
2021-07-14 10:42:16 +00:00
|
|
|
return (
|
2021-07-14 14:05:58 +00:00
|
|
|
<Dialog.Root open={isOpen} onOpenChange={handleOpenChange}>
|
2021-07-14 10:42:16 +00:00
|
|
|
<Dialog.Trigger
|
|
|
|
as={IconButton}
|
|
|
|
bp={breakpoints}
|
|
|
|
size="small"
|
|
|
|
data-shy="true"
|
|
|
|
>
|
|
|
|
<MixerVerticalIcon />
|
|
|
|
</Dialog.Trigger>
|
|
|
|
<Dialog.Overlay as={DialogOverlay} />
|
|
|
|
<Dialog.Content
|
|
|
|
as={DialogContent}
|
|
|
|
onKeyDown={stopPropagation}
|
|
|
|
onKeyUp={stopPropagation}
|
|
|
|
>
|
|
|
|
<DialogInputWrapper>
|
2021-07-14 14:05:58 +00:00
|
|
|
<MenuTextInput
|
|
|
|
ref={rInput}
|
|
|
|
value={name}
|
|
|
|
onChange={handleNameChange}
|
|
|
|
onKeyDown={handleKeydown}
|
|
|
|
/>
|
2021-07-14 10:42:16 +00:00
|
|
|
</DialogInputWrapper>
|
|
|
|
<Divider />
|
|
|
|
<Dialog.Action
|
|
|
|
as={RowButton}
|
|
|
|
bp={breakpoints}
|
|
|
|
onClick={handleDuplicate}
|
|
|
|
>
|
|
|
|
Duplicate
|
|
|
|
</Dialog.Action>
|
|
|
|
<Dialog.Action
|
|
|
|
as={RowButton}
|
|
|
|
bp={breakpoints}
|
|
|
|
disabled={hasOnlyOnePage}
|
|
|
|
onClick={handleDelete}
|
|
|
|
warn={true}
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</Dialog.Action>
|
|
|
|
<Divider />
|
2021-07-14 11:39:34 +00:00
|
|
|
<Dialog.Action as={RowButton} bp={breakpoints} onClick={handleSave}>
|
|
|
|
Save
|
|
|
|
</Dialog.Action>
|
2021-07-14 10:42:16 +00:00
|
|
|
<Dialog.Cancel as={RowButton} bp={breakpoints}>
|
|
|
|
Cancel
|
|
|
|
</Dialog.Cancel>
|
|
|
|
</Dialog.Content>
|
|
|
|
</Dialog.Root>
|
|
|
|
)
|
|
|
|
}
|