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.

798 B
Raw Blame
import { Folder } from "@/lib/types";
import { fetcher } from "@dub/utils";
import useSWR from "swr";
import useWorkspace from "./use-workspace";

export default function useFolder({
  folderId,
  enabled = true,
}: {
  folderId?: string | null;
  enabled?: boolean;
}) {
  const { id: workspaceId, plan } = useWorkspace();

  const swrEnabled =
    enabled &&
    folderId &&
    folderId !== "unsorted" &&
    workspaceId &&
    plan !== "free";

  const {
    data: folder,
    isValidating,
    isLoading,
    error,
  } = useSWR<Folder>(
    swrEnabled ? `/api/folders/${folderId}?workspaceId=${workspaceId}` : null,
    fetcher,
    {
      dedupingInterval: 60000,
      keepPreviousData: true,
    },
  );

  return {
    folder,
    loading: isLoading,
    isValidating,
    error,
  };
}