Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

3.7 KB
Raw Blame
import useWorkspaces from "@/lib/swr/use-workspaces";
import { BlurImage, Button, Combobox } from "@dub/ui";
import { cn } from "@dub/utils";
import { useMemo, useState } from "react";
import { useAddWorkspaceModal } from "../modals/add-workspace-modal";

interface WorkspaceSelectorProps {
  selectedWorkspace: string;
  setSelectedWorkspace: (workspace: string | null) => void;
  showAddWorkspaceButton?: boolean;
}

export function WorkspaceSelector({
  selectedWorkspace,
  setSelectedWorkspace,
  showAddWorkspaceButton = true,
}: WorkspaceSelectorProps) {
  const [openPopover, setOpenPopover] = useState(false);
  const { workspaces, loading } = useWorkspaces();

  const { AddWorkspaceModal, setShowAddWorkspaceModal } =
    useAddWorkspaceModal();

  const filteredWorkspaces = useMemo(() => workspaces ?? [], [workspaces]);

  const workspaceOptions = useMemo(
    () =>
      filteredWorkspaces.map((workspace) => ({
        value: workspace.slug,
        label: workspace.name,
        icon: (
          <BlurImage
            src={workspace.logo || `https://avatar.vercel.sh/${workspace.id}`}
            alt={workspace.id}
            width={16}
            height={16}
            className="size-4 rounded-full"
          />
        ),
      })),
    [filteredWorkspaces],
  );

  const selectedOption = useMemo(() => {
    if (!selectedWorkspace) {
      return null;
    }

    const workspace = filteredWorkspaces.find(
      (w) => w.slug === selectedWorkspace,
    );

    if (!workspace) {
      return null;
    }

    return {
      value: workspace.slug,
      label: workspace.name,
      icon: (
        <BlurImage
          src={workspace.logo || `https://avatar.vercel.sh/${workspace.id}`}
          alt={workspace.id}
          width={16}
          height={16}
          className="size-4 rounded-full"
        />
      ),
    };
  }, [filteredWorkspaces, selectedWorkspace]);

  return (
    <>
      {showAddWorkspaceButton && <AddWorkspaceModal />}
      <Combobox
        options={loading ? undefined : workspaceOptions}
        setSelected={(option) => {
          setSelectedWorkspace(option?.value ?? null);
        }}
        selected={selectedOption}
        icon={loading ? null : selectedOption?.icon}
        caret={true}
        placeholder={loading ? "" : "Select workspace"}
        searchPlaceholder="Search workspaces..."
        matchTriggerWidth
        open={openPopover}
        onOpenChange={setOpenPopover}
        buttonProps={{
          className: cn(
            "w-full justify-start border-neutral-300 px-3",
            "data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",
            "focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",
          ),
        }}
        emptyState={
          <div className="flex w-full flex-col items-center gap-2 py-4">
            No workspaces found
            {showAddWorkspaceButton && (
              <Button
                onClick={() => {
                  setOpenPopover(false);
                  setShowAddWorkspaceModal(true);
                }}
                variant="primary"
                className="h-7 w-fit px-2"
                text="Create new workspace"
              />
            )}
          </div>
        }
      >
        {loading ? (
          <div className="flex items-center gap-3">
            <div className="size-4 animate-pulse rounded-full bg-neutral-200" />
            <div className="h-4 w-32 animate-pulse rounded-md bg-neutral-200" />
          </div>
        ) : selectedOption ? (
          <span className="flex w-full min-w-0 items-center gap-1.5">
            <span className="min-w-0 truncate">{selectedOption.label}</span>
          </span>
        ) : null}
      </Combobox>
    </>
  );
}