2022-08-09 09:21:15 +00:00
import type { User } from "@prisma/client" ;
import { signOut , useSession } from "next-auth/react" ;
import Link from "next/link" ;
2023-02-20 14:11:51 +00:00
import type { NextRouter } from "next/router" ;
import { useRouter } from "next/router" ;
import type { Dispatch , ReactNode , SetStateAction } from "react" ;
import React , { Fragment , useEffect , useState } from "react" ;
2022-08-09 09:21:15 +00:00
import { Toaster } from "react-hot-toast" ;
import dayjs from "@calcom/dayjs" ;
import { useIsEmbed } from "@calcom/embed-core/embed-iframe" ;
2022-10-10 17:00:09 +00:00
import UnconfirmedBookingBadge from "@calcom/features/bookings/UnconfirmedBookingBadge" ;
2022-08-09 09:21:15 +00:00
import ImpersonatingBanner from "@calcom/features/ee/impersonation/components/ImpersonatingBanner" ;
import HelpMenuItem from "@calcom/features/ee/support/components/HelpMenuItem" ;
2022-11-16 21:07:20 +00:00
import { TeamsUpgradeBanner } from "@calcom/features/ee/teams/components" ;
2023-01-10 15:39:29 +00:00
import { KBarContent , KBarRoot , KBarTrigger } from "@calcom/features/kbar/Kbar" ;
2023-01-09 13:57:12 +00:00
import TimezoneChangeDialog from "@calcom/features/settings/TimezoneChangeDialog" ;
2022-12-22 19:06:26 +00:00
import { Tips } from "@calcom/features/tips" ;
2023-01-01 11:19:58 +00:00
import AdminPasswordBanner from "@calcom/features/users/components/AdminPasswordBanner" ;
2022-08-09 09:21:15 +00:00
import CustomBranding from "@calcom/lib/CustomBranding" ;
import classNames from "@calcom/lib/classNames" ;
2023-01-02 19:44:51 +00:00
import { APP_NAME , DESKTOP_APP_LINK , JOIN_SLACK , ROADMAP , WEBAPP_URL } from "@calcom/lib/constants" ;
2022-08-09 09:21:15 +00:00
import { useLocale } from "@calcom/lib/hooks/useLocale" ;
import useTheme from "@calcom/lib/hooks/useTheme" ;
import { trpc } from "@calcom/trpc/react" ;
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery" ;
2023-02-20 14:11:51 +00:00
import type { SVGComponent } from "@calcom/types/SVGComponent" ;
2022-11-23 02:55:25 +00:00
import {
Button ,
2023-01-12 22:11:15 +00:00
Credits ,
2022-11-23 02:55:25 +00:00
Dropdown ,
2022-08-09 09:21:15 +00:00
DropdownMenuContent ,
DropdownMenuItem ,
2023-01-15 11:40:40 +00:00
DropdownItem ,
2022-11-16 21:07:20 +00:00
DropdownMenuPortal ,
2022-08-09 09:21:15 +00:00
DropdownMenuSeparator ,
DropdownMenuTrigger ,
2023-01-10 15:39:29 +00:00
ErrorBoundary ,
2023-01-12 22:11:15 +00:00
Logo ,
2023-01-23 23:08:01 +00:00
HeadSeo ,
2023-01-12 22:11:15 +00:00
showToast ,
2023-01-10 15:39:29 +00:00
SkeletonText ,
} from "@calcom/ui" ;
2023-01-23 23:08:01 +00:00
import {
FiMoreVertical ,
FiMoon ,
FiExternalLink ,
FiLink ,
FiSlack ,
FiMap ,
FiHelpCircle ,
FiDownload ,
FiLogOut ,
FiCalendar ,
FiClock ,
FiUsers ,
FiGrid ,
FiMoreHorizontal ,
FiFileText ,
FiZap ,
FiSettings ,
FiArrowRight ,
FiArrowLeft ,
} from "@calcom/ui/components/icon" ;
2022-08-24 20:18:42 +00:00
2023-03-08 11:30:24 +00:00
import FreshChatProvider from "../ee/support/lib/freshchat/FreshChatProvider" ;
2023-01-24 15:27:05 +00:00
import { TeamInviteBadge } from "./TeamInviteBadge" ;
2022-08-24 20:18:42 +00:00
/* TODO: Migate this */
2022-08-09 09:21:15 +00:00
export const ONBOARDING_INTRODUCED_AT = dayjs ( "September 1 2021" ) . toISOString ( ) ;
export const ONBOARDING_NEXT_REDIRECT = {
redirect : {
permanent : false ,
destination : "/getting-started" ,
} ,
} as const ;
export const shouldShowOnboarding = ( user : Pick < User , " createdDate " | " completedOnboarding " > ) = > {
return ! user . completedOnboarding && dayjs ( user . createdDate ) . isAfter ( ONBOARDING_INTRODUCED_AT ) ;
} ;
function useRedirectToLoginIfUnauthenticated ( isPublic = false ) {
const { data : session , status } = useSession ( ) ;
const loading = status === "loading" ;
const router = useRouter ( ) ;
useEffect ( ( ) = > {
if ( isPublic ) {
return ;
}
if ( ! loading && ! session ) {
router . replace ( {
pathname : "/auth/login" ,
query : {
callbackUrl : ` ${ WEBAPP_URL } ${ location . pathname } ${ location . search } ` ,
} ,
} ) ;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
} , [ loading , session , isPublic ] ) ;
return {
loading : loading && ! session ,
session ,
} ;
}
function useRedirectToOnboardingIfNeeded() {
const router = useRouter ( ) ;
const query = useMeQuery ( ) ;
const user = query . data ;
const isRedirectingToOnboarding = user && shouldShowOnboarding ( user ) ;
useEffect ( ( ) = > {
if ( isRedirectingToOnboarding ) {
router . replace ( {
pathname : "/getting-started" ,
} ) ;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
} , [ isRedirectingToOnboarding ] ) ;
return {
isRedirectingToOnboarding ,
} ;
}
const Layout = ( props : LayoutProps ) = > {
2022-09-29 12:41:40 +00:00
const pageTitle = typeof props . heading === "string" && ! props . title ? props.heading : props.title ;
2022-08-09 09:21:15 +00:00
return (
< >
2022-10-18 17:46:22 +00:00
{ ! props . withoutSeo && (
< HeadSeo
2022-11-30 21:52:56 +00:00
title = { pageTitle ? ? APP_NAME }
2022-10-18 17:46:22 +00:00
description = { props . subtitle ? props . subtitle ? . toString ( ) : "" }
/ >
) }
2022-08-09 09:21:15 +00:00
< div >
< Toaster position = "bottom-right" / >
< / div >
2022-10-05 14:33:29 +00:00
{ /* todo: only run this if timezone is different */ }
< TimezoneChangeDialog / >
2022-12-19 17:37:20 +00:00
< div className = "flex min-h-screen flex-col" >
< div className = "divide-y divide-black" >
< TeamsUpgradeBanner / >
< ImpersonatingBanner / >
2023-01-01 11:19:58 +00:00
< AdminPasswordBanner / >
2022-12-19 17:37:20 +00:00
< / div >
< div className = "flex flex-1" data - testid = "dashboard-shell" >
2022-11-15 19:33:59 +00:00
{ props . SidebarContainer || < SideBarContainer / > }
2022-12-19 17:37:20 +00:00
< div className = "flex w-0 flex-1 flex-col" >
2022-11-15 19:33:59 +00:00
< MainContainer { ...props } / >
< / div >
2022-08-09 09:21:15 +00:00
< / div >
< / div >
< / >
) ;
} ;
2022-09-07 01:33:50 +00:00
type DrawerState = [ isOpen : boolean , setDrawerOpen : Dispatch < SetStateAction < boolean > > ] ;
2022-08-09 09:21:15 +00:00
type LayoutProps = {
centered? : boolean ;
title? : string ;
heading? : ReactNode ;
subtitle? : ReactNode ;
2023-01-14 19:11:28 +00:00
headerClassName? : string ;
2022-08-09 09:21:15 +00:00
children : ReactNode ;
CTA? : ReactNode ;
large? : boolean ;
2022-09-07 01:33:50 +00:00
MobileNavigationContainer? : ReactNode ;
2022-08-26 00:11:41 +00:00
SidebarContainer? : ReactNode ;
2022-09-07 01:33:50 +00:00
TopNavContainer? : ReactNode ;
drawerState? : DrawerState ;
2022-08-09 09:21:15 +00:00
HeadingLeftIcon? : ReactNode ;
2022-10-25 00:29:49 +00:00
backPath? : string | boolean ; // renders back button to specified path
2022-08-09 09:21:15 +00:00
// use when content needs to expand with flex
flexChildrenContainer? : boolean ;
isPublic? : boolean ;
2022-09-02 19:00:41 +00:00
withoutMain? : boolean ;
2022-10-18 17:46:22 +00:00
// Gives you the option to skip HeadSEO and render your own.
withoutSeo? : boolean ;
2022-11-30 20:51:44 +00:00
// Gives the ability to include actions to the right of the heading
actions? : JSX.Element ;
2023-01-29 09:38:31 +00:00
smallHeading? : boolean ;
2022-08-09 09:21:15 +00:00
} ;
const CustomBrandingContainer = ( ) = > {
const { data : user } = useMeQuery ( ) ;
return < CustomBranding lightVal = { user ? . brandColor } darkVal = { user ? . darkBrandColor } / > ;
} ;
2023-01-12 22:11:15 +00:00
const KBarWrapper = ( { children , withKBar = false } : { withKBar : boolean ; children : React.ReactNode } ) = >
withKBar ? (
< KBarRoot >
{ children }
< KBarContent / >
< / KBarRoot >
) : (
< > { children } < / >
) ;
2023-01-16 13:59:54 +00:00
const PublicShell = ( props : LayoutProps ) = > {
2023-01-12 22:11:15 +00:00
const { status } = useSession ( ) ;
2023-01-16 13:59:54 +00:00
return (
< KBarWrapper withKBar = { status === "authenticated" } >
< CustomBrandingContainer / >
< Layout { ...props } / >
< / KBarWrapper >
) ;
} ;
export default function Shell ( props : LayoutProps ) {
2023-01-12 22:11:15 +00:00
// if a page is unauthed and isPublic is true, the redirect does not happen.
2022-08-09 09:21:15 +00:00
useRedirectToLoginIfUnauthenticated ( props . isPublic ) ;
useRedirectToOnboardingIfNeeded ( ) ;
useTheme ( "light" ) ;
2023-01-12 22:11:15 +00:00
2023-01-16 13:59:54 +00:00
return ! props . isPublic ? (
< KBarWrapper withKBar >
2022-08-09 09:21:15 +00:00
< CustomBrandingContainer / >
< Layout { ...props } / >
2023-01-12 22:11:15 +00:00
< / KBarWrapper >
2023-01-16 13:59:54 +00:00
) : (
< PublicShell { ...props } / >
2022-08-09 09:21:15 +00:00
) ;
}
function UserDropdown ( { small } : { small? : boolean } ) {
const { t } = useLocale ( ) ;
const query = useMeQuery ( ) ;
const user = query . data ;
useEffect ( ( ) = > {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const Beacon = window . Beacon ;
// window.Beacon is defined when user actually opens up HelpScout and username is available here. On every re-render update session info, so that it is always latest.
Beacon &&
Beacon ( "session-data" , {
username : user?.username || "Unknown" ,
screenResolution : ` ${ screen . width } x ${ screen . height } ` ,
} ) ;
} ) ;
2022-11-10 23:40:01 +00:00
const mutation = trpc . viewer . away . useMutation ( {
2022-08-09 09:21:15 +00:00
onSettled() {
2022-11-10 23:40:01 +00:00
utils . viewer . me . invalidate ( ) ;
2022-08-09 09:21:15 +00:00
} ,
} ) ;
const utils = trpc . useContext ( ) ;
const [ helpOpen , setHelpOpen ] = useState ( false ) ;
const [ menuOpen , setMenuOpen ] = useState ( false ) ;
if ( ! user ) {
return null ;
}
const onHelpItemSelect = ( ) = > {
setHelpOpen ( false ) ;
setMenuOpen ( false ) ;
} ;
// Prevent rendering dropdown if user isn't available.
// We don't want to show nameless user.
if ( ! user ) {
return null ;
}
return (
2022-10-17 19:05:00 +00:00
< Dropdown open = { menuOpen } >
2023-01-02 08:28:12 +00:00
< div className = "ltr:sm:-ml-5 rtl:sm:-mr-5" >
< DropdownMenuTrigger asChild onClick = { ( ) = > setMenuOpen ( ( menuOpen ) = > ! menuOpen ) } >
2023-02-03 16:49:33 +00:00
< button className = "radix-state-open:bg-gray-200 group mx-0 flex w-full cursor-pointer appearance-none items-center rounded-full p-2 text-left outline-none hover:bg-gray-200 focus:outline-none focus:ring-0 sm:mx-2.5 sm:pl-3 md:rounded-none lg:rounded lg:pl-2" >
2023-01-02 08:28:12 +00:00
< span
className = { classNames (
2023-01-16 12:57:38 +00:00
small ? "h-6 w-6 md:ml-3" : "h-8 w-8 ltr:mr-2 rtl:ml-2" ,
2023-01-02 08:28:12 +00:00
"relative flex-shrink-0 rounded-full bg-gray-300 "
) } >
{
// eslint-disable-next-line @next/next/no-img-element
< img
className = "rounded-full"
src = { WEBAPP_URL + "/" + user . username + "/avatar.png" }
alt = { user . username || "Nameless User" }
/ >
}
{ ! user . away && (
< div className = "absolute bottom-0 right-0 h-3 w-3 rounded-full border-2 border-white bg-green-500" / >
) }
{ user . away && (
< div className = "absolute bottom-0 right-0 h-3 w-3 rounded-full border-2 border-white bg-yellow-500" / >
) }
< / span >
{ ! small && (
< span className = "flex flex-grow items-center truncate" >
< span className = "flex-grow truncate text-sm" >
2023-02-03 16:49:33 +00:00
< span className = "mb-1 block truncate font-medium leading-none text-gray-900" >
2023-01-02 08:28:12 +00:00
{ user . name || "Nameless User" }
< / span >
2023-02-03 16:49:33 +00:00
< span className = "block truncate font-normal leading-none text-gray-600" >
2023-01-02 08:28:12 +00:00
{ user . username
? process . env . NEXT_PUBLIC_WEBSITE_URL === "https://cal.com"
? ` cal.com/ ${ user . username } `
: ` / ${ user . username } `
: "No public page" }
< / span >
2022-08-09 09:21:15 +00:00
< / span >
2023-01-23 23:08:01 +00:00
< FiMoreVertical
2023-01-04 07:38:45 +00:00
className = "h-4 w-4 flex-shrink-0 text-gray-400 group-hover:text-gray-500 ltr:mr-2 rtl:ml-2 rtl:mr-4"
2023-01-02 08:28:12 +00:00
aria - hidden = "true"
/ >
2022-08-09 09:21:15 +00:00
< / span >
2023-01-02 08:28:12 +00:00
) }
< / button >
< / DropdownMenuTrigger >
< / div >
2022-09-30 10:29:22 +00:00
< DropdownMenuPortal >
2023-03-08 11:30:24 +00:00
< FreshChatProvider >
< DropdownMenuContent
onInteractOutside = { ( ) = > {
setMenuOpen ( false ) ;
setHelpOpen ( false ) ;
} }
className = "overflow-hidden rounded-md" >
{ helpOpen ? (
< HelpMenuItem onHelpItemSelect = { ( ) = > onHelpItemSelect ( ) } / >
) : (
< >
< DropdownMenuItem >
< DropdownItem
type = "button"
StartIcon = { ( props ) = > (
< FiMoon
className = { classNames (
user . away
? "text-purple-500 group-hover:text-purple-700"
: "text-gray-500 group-hover:text-gray-700" ,
props . className
) }
aria - hidden = "true"
/ >
) }
onClick = { ( ) = > {
mutation . mutate ( { away : ! user ? . away } ) ;
utils . viewer . me . invalidate ( ) ;
} } >
{ user . away ? t ( "set_as_free" ) : t ( "set_as_away" ) }
< / DropdownItem >
< / DropdownMenuItem >
< DropdownMenuSeparator / >
{ user . username && (
< >
< DropdownMenuItem >
< DropdownItem
target = "_blank"
rel = "noopener noreferrer"
href = { ` ${ process . env . NEXT_PUBLIC_WEBSITE_URL } / ${ user . username } ` }
StartIcon = { FiExternalLink } >
{ t ( "view_public_page" ) }
< / DropdownItem >
< / DropdownMenuItem >
< DropdownMenuItem >
< DropdownItem
type = "button"
StartIcon = { FiLink }
onClick = { ( e ) = > {
e . preventDefault ( ) ;
navigator . clipboard . writeText (
` ${ process . env . NEXT_PUBLIC_WEBSITE_URL } / ${ user . username } `
) ;
showToast ( t ( "link_copied" ) , "success" ) ;
} } >
{ t ( "copy_public_page_link" ) }
< / DropdownItem >
< / DropdownMenuItem >
< / >
) }
< DropdownMenuSeparator / >
< DropdownMenuItem >
< DropdownItem
StartIcon = { ( props ) = > < FiSlack strokeWidth = { 1.5 } { ...props } / > }
target = "_blank"
rel = "noreferrer"
href = { JOIN_SLACK } >
{ t ( "join_our_slack" ) }
< / DropdownItem >
< / DropdownMenuItem >
< DropdownMenuItem >
< DropdownItem StartIcon = { FiMap } target = "_blank" href = { ROADMAP } >
{ t ( "visit_roadmap" ) }
< / DropdownItem >
< / DropdownMenuItem >
< DropdownMenuItem >
< DropdownItem
type = "button"
StartIcon = { ( props ) = > < FiHelpCircle aria - hidden = "true" { ...props } / > }
onClick = { ( ) = > setHelpOpen ( true ) } >
{ t ( "help" ) }
< / DropdownItem >
< / DropdownMenuItem >
< DropdownMenuItem className = "desktop-hidden hidden lg:flex" >
< DropdownItem
StartIcon = { FiDownload }
target = "_blank"
rel = "noreferrer"
href = { DESKTOP_APP_LINK } >
{ t ( "download_desktop_app" ) }
< / DropdownItem >
< / DropdownMenuItem >
2022-08-09 09:21:15 +00:00
2023-03-08 11:30:24 +00:00
< DropdownMenuSeparator / >
< DropdownMenuItem >
< DropdownItem
type = "button"
StartIcon = { ( props ) = > < FiLogOut aria - hidden = "true" { ...props } / > }
onClick = { ( ) = > signOut ( { callbackUrl : "/auth/logout" } ) } >
{ t ( "sign_out" ) }
< / DropdownItem >
< / DropdownMenuItem >
< / >
) }
< / DropdownMenuContent >
< / FreshChatProvider >
2022-09-30 10:29:22 +00:00
< / DropdownMenuPortal >
2022-08-09 09:21:15 +00:00
< / Dropdown >
) ;
}
2022-09-15 19:53:09 +00:00
export type NavigationItemType = {
2022-08-09 09:21:15 +00:00
name : string ;
href : string ;
2022-10-10 17:00:09 +00:00
badge? : React.ReactNode ;
2022-08-09 09:21:15 +00:00
icon? : SVGComponent ;
child? : NavigationItemType [ ] ;
pro? : true ;
2022-09-17 17:53:31 +00:00
onlyMobile? : boolean ;
onlyDesktop? : boolean ;
2022-09-02 19:00:41 +00:00
isCurrent ? : ( {
item ,
isChild ,
router ,
} : {
item : NavigationItemType ;
isChild? : boolean ;
router : NextRouter ;
} ) = > boolean ;
2022-08-09 09:21:15 +00:00
} ;
2022-09-02 19:00:41 +00:00
const requiredCredentialNavigationItems = [ "Routing Forms" ] ;
2022-09-07 04:13:31 +00:00
const MORE_SEPARATOR_NAME = "more" ;
2022-10-10 17:00:09 +00:00
2022-08-09 09:21:15 +00:00
const navigation : NavigationItemType [ ] = [
{
name : "event_types_page_title" ,
href : "/event-types" ,
2023-01-23 23:08:01 +00:00
icon : FiLink ,
2022-08-09 09:21:15 +00:00
} ,
{
name : "bookings" ,
href : "/bookings/upcoming" ,
2023-01-23 23:08:01 +00:00
icon : FiCalendar ,
2022-10-10 17:00:09 +00:00
badge : < UnconfirmedBookingBadge / > ,
2022-10-27 09:45:01 +00:00
isCurrent : ( { router } ) = > {
const path = router . asPath . split ( "?" ) [ 0 ] ;
return path . startsWith ( "/bookings" ) ;
} ,
2022-08-09 09:21:15 +00:00
} ,
{
name : "availability" ,
href : "/availability" ,
2023-01-23 23:08:01 +00:00
icon : FiClock ,
2022-08-09 09:21:15 +00:00
} ,
2022-09-17 17:53:31 +00:00
{
name : "teams" ,
href : "/teams" ,
2023-01-23 23:08:01 +00:00
icon : FiUsers ,
2022-09-17 17:53:31 +00:00
onlyDesktop : true ,
2023-01-24 15:27:05 +00:00
badge : < TeamInviteBadge / > ,
2022-09-17 17:53:31 +00:00
} ,
2022-08-09 09:21:15 +00:00
{
name : "apps" ,
href : "/apps" ,
2023-01-23 23:08:01 +00:00
icon : FiGrid ,
2022-09-02 19:00:41 +00:00
isCurrent : ( { router , item } ) = > {
const path = router . asPath . split ( "?" ) [ 0 ] ;
2022-09-15 19:53:09 +00:00
// During Server rendering path is /v2/apps but on client it becomes /apps(weird..)
return (
2022-09-22 17:23:43 +00:00
( path . startsWith ( item . href ) || path . startsWith ( "/v2" + item . href ) ) && ! path . includes ( "routing-forms/" )
2022-09-15 19:53:09 +00:00
) ;
2022-09-02 19:00:41 +00:00
} ,
2022-08-09 09:21:15 +00:00
child : [
{
name : "app_store" ,
href : "/apps" ,
2022-09-15 19:53:09 +00:00
isCurrent : ( { router , item } ) = > {
const path = router . asPath . split ( "?" ) [ 0 ] ;
// During Server rendering path is /v2/apps but on client it becomes /apps(weird..)
return (
( path . startsWith ( item . href ) || path . startsWith ( "/v2" + item . href ) ) &&
2022-09-22 17:23:43 +00:00
! path . includes ( "routing-forms/" ) &&
2022-09-15 19:53:09 +00:00
! path . includes ( "/installed" )
) ;
} ,
2022-08-09 09:21:15 +00:00
} ,
{
name : "installed_apps" ,
2022-09-15 19:53:09 +00:00
href : "/apps/installed/calendar" ,
isCurrent : ( { router } ) = > {
const path = router . asPath ;
return path . startsWith ( "/apps/installed/" ) || path . startsWith ( "/v2/apps/installed/" ) ;
} ,
2022-08-09 09:21:15 +00:00
} ,
] ,
} ,
2022-09-07 04:13:31 +00:00
{
name : MORE_SEPARATOR_NAME ,
href : "/more" ,
2023-01-23 23:08:01 +00:00
icon : FiMoreHorizontal ,
2022-09-07 04:13:31 +00:00
} ,
{
name : "Routing Forms" ,
2022-09-22 17:23:43 +00:00
href : "/apps/routing-forms/forms" ,
2023-01-23 23:08:01 +00:00
icon : FiFileText ,
2022-09-07 04:13:31 +00:00
isCurrent : ( { router } ) = > {
2022-09-22 17:23:43 +00:00
return router . asPath . startsWith ( "/apps/routing-forms/" ) ;
2022-09-07 04:13:31 +00:00
} ,
} ,
{
name : "workflows" ,
href : "/workflows" ,
2023-01-23 23:08:01 +00:00
icon : FiZap ,
2022-09-07 04:13:31 +00:00
} ,
2022-08-09 09:21:15 +00:00
{
name : "settings" ,
2022-11-04 20:30:12 +00:00
href : "/settings/my-account/profile" ,
2023-01-23 23:08:01 +00:00
icon : FiSettings ,
2022-08-09 09:21:15 +00:00
} ,
] ;
2022-09-07 04:13:31 +00:00
const moreSeparatorIndex = navigation . findIndex ( ( item ) = > item . name === MORE_SEPARATOR_NAME ) ;
// We create all needed navigation items for the different use cases
const { desktopNavigationItems , mobileNavigationBottomItems , mobileNavigationMoreItems } = navigation . reduce <
Record < string , NavigationItemType [ ] >
> (
( items , item , index ) = > {
2022-12-19 17:37:20 +00:00
// We filter out the "more" separator in` desktop navigation
2022-09-07 04:13:31 +00:00
if ( item . name !== MORE_SEPARATOR_NAME ) items . desktopNavigationItems . push ( item ) ;
// Items for mobile bottom navigation
2022-09-17 17:53:31 +00:00
if ( index < moreSeparatorIndex + 1 && ! item . onlyDesktop ) items . mobileNavigationBottomItems . push ( item ) ;
2022-09-07 04:13:31 +00:00
// Items for the "more" menu in mobile navigation
else items . mobileNavigationMoreItems . push ( item ) ;
return items ;
} ,
{ desktopNavigationItems : [ ] , mobileNavigationBottomItems : [ ] , mobileNavigationMoreItems : [ ] }
) ;
2022-08-09 09:21:15 +00:00
const Navigation = ( ) = > {
return (
2023-01-12 16:44:41 +00:00
< nav className = "mt-2 flex-1 md:px-2 lg:mt-6 lg:px-0" >
2022-09-07 04:13:31 +00:00
{ desktopNavigationItems . map ( ( item ) = > (
2022-08-09 09:21:15 +00:00
< NavigationItem key = { item . name } item = { item } / >
) ) }
2023-01-12 16:44:41 +00:00
< div className = "mt-0.5 text-gray-500 lg:hidden" >
2022-08-09 09:21:15 +00:00
< KBarTrigger / >
2022-09-05 10:02:21 +00:00
< / div >
2022-08-09 09:21:15 +00:00
< / nav >
) ;
} ;
function useShouldDisplayNavigationItem ( item : NavigationItemType ) {
const { status } = useSession ( ) ;
2022-11-10 23:40:01 +00:00
const { data : routingForms } = trpc . viewer . appById . useQuery (
{ appId : "routing-forms" } ,
{
enabled : status === "authenticated" && requiredCredentialNavigationItems . includes ( item . name ) ,
trpc : { } ,
}
) ;
2022-10-14 16:24:43 +00:00
return ! requiredCredentialNavigationItems . includes ( item . name ) || routingForms ? . isInstalled ;
2022-08-09 09:21:15 +00:00
}
2022-09-02 19:00:41 +00:00
const defaultIsCurrent : NavigationItemType [ "isCurrent" ] = ( { isChild , item , router } ) = > {
return isChild ? item . href === router.asPath : router.asPath.startsWith ( item . href ) ;
} ;
2022-08-09 09:21:15 +00:00
const NavigationItem : React.FC < {
2023-01-12 16:44:41 +00:00
index? : number ;
2022-08-09 09:21:15 +00:00
item : NavigationItemType ;
isChild? : boolean ;
} > = ( props ) = > {
const { item , isChild } = props ;
2022-09-05 19:06:34 +00:00
const { t , isLocaleReady } = useLocale ( ) ;
2022-08-09 09:21:15 +00:00
const router = useRouter ( ) ;
2022-09-02 19:00:41 +00:00
const isCurrent : NavigationItemType [ "isCurrent" ] = item . isCurrent || defaultIsCurrent ;
const current = isCurrent ( { isChild : ! ! isChild , item , router } ) ;
2022-08-09 09:21:15 +00:00
const shouldDisplayNavigationItem = useShouldDisplayNavigationItem ( props . item ) ;
2022-08-24 20:18:42 +00:00
2022-08-09 09:21:15 +00:00
if ( ! shouldDisplayNavigationItem ) return null ;
2022-08-24 20:18:42 +00:00
2022-08-09 09:21:15 +00:00
return (
< Fragment >
2023-01-06 12:13:56 +00:00
< Link
href = { item . href }
aria - label = { t ( item . name ) }
className = { classNames (
2023-01-12 16:57:43 +00:00
"group flex items-center rounded-md py-2 px-3 text-sm font-medium text-gray-600 hover:bg-gray-100 [&[aria-current='page']]:bg-gray-200 [&[aria-current='page']]:hover:text-gray-900" ,
2023-01-06 12:13:56 +00:00
isChild
2023-01-12 16:44:41 +00:00
? ` [&[aria-current='page']]:text-brand-900 hidden h-8 pl-16 lg:flex lg:pl-11 [&[aria-current='page']]:bg-transparent ${
props . index === 0 ? "mt-0" : "mt-px"
} `
: "[&[aria-current='page']]:text-brand-900 mt-0.5 text-sm"
2023-01-06 12:13:56 +00:00
) }
aria - current = { current ? "page" : undefined } >
{ item . icon && (
< item.icon
2023-01-10 12:44:30 +00:00
className = "h-4 w-4 flex-shrink-0 text-gray-500 ltr:mr-2 rtl:ml-2 [&[aria-current='page']]:text-inherit"
2023-01-06 12:13:56 +00:00
aria - hidden = "true"
aria - current = { current ? "page" : undefined }
/ >
) }
{ isLocaleReady ? (
< span className = "hidden w-full justify-between lg:flex" >
< div className = "flex" > { t ( item . name ) } < / div >
{ item . badge && item . badge }
< / span >
) : (
< SkeletonText className = "h-3 w-32" / >
) }
2022-08-09 09:21:15 +00:00
< / Link >
{ item . child &&
2022-09-02 19:00:41 +00:00
isCurrent ( { router , isChild , item } ) &&
2023-01-12 16:44:41 +00:00
item . child . map ( ( item , index ) = > < NavigationItem index = { index } key = { item . name } item = { item } isChild / > ) }
2022-08-09 09:21:15 +00:00
< / Fragment >
) ;
} ;
function MobileNavigationContainer() {
const { status } = useSession ( ) ;
if ( status !== "authenticated" ) return null ;
return < MobileNavigation / > ;
}
const MobileNavigation = ( ) = > {
const isEmbed = useIsEmbed ( ) ;
2022-09-21 15:39:54 +00:00
2022-08-09 09:21:15 +00:00
return (
< >
< nav
className = { classNames (
2023-02-20 14:11:51 +00:00
"pwa:pb-2.5 fixed bottom-0 z-30 -mx-4 flex w-full border border-t border-gray-200 bg-gray-50 bg-opacity-40 px-1 shadow backdrop-blur-md md:hidden" ,
2022-08-09 09:21:15 +00:00
isEmbed && "hidden"
) } >
2022-09-07 04:13:31 +00:00
{ mobileNavigationBottomItems . map ( ( item ) = > (
< MobileNavigationItem key = { item . name } item = { item } / >
) ) }
2022-08-09 09:21:15 +00:00
< / nav >
{ /* add padding to content for mobile navigation*/ }
< div className = "block pt-12 md:hidden" / >
< / >
) ;
} ;
const MobileNavigationItem : React.FC < {
item : NavigationItemType ;
isChild? : boolean ;
} > = ( props ) = > {
2022-09-07 04:13:31 +00:00
const { item , isChild } = props ;
2022-08-09 09:21:15 +00:00
const router = useRouter ( ) ;
2022-09-05 19:06:34 +00:00
const { t , isLocaleReady } = useLocale ( ) ;
2022-09-02 19:00:41 +00:00
const isCurrent : NavigationItemType [ "isCurrent" ] = item . isCurrent || defaultIsCurrent ;
const current = isCurrent ( { isChild : ! ! isChild , item , router } ) ;
2022-08-09 09:21:15 +00:00
const shouldDisplayNavigationItem = useShouldDisplayNavigationItem ( props . item ) ;
2022-09-05 19:06:34 +00:00
2022-08-09 09:21:15 +00:00
if ( ! shouldDisplayNavigationItem ) return null ;
return (
2023-01-06 12:13:56 +00:00
< Link
key = { item . name }
href = { item . href }
2023-02-20 14:11:51 +00:00
className = "relative my-2 min-w-0 flex-1 overflow-hidden rounded-md !bg-transparent p-1 text-center text-xs font-medium text-gray-400 hover:text-gray-700 focus:z-10 sm:text-sm [&[aria-current='page']]:text-gray-900"
2023-01-06 12:13:56 +00:00
aria - current = { current ? "page" : undefined } >
{ item . badge && < div className = "absolute right-1 top-1" > { item . badge } < / div > }
{ item . icon && (
< item.icon
className = "mx-auto mb-1 block h-5 w-5 flex-shrink-0 text-center text-inherit [&[aria-current='page']]:text-gray-900"
aria - hidden = "true"
aria - current = { current ? "page" : undefined }
/ >
) }
{ isLocaleReady ? < span className = "block truncate" > { t ( item . name ) } < / span > : < SkeletonText / > }
2022-08-09 09:21:15 +00:00
< / Link >
) ;
} ;
2022-09-07 04:13:31 +00:00
const MobileNavigationMoreItem : React.FC < {
item : NavigationItemType ;
isChild? : boolean ;
} > = ( props ) = > {
const { item } = props ;
const { t , isLocaleReady } = useLocale ( ) ;
const shouldDisplayNavigationItem = useShouldDisplayNavigationItem ( props . item ) ;
if ( ! shouldDisplayNavigationItem ) return null ;
return (
< li className = "border-b last:border-b-0" key = { item . name } >
2023-01-06 12:13:56 +00:00
< Link href = { item . href } className = "flex items-center justify-between p-5 hover:bg-gray-100" >
< span className = "flex items-center font-semibold text-gray-700 " >
{ item . icon && < item.icon className = "h-5 w-5 flex-shrink-0 ltr:mr-3 rtl:ml-3" aria - hidden = "true" / > }
{ isLocaleReady ? t ( item . name ) : < SkeletonText / > }
< / span >
2023-01-23 23:08:01 +00:00
< FiArrowRight className = "h-5 w-5 text-gray-500" / >
2022-09-07 04:13:31 +00:00
< / Link >
< / li >
) ;
} ;
2022-08-09 09:21:15 +00:00
function SideBarContainer() {
const { status } = useSession ( ) ;
const router = useRouter ( ) ;
2023-01-02 19:44:51 +00:00
2022-09-15 19:53:09 +00:00
// Make sure that Sidebar is rendered optimistically so that a refresh of pages when logged in have SideBar from the beginning.
// This improves the experience of refresh on app store pages(when logged in) which are SSG.
// Though when logged out, app store pages would temporarily show SideBar until session status is confirmed.
if ( status !== "loading" && status !== "authenticated" ) return null ;
2022-08-09 09:21:15 +00:00
if ( router . route . startsWith ( "/v2/settings/" ) ) return null ;
return < SideBar / > ;
}
function SideBar() {
return (
2022-12-19 17:37:20 +00:00
< div className = "relative" >
2023-01-16 12:57:38 +00:00
< aside className = "desktop-transparent top-0 hidden h-full max-h-screen w-14 flex-col overflow-y-auto overflow-x-hidden border-r border-gray-100 bg-gray-50 md:sticky md:flex lg:w-56 lg:px-4" >
2023-01-10 12:44:30 +00:00
< div className = "flex h-full flex-col justify-between py-3 lg:pt-6 " >
2022-12-19 17:37:20 +00:00
< header className = "items-center justify-between md:hidden lg:flex" >
2023-01-10 12:44:30 +00:00
< Link href = "/event-types" className = "px-2" >
2023-01-06 12:13:56 +00:00
< Logo small / >
2022-12-19 17:37:20 +00:00
< / Link >
2023-01-04 07:38:45 +00:00
< div className = "flex space-x-2 rtl:space-x-reverse" >
2022-12-19 17:37:20 +00:00
< button
color = "minimal"
onClick = { ( ) = > window . history . back ( ) }
2023-01-12 16:57:43 +00:00
className = "desktop-only group flex text-sm font-medium text-gray-500 hover:text-gray-900" >
2023-01-23 23:08:01 +00:00
< FiArrowLeft className = "h-4 w-4 flex-shrink-0 text-gray-500 group-hover:text-gray-900" / >
2022-12-19 17:37:20 +00:00
< / button >
< button
color = "minimal"
onClick = { ( ) = > window . history . forward ( ) }
2023-01-12 16:57:43 +00:00
className = "desktop-only group flex text-sm font-medium text-gray-500 hover:text-gray-900" >
2023-01-23 23:08:01 +00:00
< FiArrowRight className = "h-4 w-4 flex-shrink-0 text-gray-500 group-hover:text-gray-900" / >
2022-12-19 17:37:20 +00:00
< / button >
< KBarTrigger / >
< / div >
< / header >
< hr className = "desktop-only absolute -left-3 -right-3 mt-4 block w-full border-gray-200" / >
{ /* logo icon for tablet */ }
2023-01-06 12:13:56 +00:00
< Link href = "/event-types" className = "text-center md:inline lg:hidden" >
< Logo small icon / >
2022-08-09 09:21:15 +00:00
< / Link >
2022-09-07 15:01:33 +00:00
2022-12-19 17:37:20 +00:00
< Navigation / >
< / div >
2022-09-07 15:01:33 +00:00
2023-01-02 10:13:27 +00:00
< div >
2023-01-15 17:54:52 +00:00
< Tips / >
2022-12-19 17:37:20 +00:00
< div data - testid = "user-dropdown-trigger" >
< span className = "hidden lg:inline" >
< UserDropdown / >
< / span >
< span className = "hidden md:inline lg:hidden" >
< UserDropdown small / >
< / span >
< / div >
2023-01-02 19:44:51 +00:00
< Credits / >
2022-12-19 17:37:20 +00:00
< / div >
< / aside >
< / div >
2022-08-09 09:21:15 +00:00
) ;
}
2022-09-02 19:00:41 +00:00
export function ShellMain ( props : LayoutProps ) {
const router = useRouter ( ) ;
2022-09-05 19:06:34 +00:00
const { isLocaleReady } = useLocale ( ) ;
2023-02-02 08:24:31 +00:00
2022-08-09 09:21:15 +00:00
return (
2022-09-02 19:00:41 +00:00
< >
2023-02-24 00:19:52 +00:00
< div
className = { classNames (
2023-02-24 14:21:31 +00:00
"flex items-center md:mt-0 md:mb-6" ,
2023-02-24 00:19:52 +00:00
props . smallHeading ? "lg:mb-7" : "lg:mb-8"
) } >
2022-09-02 19:00:41 +00:00
{ ! ! props . backPath && (
2022-09-28 17:46:14 +00:00
< Button
2023-01-19 14:55:32 +00:00
variant = "icon"
2023-01-31 20:19:02 +00:00
size = "sm"
2022-09-28 17:46:14 +00:00
color = "minimal"
2022-10-25 00:29:49 +00:00
onClick = { ( ) = >
typeof props . backPath === "string" ? router . push ( props . backPath as string ) : router . back ( )
}
2023-01-23 23:08:01 +00:00
StartIcon = { FiArrowLeft }
2022-09-28 17:46:14 +00:00
aria - label = "Go Back"
2023-02-24 00:19:52 +00:00
className = "rounded-md ltr:mr-2 rtl:ml-2"
2022-09-02 19:00:41 +00:00
/ >
) }
2022-08-24 20:18:42 +00:00
{ props . heading && (
2023-01-31 20:19:02 +00:00
< header className = { classNames ( props . large && "py-8" , "flex w-full max-w-full items-center" ) } >
2022-08-24 20:18:42 +00:00
{ props . HeadingLeftIcon && < div className = "ltr:mr-4" > { props . HeadingLeftIcon } < / div > }
2023-02-02 08:24:31 +00:00
< div className = { classNames ( "w-full ltr:mr-4 rtl:ml-4 md:block" , props . headerClassName ) } >
2022-09-05 19:06:34 +00:00
{ props . heading && (
2023-02-13 12:29:48 +00:00
< h3
2023-01-29 09:38:31 +00:00
className = { classNames (
2023-02-03 16:49:33 +00:00
"font-cal max-w-28 sm:max-w-72 md:max-w-80 hidden truncate text-xl font-semibold tracking-wide text-black md:block xl:max-w-full" ,
2023-01-31 20:19:02 +00:00
props . smallHeading ? "text-base" : "text-xl"
2023-01-29 09:38:31 +00:00
) } >
2022-09-15 19:53:09 +00:00
{ ! isLocaleReady ? < SkeletonText invisible / > : props . heading }
2023-02-13 12:29:48 +00:00
< / h3 >
2022-09-05 19:06:34 +00:00
) }
2023-01-31 22:33:20 +00:00
{ props . subtitle && (
2023-02-02 08:24:31 +00:00
< p className = "hidden text-sm text-gray-500 md:block" >
2023-01-31 22:31:04 +00:00
{ ! isLocaleReady ? < SkeletonText invisible / > : props . subtitle }
< / p >
) }
2022-08-24 20:18:42 +00:00
< / div >
2022-09-09 15:02:31 +00:00
{ props . CTA && (
2022-09-28 17:56:59 +00:00
< div
className = { classNames (
2023-01-15 17:54:52 +00:00
props . backPath
? "relative"
2023-02-20 14:11:51 +00:00
: "pwa:bottom-24 fixed bottom-20 z-40 ltr:right-4 rtl:left-4 md:z-auto md:ltr:right-0 md:rtl:left-0" ,
2023-02-02 08:24:31 +00:00
"flex-shrink-0 md:relative md:bottom-auto md:right-auto"
2022-09-28 17:56:59 +00:00
) } >
2022-09-09 15:02:31 +00:00
{ props . CTA }
< / div >
) }
2022-11-30 20:51:44 +00:00
{ props . actions && props . actions }
2022-09-07 15:01:33 +00:00
< / header >
2022-08-24 20:18:42 +00:00
) }
2022-09-02 19:00:41 +00:00
< / div >
2023-01-15 17:54:52 +00:00
< div className = { classNames ( props . flexChildrenContainer && "flex flex-1 flex-col" ) } >
2022-09-02 19:00:41 +00:00
{ props . children }
< / div >
< / >
) ;
}
2022-09-07 01:33:50 +00:00
function MainContainer ( {
MobileNavigationContainer : MobileNavigationContainerProp = < MobileNavigationContainer / > ,
TopNavContainer : TopNavContainerProp = < TopNavContainer / > ,
. . . props
} : LayoutProps ) {
2022-09-02 19:00:41 +00:00
return (
2022-12-19 17:37:20 +00:00
< main className = "relative z-0 flex-1 bg-white focus:outline-none" >
2022-09-02 19:00:41 +00:00
{ /* show top navigation for md and smaller (tablet and phones) */ }
2022-09-07 01:33:50 +00:00
{ TopNavContainerProp }
2023-02-23 22:09:10 +00:00
< div className = "max-w-full py-4 px-4 md:py-8 lg:px-12" >
2022-09-06 17:05:16 +00:00
< ErrorBoundary >
{ ! props . withoutMain ? < ShellMain { ...props } > { props . children } < / ShellMain > : props . children }
< / ErrorBoundary >
2022-09-24 11:55:50 +00:00
{ /* show bottom navigation for md and smaller (tablet and phones) on pages where back button doesn't exist */ }
{ ! props . backPath ? MobileNavigationContainerProp : null }
2022-09-06 17:05:16 +00:00
< / div >
2022-08-09 09:21:15 +00:00
< / main >
) ;
}
function TopNavContainer() {
const { status } = useSession ( ) ;
if ( status !== "authenticated" ) return null ;
return < TopNav / > ;
}
function TopNav() {
const isEmbed = useIsEmbed ( ) ;
const { t } = useLocale ( ) ;
return (
2022-09-09 15:02:31 +00:00
< >
< nav
style = { isEmbed ? { display : "none" } : { } }
2022-12-19 17:37:20 +00:00
className = "sticky top-0 z-40 flex w-full items-center justify-between border-b border-gray-200 bg-gray-50 bg-opacity-50 py-1.5 px-4 backdrop-blur-lg sm:p-4 md:hidden" >
2022-09-09 15:02:31 +00:00
< Link href = "/event-types" >
2023-01-06 12:13:56 +00:00
< Logo / >
2022-09-09 15:02:31 +00:00
< / Link >
< div className = "flex items-center gap-2 self-center" >
2023-01-12 16:57:43 +00:00
< span className = "group flex items-center rounded-full text-sm font-medium text-gray-700 hover:bg-gray-50 hover:text-gray-900 lg:hidden" >
2022-09-09 15:02:31 +00:00
< KBarTrigger / >
< / span >
< button className = "rounded-full p-1 text-gray-400 hover:bg-gray-50 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-black focus:ring-offset-2" >
< span className = "sr-only" > { t ( "settings" ) } < / span >
2023-02-24 18:39:09 +00:00
< Link href = "/settings/my-account/profile" >
2023-01-23 23:08:01 +00:00
< FiSettings className = "h-4 w-4 text-gray-700" aria - hidden = "true" / >
2022-09-09 15:02:31 +00:00
< / Link >
< / button >
< UserDropdown small / >
< / div >
< / nav >
< / >
2022-08-09 09:21:15 +00:00
) ;
}
2022-09-07 04:13:31 +00:00
export const MobileNavigationMoreItems = ( ) = > (
< ul className = "mt-2 rounded-md border" >
{ mobileNavigationMoreItems . map ( ( item ) = > (
< MobileNavigationMoreItem key = { item . name } item = { item } / >
) ) }
< / ul >
) ;