UNPKG

@aws-northstar/ui

Version:
102 lines (98 loc) 6.66 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ import { useState, useCallback, createContext, useContext, useMemo, useEffect, createElement, isValidElement, } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import BreadcrumbGroup from '@cloudscape-design/components/breadcrumb-group'; import SideNavigation from '@cloudscape-design/components/side-navigation'; import SplitPanel from '@cloudscape-design/components/split-panel'; import Box from '@cloudscape-design/components/box'; import AppLayoutComponent from '@cloudscape-design/components/app-layout'; import ContentLayout from '@cloudscape-design/components/content-layout'; import NavHeader from './components/NavHeader'; import { splitPanelI18nStrings } from './constants'; import getBreadcrumbs from './utils/getBreadcrumbs'; const initialState = { setContentType: () => { }, setSplitPanelOpen: () => { }, setSplitPanelSize: () => { }, setSplitPanelProps: () => { }, setSplitPanelPreferences: () => { }, setNotifications: () => { }, setNavigationOpen: () => { }, setTools: () => { }, setToolsHide: () => { }, setToolsOpen: () => { }, setToolsWidth: () => { }, }; export const AppLayoutContext = createContext(initialState); const ContentLayoutType = createElement(ContentLayout).type; /** * Provides the basic layout for all types of pages, including collapsible side navigation, tools panel, and split panel. */ const AppLayout = ({ title, defaultBreadcrumb = 'home', availableRoutes, breadcrumbGroupHide, children, ...props }) => { const navigate = useNavigate(); const [contentType, setContentType] = useState(props.contentType); const [splitPanelOpen, setSplitPanelOpen] = useState(props.splitPanelOpen ?? false); const [splitPanelSize, setSplitPanelSize] = useState(props.splitPanelSize); const [splitPanelProps, setSplitPanelProps] = useState(); const [splitPanelPreferences, setSplitPanelPreferences] = useState(props.splitPanelPreferences ?? { position: 'bottom' }); const [navigationOpen, setNavigationOpen] = useState(props.navigationOpen ?? false); const [notifications, setNotifications] = useState(props.notifications); const [tools, setTools] = useState(props.tools); const [toolsHide, setToolsHide] = useState(props.toolsHide ?? true); const [toolsOpen, setToolsOpen] = useState(props.toolsOpen ?? false); const [toolsWidth, setToolsWidth] = useState(props.toolsWidth); const [activeHref, setActiveHref] = useState('/'); const [activeBreadcrumbs, setActiveBreadcrumbs] = useState([ { text: defaultBreadcrumb, href: '/' }, ]); const location = useLocation(); useEffect(() => { setActiveHref(location.pathname); const breadcrumbs = getBreadcrumbs(location.pathname, location.search, defaultBreadcrumb, availableRoutes); setActiveBreadcrumbs(breadcrumbs); }, [location, defaultBreadcrumb, availableRoutes]); const onNavigate = useCallback((e) => { if (!e.detail.external) { e.preventDefault(); setContentType(undefined); setSplitPanelOpen(false); setSplitPanelSize(undefined); setSplitPanelProps(undefined); navigate(e.detail.href); } }, [navigate]); const splitPanel = useMemo(() => { return splitPanelProps ? (_jsx(SplitPanel, { i18nStrings: splitPanelI18nStrings, ...splitPanelProps, children: splitPanelProps?.children })) : null; }, [splitPanelProps]); useEffect(() => { setNotifications(props.notifications); }, [props.notifications]); return (_jsxs(AppLayoutContext.Provider, { value: { setContentType, setSplitPanelOpen, setSplitPanelSize, setSplitPanelProps, setSplitPanelPreferences, setNotifications, setNavigationOpen, setTools, setToolsHide, setToolsOpen, setToolsWidth, }, children: ['header' in props ? (props.header) : (_jsx(NavHeader, { title: title, href: props.href, logo: props.logo, user: props.user, notificationsUtility: props.notificationsUtility, onSignout: props.onSignout })), _jsx(AppLayoutComponent, { headerSelector: 'header' in props ? undefined : '#northstar_applayout_header', breadcrumbs: breadcrumbGroupHide ? undefined : 'breadcrumbGroup' in props ? (props.breadcrumbGroup) : (_jsx(BreadcrumbGroup, { onFollow: onNavigate, items: activeBreadcrumbs })), navigation: 'navigation' in props ? (props.navigation) : (_jsx(SideNavigation, { header: { text: title, href: '/' }, activeHref: activeHref, onFollow: onNavigate, items: props.navigationItems })), content: (!contentType || contentType === 'default') && !(children && isValidElement(children) && children.type === ContentLayoutType) ? (_jsx(Box, { padding: { top: 'l' }, children: children })) : (children), ...props, contentType: contentType, splitPanelOpen: splitPanelOpen, splitPanelSize: splitPanelSize, splitPanel: splitPanel, splitPanelPreferences: splitPanelPreferences, onSplitPanelToggle: ({ detail }) => setSplitPanelOpen(detail.open), onSplitPanelResize: ({ detail }) => setSplitPanelSize(detail.size), onSplitPanelPreferencesChange: ({ detail }) => setSplitPanelPreferences(detail), notifications: notifications, navigationOpen: navigationOpen, onNavigationChange: ({ detail }) => setNavigationOpen(detail.open), toolsHide: toolsHide, tools: tools, toolsOpen: toolsOpen, toolsWidth: toolsWidth, onToolsChange: ({ detail }) => setToolsOpen(detail.open) })] })); }; export const useAppLayoutContext = () => useContext(AppLayoutContext); export default AppLayout;