fosrl.pangolin/src/components/Header.tsx

285 lines
13 KiB
TypeScript
Raw Normal View History

2024-10-13 22:49:14 -04:00
"use client";
import { createApiClient } from "@app/api";
2024-10-13 22:49:14 -04:00
import { Avatar, AvatarFallback } from "@app/components/ui/avatar";
import { Button } from "@app/components/ui/button";
2024-11-22 23:06:12 -05:00
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator
2024-11-22 23:06:12 -05:00
} from "@app/components/ui/command";
2024-10-13 22:49:14 -04:00
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger
2024-10-13 22:49:14 -04:00
} from "@app/components/ui/dropdown-menu";
2024-11-22 23:06:12 -05:00
import {
Popover,
PopoverContent,
PopoverTrigger
2024-11-22 23:06:12 -05:00
} from "@app/components/ui/popover";
import { useEnvContext } from "@app/hooks/useEnvContext";
import { useToast } from "@app/hooks/useToast";
2024-11-22 23:06:12 -05:00
import { cn, formatAxiosError } from "@app/lib/utils";
2024-10-19 16:37:40 -04:00
import { ListOrgsResponse } from "@server/routers/org";
import {
Check,
ChevronsUpDown,
Laptop,
LogOut,
Moon,
Plus,
2024-12-23 23:59:15 -05:00
Sun
} from "lucide-react";
import { useTheme } from "next-themes";
2024-10-13 22:49:14 -04:00
import Link from "next/link";
2024-10-19 16:37:40 -04:00
import { useRouter } from "next/navigation";
2024-11-22 23:06:12 -05:00
import { useState } from "react";
2024-12-23 23:59:15 -05:00
import Enable2FaForm from "./Enable2FaForm";
import { userUserContext } from "@app/hooks/useUserContext";
2024-10-13 22:49:14 -04:00
type HeaderProps = {
2024-12-23 23:59:15 -05:00
orgId?: string;
orgs?: ListOrgsResponse["orgs"];
2024-10-13 22:49:14 -04:00
};
2024-12-23 23:59:15 -05:00
export function Header({ orgId, orgs }: HeaderProps) {
2024-10-19 16:37:40 -04:00
const { toast } = useToast();
const { setTheme, theme } = useTheme();
2024-10-19 16:37:40 -04:00
2024-12-23 23:59:15 -05:00
const { user, updateUser } = userUserContext();
2024-11-22 23:06:12 -05:00
const [open, setOpen] = useState(false);
const [userTheme, setUserTheme] = useState<"light" | "dark" | "system">(
theme as "light" | "dark" | "system"
);
2024-11-22 23:06:12 -05:00
2024-12-23 23:59:15 -05:00
const [openEnable2fa, setOpenEnable2fa] = useState(false);
2024-10-19 16:37:40 -04:00
const router = useRouter();
const api = createApiClient(useEnvContext());
2024-10-13 22:49:14 -04:00
function getInitials() {
2024-12-23 23:59:15 -05:00
return user.email.substring(0, 2).toUpperCase();
2024-10-13 22:49:14 -04:00
}
2024-10-19 16:37:40 -04:00
function logout() {
api.post("/auth/logout")
.catch((e) => {
console.error("Error logging out", e);
toast({
title: "Error logging out",
description: formatAxiosError(e, "Error logging out")
2024-10-19 16:37:40 -04:00
});
})
.then(() => {
router.push("/auth/login");
});
}
function handleThemeChange(theme: "light" | "dark" | "system") {
setUserTheme(theme);
setTheme(theme);
}
2024-10-13 22:49:14 -04:00
return (
<>
2024-12-23 23:59:15 -05:00
<Enable2FaForm open={openEnable2fa} setOpen={setOpenEnable2fa} />
2024-10-13 22:49:14 -04:00
<div className="flex items-center justify-between">
2024-10-14 00:02:55 -04:00
<div className="flex items-center gap-4">
2024-10-13 23:05:22 -04:00
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
2024-10-14 12:10:02 -04:00
variant="outline"
2024-10-13 23:05:22 -04:00
className="relative h-10 w-10 rounded-full"
>
2024-10-14 12:10:02 -04:00
<Avatar className="h-9 w-9">
2024-10-13 23:05:22 -04:00
<AvatarFallback>
{getInitials()}
</AvatarFallback>
</Avatar>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-56"
2024-10-14 12:10:02 -04:00
align="start"
2024-10-13 23:05:22 -04:00
forceMount
>
<DropdownMenuLabel className="font-normal">
<div className="flex flex-col space-y-1">
<p className="text-sm font-medium leading-none">
Signed in as
</p>
<p className="text-xs leading-none text-muted-foreground">
2024-12-23 23:59:15 -05:00
{user.email}
2024-10-13 23:05:22 -04:00
</p>
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
2024-12-23 23:59:15 -05:00
{!user.twoFactorEnabled && (
<DropdownMenuItem
onClick={() => setOpenEnable2fa(true)}
>
<span>Enable Two-factor</span>
</DropdownMenuItem>
)}
{user.twoFactorEnabled && (
<DropdownMenuItem>
<span>Disable Two-factor</span>
</DropdownMenuItem>
)}
<DropdownMenuSeparator />
<DropdownMenuLabel>Theme</DropdownMenuLabel>
{(["light", "dark", "system"] as const).map(
(themeOption) => (
<DropdownMenuItem
key={themeOption}
onClick={() =>
handleThemeChange(themeOption)
}
>
{themeOption === "light" && (
<Sun className="mr-2 h-4 w-4" />
)}
{themeOption === "dark" && (
<Moon className="mr-2 h-4 w-4" />
)}
{themeOption === "system" && (
<Laptop className="mr-2 h-4 w-4" />
)}
<span className="capitalize">
{themeOption}
</span>
{userTheme === themeOption && (
<span className="absolute right-2 flex h-3.5 w-3.5 items-center justify-center">
<span className="h-2 w-2 rounded-full bg-primary"></span>
</span>
)}
</DropdownMenuItem>
)
)}
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => logout()}>
<LogOut className="mr-2 h-4 w-4" />
<span>Log out</span>
</DropdownMenuItem>
2024-10-13 23:05:22 -04:00
</DropdownMenuContent>
</DropdownMenu>
<span className="truncate max-w-[150px] md:max-w-none font-medium">
2024-12-23 23:59:15 -05:00
{user.email}
2024-10-14 12:10:02 -04:00
</span>
</div>
<div className="flex items-center">
<div className="hidden md:block">
<div className="flex items-center gap-4 mr-4">
<Link
href="/docs"
2024-10-15 23:52:58 -04:00
className="text-muted-foreground hover:text-foreground"
2024-10-14 12:10:02 -04:00
>
Documentation
</Link>
<Link
href="/support"
2024-10-15 23:52:58 -04:00
className="text-muted-foreground hover:text-foreground"
2024-10-14 12:10:02 -04:00
>
Support
</Link>
</div>
</div>
2024-12-23 23:59:15 -05:00
{orgs && (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
size="lg"
role="combobox"
aria-expanded={open}
className="w-full md:w-[200px] h-12 px-3 py-4 bg-neutral hover:bg-neutral"
>
<div className="flex items-center justify-between w-full">
<div className="flex flex-col items-start">
<span className="font-bold text-sm">
Organization
</span>
<span className="text-sm text-muted-foreground">
{orgId
? orgs?.find(
(org) =>
org.orgId ===
orgId
)?.name
: "None selected"}
</span>
</div>
<ChevronsUpDown className="h-4 w-4 shrink-0 opacity-50" />
2024-11-22 23:06:12 -05:00
</div>
2024-12-23 23:59:15 -05:00
</Button>
</PopoverTrigger>
<PopoverContent className="[100px] md:w-[180px] p-0">
<Command>
<CommandInput placeholder="Search..." />
<CommandEmpty>
No organizations found.
</CommandEmpty>
<CommandGroup heading="Create">
<CommandList>
2024-11-22 23:06:12 -05:00
<CommandItem
onSelect={(currentValue) => {
2024-12-23 23:59:15 -05:00
router.push("/setup");
2024-11-22 23:06:12 -05:00
}}
>
2024-12-23 23:59:15 -05:00
<Plus className="mr-2 h-4 w-4" />
New Organization
2024-11-22 23:06:12 -05:00
</CommandItem>
2024-12-23 23:59:15 -05:00
</CommandList>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Organizations">
<CommandList>
{orgs.map((org) => (
<CommandItem
key={org.orgId}
onSelect={(
currentValue
) => {
router.push(
`/${org.orgId}/settings`
);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
orgId === org.orgId
? "opacity-100"
: "opacity-0"
)}
/>
{org.name}
</CommandItem>
))}
</CommandList>
</CommandGroup>
</Command>
</PopoverContent>
</Popover>
)}
2024-10-13 23:05:22 -04:00
</div>
2024-10-13 22:49:14 -04:00
</div>
</>
);
}
2024-12-23 23:59:15 -05:00
export default Header;