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.

4.6 KB
Raw Blame
import { FOLDER_WORKSPACE_ACCESS } from "@/lib/folder/constants";
import useCurrentFolderId from "@/lib/swr/use-current-folder-id";
import useFolder from "@/lib/swr/use-folder";
import { useFolderUsers } from "@/lib/swr/use-folder-users";
import useWorkspace from "@/lib/swr/use-workspace";
import { FolderUser } from "@/lib/types";
import { UserAvatar } from "@/ui/users/user-avatar";
import { BlurImage, LoadingSpinner } from "@dub/ui";
import { PropsWithChildren } from "react";
import { FolderActions } from "./folder-actions";

export function FolderInfoPanel() {
  const {
    id: workspaceId,
    logo: workspaceLogo,
    name: workspaceName,
  } = useWorkspace();
  const { folderId } = useCurrentFolderId();
  const { folder } = useFolder({ folderId });

  const { users, isLoading: isLoadingUsers } = useFolderUsers({
    folderId: folderId,
    enabled: !!folder,
  });

  if (!folder)
    return (
      <div className="flex size-full items-center justify-center">
        <LoadingSpinner />
      </div>
    );

  const owners = users?.filter((u) => u.role === "owner");
  const editors = users?.filter((u) => u.role === "editor");
  const viewers = users?.filter((u) => u.role === "viewer");

  return (
    <div className="flex flex-col gap-6">
      {folder.description && (
        <div className="flex flex-col gap-2">
          <SectionHeader>Description</SectionHeader>
          <p className="text-content-subtle whitespace-pre-wrap text-sm">
            {folder.description}
          </p>
        </div>
      )}

      <div className="flex flex-col gap-2">
        <SectionHeader>Workspace</SectionHeader>
        <div className="flex items-center gap-3">
          <BlurImage
            src={workspaceLogo || `https://avatar.vercel.sh/${workspaceId}`}
            referrerPolicy="no-referrer"
            width={36}
            height={36}
            alt={`${workspaceName} logo`}
            className="size-9 shrink-0 overflow-hidden rounded-full"
            draggable={false}
          />
          <div>
            <span className="text-content-emphasis min-w-0 truncate text-sm font-semibold">
              {workspaceName}
            </span>
            <div className="text-content-default bg-bg-emphasis w-fit rounded-md px-1 text-xs font-semibold">
              {folder.accessLevel
                ? FOLDER_WORKSPACE_ACCESS[folder.accessLevel]
                : "No access"}
            </div>
          </div>
        </div>
      </div>

      {Boolean(isLoadingUsers || owners?.length) && (
        <div className="flex flex-col gap-2">
          <SectionHeader>Owner</SectionHeader>
          <UserList users={owners} isLoading={isLoadingUsers} />
        </div>
      )}

      {Boolean(isLoadingUsers || editors?.length) && (
        <div className="flex flex-col gap-2">
          <SectionHeader>Editor</SectionHeader>
          <UserList users={editors} isLoading={isLoadingUsers} />
        </div>
      )}

      {Boolean(isLoadingUsers || viewers?.length) && (
        <div className="flex flex-col gap-2">
          <SectionHeader>Viewer</SectionHeader>
          <UserList users={viewers} isLoading={isLoadingUsers} />
        </div>
      )}
    </div>
  );
}

const SectionHeader = ({ children }: PropsWithChildren) => (
  <h3 className="text-content-default text-sm font-semibold">{children}</h3>
);

const UserList = ({
  users,
  isLoading,
}: {
  users?: FolderUser[];
  isLoading?: boolean;
}) => (
  <div className="flex flex-col gap-3">
    {isLoading
      ? [...Array(3)].map((_, index) => (
          <div key={index} className="flex items-center gap-3">
            <div className="size-8 animate-pulse rounded-full bg-neutral-200" />
            <div className="flex flex-col">
              <div className="h-4 w-24 animate-pulse rounded bg-neutral-200" />
              <div className="mt-1 h-3 w-32 animate-pulse rounded bg-neutral-200" />
            </div>
          </div>
        ))
      : users?.map((user) => (
          <div key={user.id} className="flex min-w-12 items-center gap-3">
            <UserAvatar user={user} className="size-8" />
            <div className="min-w-0">
              <h3 className="truncate text-xs font-medium text-neutral-800">
                {user?.name || user?.email || "Anonymous User"}
              </h3>
              <p className="truncate text-xs font-normal text-neutral-400">
                {user.email}
              </p>
            </div>
          </div>
        ))}
  </div>
);

export function FolderInfoPanelControls() {
  const { folderId } = useCurrentFolderId();
  const { folder } = useFolder({ folderId });

  if (!folder) return null;

  return <FolderActions folder={folder} className="border-subtle border" />;
}