import React, { useMemo, useState } from 'react'; import isNull from 'lodash/isNull'; import { cx } from '@leafygreen-ui/emotion'; import { usePoller, useViewportSize } from '@leafygreen-ui/hooks'; // @ts-expect-error import ActivityFeedIcon from '@leafygreen-ui/icon/dist/ActivityFeed'; // @ts-expect-error import BellIcon from '@leafygreen-ui/icon/dist/Bell'; // @ts-expect-error import InviteUserIcon from '@leafygreen-ui/icon/dist/InviteUser'; // @ts-expect-error import VerticalEllipsisIcon from '@leafygreen-ui/icon/dist/VerticalEllipsis'; import IconButton from '@leafygreen-ui/icon-button'; import { useDarkMode } from '@leafygreen-ui/leafygreen-provider'; import { Menu, MenuItem } from '@leafygreen-ui/menu'; import Tooltip from '@leafygreen-ui/tooltip'; import { breakpoints } from '../breakpoints'; import { ProjectSelect } from '../mongo-select'; import { useMongoNavContext } from '../MongoNavContext'; import { useOnElementClick } from '../on-element-click-provider'; import { navContainerBaseStyle, navContainerThemeStyle, navSectionStyle, } from '../styles'; import { ActiveNavElement, Environment, Mode, NavElement, ProjectStatus, } from '../types'; import { ProductTabs } from './ProductTabs'; import { ProjectStatusBadge } from './project-nav-helpers'; import { alertBadgeStyle, iconButtonThemeStyles, menuIconButtonStyle, projectNavContainerBaseStyle, projectNavContainerThemeStyle, projectNavFirstSectionStyle, projectSelectWrapperstyle, tooltipStyles, } from './ProjectNav.styles'; import { ProjectNavProps } from './ProjectNav.types'; const { ProjectNavProjectDropdown, ProjectNavProjectSettings, ProjectNavProjectSupport, ProjectNavProjectIntegrations, ProjectNavInvite, ProjectNavActivityFeed, ProjectNavAlerts, } = NavElement; export default function ProjectNav({ /* eslint-disable react/prop-types */ admin, data, constructProjectURL, urls, onProjectChange, hosts, mode, alertPollingInterval, environment = Environment.Commercial, }: /* eslint-enable react/prop-types */ ProjectNavProps) { const [open, setOpen] = useState(false); const { theme } = useDarkMode(); const { activeNav, isLoading, currentProject: current, } = useMongoNavContext(); const [alerts, setAlerts] = useState(current?.alertsOpen ?? 0); const onElementClick = useOnElementClick(); const viewportSize = useViewportSize(); const isDisabled = isLoading || isNull(current); function fetchAlertsCount() { return fetch( `${hosts.cloud}/user/shared/alerts/project/${currentProjectId}`, { credentials: 'include', mode: 'cors', method: 'GET', }, ) .then(response => response.json()) .then(result => setAlerts(result.length)) .catch(console.error); } const currentProjectId = current?.projectId; usePoller(fetchAlertsCount, { interval: alertPollingInterval, enabled: mode === Mode.Production && currentProjectId != null, }); const isMobile = viewportSize ? viewportSize.width < breakpoints.small : false; const isProjectInvite = !isDisabled && activeNav === ActiveNavElement.ProjectNavInvite; const isActivityFeed = !isDisabled && activeNav === ActiveNavElement.ProjectNavActivityFeed; const isProjectAlerts = !isDisabled && activeNav === ActiveNavElement.ProjectNavAlerts; const sharedTooltipOnClick: React.MouseEventHandler = e => e.preventDefault(); const sharedTooltipProps = useMemo(() => { return { variant: 'dark', usePortal: false, className: tooltipStyles, onClick: sharedTooltipOnClick, }; }, []); return ( ); }