'use client'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import { CheckCircle2, ChevronsUpDown, Plus, Settings2 } from 'lucide-react'; import { signOut } from 'next-auth/react'; import { TEAM_MEMBER_ROLE_MAP, TEAM_URL_REGEX } from '@documenso/lib/constants/teams'; import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin'; import type { GetTeamsResponse } from '@documenso/lib/server-only/team/get-teams'; import { extractInitials } from '@documenso/lib/utils/recipient-formatter'; import { canExecuteTeamAction } from '@documenso/lib/utils/teams'; import type { User } from '@documenso/prisma/client'; import { trpc } from '@documenso/trpc/react'; import { cn } from '@documenso/ui/lib/utils'; import { AvatarWithText } from '@documenso/ui/primitives/avatar'; import { Button } from '@documenso/ui/primitives/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from '@documenso/ui/primitives/dropdown-menu'; export type MenuSwitcherProps = { user: User; teams: GetTeamsResponse; }; export const MenuSwitcher = ({ user, teams: initialTeamsData }: MenuSwitcherProps) => { const pathname = usePathname(); const isUserAdmin = isAdmin(user); const { data: teamsQueryResult } = trpc.team.getTeams.useQuery(undefined, { initialData: initialTeamsData, }); const teams = teamsQueryResult && teamsQueryResult.length > 0 ? teamsQueryResult : null; const isPathTeamUrl = (teamUrl: string) => { if (!pathname || !pathname.startsWith(`/t/`)) { return false; } return pathname.split('/')[2] === teamUrl; }; const selectedTeam = teams?.find((team) => isPathTeamUrl(team.url)); const formatAvatarFallback = (teamName?: string) => { if (teamName !== undefined) { return teamName.slice(0, 1).toUpperCase(); } return user.name ? extractInitials(user.name) : user.email.slice(0, 1).toUpperCase(); }; const formatSecondaryAvatarText = (team?: typeof selectedTeam) => { if (!team) { return 'Personal Account'; } if (team.ownerUserId === user.id) { return 'Owner'; } return TEAM_MEMBER_ROLE_MAP[team.currentTeamMember.role]; }; /** * Formats the redirect URL so we can switch between documents and templates page * seemlessly between teams and personal accounts. */ const formatRedirectUrlOnSwitch = (teamUrl?: string) => { const baseUrl = teamUrl ? `/t/${teamUrl}/` : '/'; const currentPathname = (pathname ?? '/').replace(TEAM_URL_REGEX, ''); if (currentPathname === '/templates') { return `${baseUrl}templates`; } return baseUrl; }; return ( {teams ? ( <> Personal ) } />

Teams

{teams.map((team) => ( ) } /> ))}
) : ( Create team )} {isUserAdmin && ( Admin panel )} User settings {selectedTeam && canExecuteTeamAction('MANAGE_TEAM', selectedTeam.currentTeamMember.role) && ( Team settings )} signOut({ callbackUrl: '/', }) } > Sign Out
); };