"use client"; import * as React from "react"; import { Check, ChevronsUpDown, PlusCircle } from "lucide-react"; import type { Workspace as PrismaWorkspace } from "@prisma/client"; import { cn } from "@ui/lib/utils"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Button } from "@ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, } from "@/components/ui/command"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { api } from "@/utils/api"; import { useSession } from "next-auth/react"; type PopoverTriggerProps = React.ComponentPropsWithoutRef< typeof PopoverTrigger >; type TeamSwitcherProps = PopoverTriggerProps; type Workspace = Pick; export default function TeamSwitcher({ className }: TeamSwitcherProps) { const ctx = api.useContext(); const { data: session } = useSession(); const { data: workspaces } = api.workspace.getAllForLoggedUser.useQuery( undefined, { enabled: session?.user !== undefined, } ); const { mutateAsync } = api.user.switchActiveWorkspace.useMutation({ onSuccess: () => { void ctx.workspace.getAllForLoggedUser.invalidate(); }, }); const [open, setOpen] = React.useState(false); const [showNewWorkspaceDialog, setShowNewWorkspaceDialog] = React.useState(false); const [selectedWS, setSelectedWS] = React.useState({ id: session?.user?.activeWorkspaceId || "", name: workspaces?.find((w) => w.id === session?.user?.activeWorkspaceId) ?.name || "", }); return ( No team found. {workspaces?.map((ws) => ( { setSelectedWS({ id: ws.id, name: ws.name, }); setOpen(false); void mutateAsync({ workspaceId: ws.id }); }} className="text-sm" > {session?.user.name ? session?.user?.name .split(" ") .map((n) => n[0]) .join("") : ""} {ws.name} ))} { setOpen(false); setShowNewWorkspaceDialog(true); }} > Create Team ); } /** * To use this Dialog, make sure you wrap it in a DialogTrigger component. * To activate the AddWorkspaceDialog component from within a Context Menu or Dropdown Menu, you must encase the Context Menu or Dropdown Menu component in the AddWorkspaceDialog component. */ export function AddWorkspaceDialog({ children, open, onOpenChange, }: { children: React.ReactNode; open: boolean; onOpenChange: React.Dispatch>; }) { const ctx = api.useContext(); const { mutateAsync } = api.workspace.create.useMutation({ onSuccess: () => { void ctx.workspace.getAllForLoggedUser.invalidate(); }, }); const [workspaceName, changeWorkspaceName] = React.useState(""); const { data: session } = useSession(); return ( {children} Create workspace Create a new workspace and invite your team members
changeWorkspaceName(e.target.value)} />
{/*
This is a nice way to do forms so I am not deleting it yet until ive used it somewhere else*/}
); }