import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@radix-ui/react-collapsible"; import { useSession } from "next-auth/react"; import Link from "next/link"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import type { ComponentProps } from "react"; import React, { Suspense, useEffect, useState } from "react"; import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider"; import Shell from "@calcom/features/shell/Shell"; import { classNames } from "@calcom/lib"; import { HOSTED_CAL_FEATURES, WEBAPP_URL } from "@calcom/lib/constants"; import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { IdentityProvider, MembershipRole, UserPermissionRole } from "@calcom/prisma/enums"; import { trpc } from "@calcom/trpc/react"; import type { VerticalTabItemProps } from "@calcom/ui"; import { Badge, Button, ErrorBoundary, Skeleton, useMeta, VerticalTabItem } from "@calcom/ui"; import { ArrowLeft, ChevronDown, ChevronRight, CreditCard, Key, Loader, Lock, Menu, Plus, Terminal, User, Users, } from "@calcom/ui/components/icon"; const tabs: VerticalTabItemProps[] = [ { name: "my_account", href: "/settings/my-account", icon: User, children: [ { name: "profile", href: "/settings/my-account/profile" }, { name: "general", href: "/settings/my-account/general" }, { name: "calendars", href: "/settings/my-account/calendars" }, { name: "conferencing", href: "/settings/my-account/conferencing" }, { name: "appearance", href: "/settings/my-account/appearance" }, // TODO // { name: "referrals", href: "/settings/my-account/referrals" }, ], }, { name: "security", href: "/settings/security", icon: Key, children: [ { name: "password", href: "/settings/security/password" }, { name: "impersonation", href: "/settings/security/impersonation" }, { name: "2fa_auth", href: "/settings/security/two-factor-auth" }, ], }, { name: "billing", href: "/settings/billing", icon: CreditCard, children: [{ name: "manage_billing", href: "/settings/billing" }], }, { name: "developer", href: "/settings/developer", icon: Terminal, children: [ // { name: "webhooks", href: "/settings/developer/webhooks" }, { name: "api_keys", href: "/settings/developer/api-keys" }, // TODO: Add profile level for embeds // { name: "embeds", href: "/v2/settings/developer/embeds" }, ], }, { name: "organization", href: "/settings/organizations", children: [ { name: "profile", href: "/settings/organizations/profile", }, { name: "general", href: "/settings/organizations/general", }, { name: "members", href: "/settings/organizations/members", }, { name: "appearance", href: "/settings/organizations/appearance", }, { name: "billing", href: "/settings/organizations/billing", }, ], }, { name: "teams", href: "/settings/teams", icon: Users, children: [], }, { name: "admin", href: "/settings/admin", icon: Lock, children: [ // { name: "features", href: "/settings/admin/flags" }, { name: "license", href: "/auth/setup?step=1" }, { name: "impersonation", href: "/settings/admin/impersonation" }, { name: "apps", href: "/settings/admin/apps/calendar" }, { name: "users", href: "/settings/admin/users" }, { name: "organizations", href: "/settings/admin/organizations" }, { name: "oAuth", href: "/settings/admin/oAuth" }, ], }, ]; tabs.find((tab) => { // Add "SAML SSO" to the tab if (tab.name === "security" && !HOSTED_CAL_FEATURES) { tab.children?.push({ name: "sso_configuration", href: "/settings/security/sso" }); } }); // The following keys are assigned to admin only const adminRequiredKeys = ["admin"]; const organizationRequiredKeys = ["organization"]; const useTabs = () => { const session = useSession(); const { data: user } = trpc.viewer.me.useQuery(); const orgBranding = useOrgBranding(); const isAdmin = session.data?.user.role === UserPermissionRole.ADMIN; tabs.map((tab) => { if (tab.href === "/settings/my-account") { tab.name = user?.name || "my_account"; tab.icon = undefined; tab.avatar = `${orgBranding?.fullDomain ?? WEBAPP_URL}/${session?.data?.user?.username}/avatar.png`; } else if (tab.href === "/settings/organizations") { tab.name = orgBranding?.name || "organization"; tab.avatar = `${orgBranding?.fullDomain}/org/${orgBranding?.slug}/avatar.png`; } else if ( tab.href === "/settings/security" && user?.identityProvider === IdentityProvider.GOOGLE && !user?.twoFactorEnabled ) { tab.children = tab?.children?.filter( (childTab) => childTab.href !== "/settings/security/two-factor-auth" ); } return tab; }); // check if name is in adminRequiredKeys return tabs.filter((tab) => { if (organizationRequiredKeys.includes(tab.name)) return !!session.data?.user?.org; if (isAdmin) return true; return !adminRequiredKeys.includes(tab.name); }); }; const BackButtonInSidebar = ({ name }: { name: string }) => { return ( {name} ); }; interface SettingsSidebarContainerProps { className?: string; navigationIsOpenedOnMobile?: boolean; bannersHeight?: number; } const SettingsSidebarContainer = ({ className = "", navigationIsOpenedOnMobile, bannersHeight, }: SettingsSidebarContainerProps) => { const searchParams = useSearchParams(); const { t } = useLocale(); const tabsWithPermissions = useTabs(); const [teamMenuState, setTeamMenuState] = useState<{ teamId: number | undefined; teamMenuOpen: boolean }[]>(); const [otherTeamMenuState, setOtherTeamMenuState] = useState< { teamId: number | undefined; teamMenuOpen: boolean; }[] >(); const { data: teams } = trpc.viewer.teams.list.useQuery(); const session = useSession(); const { data: currentOrg } = trpc.viewer.organizations.listCurrent.useQuery(undefined, { enabled: !!session.data?.user?.org, }); const { data: otherTeams } = trpc.viewer.organizations.listOtherTeams.useQuery(); useEffect(() => { if (teams) { const teamStates = teams?.map((team) => ({ teamId: team.id, teamMenuOpen: String(team.id) === searchParams?.get("id"), })); setTeamMenuState(teamStates); setTimeout(() => { const tabMembers = Array.from(document.getElementsByTagName("a")).filter( (bottom) => bottom.dataset.testid === "vertical-tab-Members" )[1]; tabMembers?.scrollIntoView({ behavior: "smooth" }); }, 100); } }, [searchParams?.get("id"), teams]); // Same as above but for otherTeams useEffect(() => { if (otherTeams) { const otherTeamStates = otherTeams?.map((team) => ({ teamId: team.id, teamMenuOpen: String(team.id) === searchParams?.get("id"), })); setOtherTeamMenuState(otherTeamStates); setTimeout(() => { // @TODO: test if this works for 2 dataset testids const tabMembers = Array.from(document.getElementsByTagName("a")).filter( (bottom) => bottom.dataset.testid === "vertical-tab-Members" )[1]; tabMembers?.scrollIntoView({ behavior: "smooth" }); }, 100); } }, [searchParams?.get("id"), otherTeams]); const isOrgAdminOrOwner = currentOrg && currentOrg?.user?.role && ["OWNER", "ADMIN"].includes(currentOrg?.user?.role); if (isOrgAdminOrOwner) { const teamsIndex = tabsWithPermissions.findIndex((tab) => tab.name === "teams"); tabsWithPermissions.splice(teamsIndex + 1, 0, { name: "other_teams", href: "/settings/organizations/teams/other", icon: Users, children: [], }); } return ( ); }; const MobileSettingsContainer = (props: { onSideContainerOpen?: () => void }) => { const { t } = useLocale(); const router = useRouter(); return ( <> ); }; export default function SettingsLayout({ children, ...rest }: { children: React.ReactNode } & ComponentProps) { const pathname = usePathname(); const state = useState(false); const { t } = useLocale(); const [sideContainerOpen, setSideContainerOpen] = state; useEffect(() => { const closeSideContainer = () => { if (window.innerWidth >= 1024) { setSideContainerOpen(false); } }; window.addEventListener("resize", closeSideContainer); return () => { window.removeEventListener("resize", closeSideContainer); }; }, []); useEffect(() => { if (sideContainerOpen) { setSideContainerOpen(!sideContainerOpen); } }, [pathname]); return ( } drawerState={state} MobileNavigationContainer={null} TopNavContainer={ setSideContainerOpen(!sideContainerOpen)} /> }>
}>{children}
); } const SidebarContainerElement = ({ sideContainerOpen, bannersHeight, setSideContainerOpen, }: SidebarContainerElementProps) => { const { t } = useLocale(); return ( <> {/* Mobile backdrop */} {sideContainerOpen && ( )} ); }; type SidebarContainerElementProps = { sideContainerOpen: boolean; bannersHeight?: number; setSideContainerOpen: React.Dispatch>; }; export const getLayout = (page: React.ReactElement) => {page}; export function ShellHeader() { const { meta } = useMeta(); const { t, isLocaleReady } = useLocale(); return ( <>
{meta.backButton && ( )}
{meta.title && isLocaleReady ? (

{t(meta.title)}

) : (
)} {meta.description && isLocaleReady ? (

{t(meta.description)}

) : (
)}
{meta.CTA}
); }