import { Box, Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger, Icon, IconButton, ProgressCircle, Text, Tooltip, useMediaQuery, } from "@prismicio/editor-ui"; import * as VisuallyHidden from "@radix-ui/react-visually-hidden"; import type { Environment } from "@slicemachine/manager/client"; import { clsx } from "clsx"; import type { FC, ReactNode } from "react"; import { EnvironmentLogoutButton } from "@/features/auth/LogoutButton"; import { LoginIcon } from "@/icons/LoginIcon"; import { LogoIcon } from "@/icons/LogoIcon"; import styles from "./SideNavEnvironmentSelector.module.css"; type SideNavEnvironmentSelectorProps = { activeEnvironment?: Environment; disabled?: boolean; environments?: Environment[]; loading?: boolean; variant?: "default" | "offline" | "unauthenticated"; onLogInClick?: () => void; onSelect?: (environment: Environment) => void | Promise; }; export const SideNavEnvironmentSelector: FC = ( props, ) => { const { activeEnvironment, disabled = false, environments = [], loading = false, variant = "default", onLogInClick, onSelect, } = props; const collapsed = useMediaQuery({ max: "medium" }); const isProductionEnvironmentActive = activeEnvironment?.kind === "prod"; return ( {collapsed && environments.length > 1 ? ( } /> ) : ( )} {environments.length > 1 && ( )} {!collapsed && ( <> {variant === "default" ? ( Environment ) : undefined} {variant === "unauthenticated" ? ( ) : undefined} {variant === "offline" ? ( Offline ) : undefined} {variant === "default" ? ( {isProductionEnvironmentActive || activeEnvironment === undefined ? "Production" : activeEnvironment?.name} ) : undefined} {variant === "unauthenticated" ? ( } onClick={onLogInClick} hiddenLabel="Log in to enable environments" /> ) : undefined} {environments.length > 1 ? ( : "unfoldMore"} hiddenLabel="Select environment" /> } /> ) : undefined} {variant === "default" && } )} ); }; type EnvironmentDropdownMenuProps = Pick< SideNavEnvironmentSelectorProps, "activeEnvironment" | "onSelect" > & { disabled: boolean; environments: Environment[]; trigger: ReactNode; }; const EnvironmentDropdownMenu: FC = (props) => { const { activeEnvironment, disabled, environments, onSelect, trigger } = props; const nonPersonalEnvironments = environments.filter( (environment) => environment.kind !== "dev", ); const personalEnvironment = environments.find( (environment) => environment.kind === "dev", ); return ( {trigger} {personalEnvironment ? ( Regular Environments ) : undefined} {nonPersonalEnvironments.map((environment) => ( ))} {personalEnvironment ? ( <> Personal Environment ) : undefined} ); }; type EnvironmentDropdownMenuItemProps = { environment: Environment; isActive?: boolean; } & Pick; const EnvironmentDropdownMenuItem: FC = ( props, ) => { const { environment, onSelect, isActive } = props; const isProductionEnvironment = environment.kind === "prod"; return ( } endIcon={Boolean(isActive) ? : undefined} onSelect={() => void onSelect?.(environment)} > {isProductionEnvironment ? "Production" : environment.name} ); }; const humanReadableKindMap = { prod: "Production", stage: "Staging", dev: "Development", } as const; type EnvironmentDotProps = { kind: Environment["kind"]; asStatus?: boolean; className?: string; }; const EnvironmentDot: FC = (props) => { const { kind, asStatus = false, className, ...otherProps } = props; const humanReadableKind = humanReadableKindMap[kind]; return (
{asStatus ? ( {humanReadableKind} environment ) : null}
); };