import {AddIcon, DeleteIcon, ViewIcon} from '@chakra-ui/icons' import { Box, Button, ButtonGroup, Divider, HStack, IconButton, Stack, Tooltip } from '@chakra-ui/react' import {FaGlobeEurope} from '@react-icons/all-files/fa/FaGlobeEurope' import * as React from 'react' import {JaenPageProvider} from '../../../../internal/services/page' import {IJaenPage} from '../../../../types' import { pageUpdateValidation, usePageManager } from '../../providers/PageManagerProvider' import {ContentValues, PageContent} from './PageContent' import PageTree, {PageTreeProps} from './PageTree' export interface PagesTabProps extends PageTreeProps { getPage: (id: string) => IJaenPage | null onPageUpdate: (id: string, values: ContentValues) => void } /** * PagesTab is a component for displaying the page tree with content in the dashboard. * Display PageTree and PageContent next to each other. */ const PagesTab = () => { const manager = usePageManager() const rootPageId = manager.rootPageId const landingPage = React.useMemo(() => manager.onGet(rootPageId), [ rootPageId ]) as IJaenPage const [selection, setSelection] = React.useState(landingPage) const relativeRootPageId = selection.id === rootPageId ? null : selection.id const isSelectionLandingPage = selection?.id === rootPageId const onSelect = (id: string | null) => { const page = (id && manager.onGet(id)) || landingPage setSelection(page) } const handlePageUpdate = React.useCallback( (values: ContentValues) => { selection && manager.onUpdate(selection.id, values) }, [selection] ) const handleItemDelete = React.useCallback(() => { const id = selection && selection.id setSelection(landingPage) id && manager.onDelete(id) }, [selection]) const selectedTemplate = React.useMemo( () => manager.templates.find(t => t.name === selection?.template) || null, [manager.templates, selection?.template] ) const pageTitle = selection?.jaenPageMetadata.title || 'Page' const templatesForPage = React.useMemo( () => manager.templates.find(t => t.name === selection?.template), [manager.templates, selection?.template] ) const disableAdd = templatesForPage ? templatesForPage.children.length === 0 : false const disableDelete = !selection?.template || isSelectionLandingPage const disableNavigate = !selection return ( <> } disabled={disableAdd} onClick={() => manager.onToggleCreator(relativeRootPageId)} /> } onClick={handleItemDelete} disabled={disableDelete} /> } onClick={() => manager.onNavigate(selection?.id!)} disabled={disableNavigate} /> {selection && ( { return pageUpdateValidation({ name, value, parentId: selection.parent?.id, treeItems: manager.tree }) }} /> )} ) } export default PagesTab