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.

list folders with links count

Kiran K70a35b849cfc
diff --git a/apps/web/app/api/folders/route.ts b/apps/web/app/api/folders/route.ts
index 19b4be09ee..4f86e2e37f 100644
--- a/apps/web/app/api/folders/route.ts
+++ b/apps/web/app/api/folders/route.ts
@@ -18,6 +18,8 @@ export const GET = withWorkspace(
select: {
id: true,
name: true,
+ createdAt: true,
+ updatedAt: true,
},
orderBy: {
name: "asc",
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx
new file mode 100644
index 0000000000..d856124957
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page-client.tsx
@@ -0,0 +1,85 @@
+"use client";
+
+import useFolders from "@/lib/swr/use-folders";
+import useLinksCount from "@/lib/swr/use-links-count";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { FolderCard } from "@/ui/folders/folder-card";
+import { FolderCardPlaceholder } from "@/ui/folders/folder-card-placeholder";
+import EmptyState from "@/ui/shared/empty-state";
+import { CardList, TooltipContent } from "@dub/ui";
+import { InfoTooltip } from "@dub/ui/src/tooltip";
+import { Folder } from "lucide-react";
+
+export const FoldersPageClient = () => {
+ const { id: workspaceId } = useWorkspace();
+ const { folders, isLoading, isValidating } = useFolders();
+
+ const { data: linksCount } = useLinksCount({
+ groupBy: "folderId",
+ showArchived: true,
+ });
+
+ // const { AddEditTagModal, AddTagButton } = useAddEditTagModal();
+ // const [openMenuTagId, setOpenMenuTagId] = useState<string | null>(null);
+ // const { tags, loading } = useTags();
+ // const { data: tagsCount } = useLinksCount({
+ // groupBy: "tagId",
+ // showArchived: true,
+ // });
+
+ return (
+ <>
+ <div className="grid gap-5">
+ <div className="flex flex-wrap justify-between gap-6">
+ <div className="flex items-center gap-x-2">
+ <h1 className="text-2xl font-semibold tracking-tight text-black">
+ Folders
+ </h1>
+ <InfoTooltip
+ content={
+ <TooltipContent
+ title="Learn more about how to use folders on Dub."
+ href="https://dub.co/help/article/how-to-use-folders"
+ target="_blank"
+ cta="Learn more"
+ />
+ }
+ />
+ </div>
+ <div className="flex w-full flex-wrap items-center gap-3 sm:w-auto">
+ {/* <AddTagButton /> */}
+ </div>
+ </div>
+
+ {/* {workspaceId && <AddEditTagModal />} */}
+
+ {isLoading || folders?.length ? (
+ <CardList variant="loose" loading={isLoading}>
+ {folders?.length
+ ? folders.map((folder) => (
+ <FolderCard
+ key={folder.id}
+ folder={folder}
+ linksCount={
+ linksCount?.find((c) => c.folderId === folder.id)
+ ?.count ?? 0
+ }
+ />
+ ))
+ : Array.from({ length: 6 }).map((_, idx) => (
+ <FolderCardPlaceholder key={idx} />
+ ))}
+ </CardList>
+ ) : (
+ <div className="flex flex-col items-center gap-4 rounded-xl border border-gray-200 py-10">
+ <EmptyState
+ icon={Folder}
+ title="No folders found for this workspace."
+ />
+ {/* <AddTagButton /> */}
+ </div>
+ )}
+ </div>
+ </>
+ );
+};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page.tsx
new file mode 100644
index 0000000000..1bab2ccc16
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/folders/page.tsx
@@ -0,0 +1,5 @@
+import { FoldersPageClient } from "./page-client";
+
+export default async function FoldersPage() {
+ return <FoldersPageClient />;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout-client.tsx
index 9e42daf6d3..050f643f2b 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout-client.tsx
@@ -15,7 +15,7 @@ import {
Tag,
Users6,
} from "@dub/ui/src/icons";
-import { Webhook } from "lucide-react";
+import { Folder, Webhook } from "lucide-react";
import { ReactNode, useMemo } from "react";
export default function WorkspaceSettingsLayoutClient({
@@ -41,6 +41,11 @@ export default function WorkspaceSettingsLayoutClient({
icon: Globe,
segment: "domains",
},
+ {
+ name: "Folders",
+ icon: Folder,
+ segment: "folders",
+ },
{
name: "Tags",
icon: Tag,
diff --git a/apps/web/lib/api/links/get-links-count.ts b/apps/web/lib/api/links/get-links-count.ts
index af5ad6c3f6..2b3da10c3b 100644
--- a/apps/web/lib/api/links/get-links-count.ts
+++ b/apps/web/lib/api/links/get-links-count.ts
@@ -23,6 +23,8 @@ export async function getLinksCount({
withTags,
} = searchParams;
+ console.log({ groupBy });
+
const combinedTagIds = combineTagIds({ tagId, tagIds });
const linksWhere = {
@@ -90,7 +92,34 @@ export async function getLinksCount({
: {}),
};
- if (groupBy === "domain" || groupBy === "userId") {
+ // Get the counts for each folder
+ if (groupBy === "folderId") {
+ const response = await prisma.folder.findMany({
+ where: {
+ projectId: workspaceId,
+ },
+ include: {
+ _count: {
+ select: {
+ links: {
+ where: linksWhere,
+ },
+ },
+ },
+ },
+ });
+
+ return response.map((folder) => ({
+ folderId: folder.id,
+ count: folder._count.links,
+ }));
+ }
+
+ if (
+ groupBy === "domain" ||
+ groupBy === "userId" ||
+ groupBy === "folderId"
+ ) {
return await prisma.link.groupBy({
by: [groupBy],
where,
diff --git a/apps/web/lib/swr/use-folders.ts b/apps/web/lib/swr/use-folders.ts
new file mode 100644
index 0000000000..63676ab867
--- /dev/null
+++ b/apps/web/lib/swr/use-folders.ts
@@ -0,0 +1,22 @@
+import { FolderProps } from "@/lib/types";
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import useWorkspace from "./use-workspace";
+
+export default function useFolders() {
+ const { id } = useWorkspace();
+
+ const {
+ data: folders,
+ isValidating,
+ isLoading,
+ } = useSWR<FolderProps[]>(id && `/api/folders?workspaceId=${id}`, fetcher, {
+ dedupingInterval: 60000,
+ });
+
+ return {
+ folders,
+ isLoading,
+ isValidating,
+ };
+}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index f48c77654e..60d0ec65f2 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -4,6 +4,7 @@ import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
import { Link, Project, Webhook } from "@prisma/client";
import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";
import { trackCustomerResponseSchema } from "./zod/schemas/customers";
+import { folderSchema } from "./zod/schemas/folders";
import { integrationSchema } from "./zod/schemas/integration";
import { trackLeadResponseSchema } from "./zod/schemas/leads";
import { createLinkBodySchema } from "./zod/schemas/links";
@@ -259,3 +260,5 @@ export type TrackCustomerResponse = z.infer<typeof trackCustomerResponseSchema>;
export type TrackLeadResponse = z.infer<typeof trackLeadResponseSchema>;
export type TrackSaleResponse = z.infer<typeof trackSaleResponseSchema>;
+
+export type FolderProps = z.infer<typeof folderSchema>;
diff --git a/apps/web/lib/zod/schemas/folders.ts b/apps/web/lib/zod/schemas/folders.ts
index 94d04002d3..a19891b2aa 100644
--- a/apps/web/lib/zod/schemas/folders.ts
+++ b/apps/web/lib/zod/schemas/folders.ts
@@ -12,6 +12,8 @@ const folderAccessControlSchema = z.object({
export const folderSchema = z.object({
id: z.string().describe("The unique ID of the folder."),
name: z.string().describe("The name of the folder."),
+ createdAt: z.date().describe("The date the folder was created."),
+ updatedAt: z.date().describe("The date the folder was updated."),
// users: z
// .array(folderAccessControlSchema)
// .describe("The list of users and their access level to the folder.")
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index d0ab5bbe5a..3716688067 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -91,7 +91,12 @@ export const getLinksQuerySchema = LinksQuerySchema.merge(
export const getLinksCountQuerySchema = LinksQuerySchema.merge(
z.object({
groupBy: z
- .union([z.literal("domain"), z.literal("tagId"), z.literal("userId")])
+ .union([
+ z.literal("domain"),
+ z.literal("tagId"),
+ z.literal("userId"),
+ z.literal("folderId"),
+ ])
.optional()
.describe("The field to group the links by."),
}),
diff --git a/apps/web/ui/folders/folder-card-placeholder.tsx b/apps/web/ui/folders/folder-card-placeholder.tsx
new file mode 100644
index 0000000000..f1b7be286c
--- /dev/null
+++ b/apps/web/ui/folders/folder-card-placeholder.tsx
@@ -0,0 +1,18 @@
+import { CardList } from "@dub/ui";
+
+export function FolderCardPlaceholder() {
+ return (
+ <CardList.Card>
+ <div className="flex h-8 items-center justify-between gap-5 sm:gap-8 md:gap-12">
+ <div className="flex items-center gap-3">
+ <div className="h-5 w-5 animate-pulse rounded-md bg-gray-200" />
+ <div className="h-5 w-16 animate-pulse rounded-md bg-gray-200 sm:w-32" />
+ </div>
+ <div className="flex items-center gap-5 sm:gap-8 md:gap-12">
+ <div className="h-5 w-16 animate-pulse rounded-md bg-gray-200" />
+ <div className="w-8" />
+ </div>
+ </div>
+ </CardList.Card>
+ );
+}
diff --git a/apps/web/ui/folders/folder-card.tsx b/apps/web/ui/folders/folder-card.tsx
new file mode 100644
index 0000000000..9182aa9adc
--- /dev/null
+++ b/apps/web/ui/folders/folder-card.tsx
@@ -0,0 +1,27 @@
+import { FolderProps } from "@/lib/types";
+import { CardList, useMediaQuery } from "@dub/ui";
+import { FolderInfo } from "./folder-info";
+
+export const FolderCard = ({
+ folder,
+ linksCount,
+}: {
+ folder: FolderProps;
+ linksCount: number;
+}) => {
+ const { isMobile } = useMediaQuery();
+
+ return (
+ <>
+ <CardList.Card
+ key={folder.id}
+ onClick={isMobile ? undefined : () => () => {}}
+ innerClassName="flex items-center gap-5 sm:gap-8 md:gap-12 text-sm"
+ >
+ <div className="min-w-0 grow">
+ <FolderInfo folder={folder} linksCount={linksCount} />
+ </div>
+ </CardList.Card>
+ </>
+ );
+};
diff --git a/apps/web/ui/folders/folder-info.tsx b/apps/web/ui/folders/folder-info.tsx
new file mode 100644
index 0000000000..d926500b16
--- /dev/null
+++ b/apps/web/ui/folders/folder-info.tsx
@@ -0,0 +1,110 @@
+"use client";
+
+import { FolderProps } from "@/lib/types";
+import { Avatar, TokenAvatar } from "@dub/ui";
+import {
+ Apple,
+ Cards,
+ CircleHalfDottedClock,
+ EarthPosition,
+ EyeSlash,
+ InputPassword,
+ Robot,
+} from "@dub/ui/src/icons";
+import { useRef } from "react";
+import { Link } from "../shared/icons";
+
+const quickViewSettings = [
+ { label: "Custom Social Media Cards", icon: Cards, key: "proxy" },
+ { label: "Link Cloaking", icon: EyeSlash, key: "rewrite" },
+ { label: "Password Protection", icon: InputPassword, key: "password" },
+ { label: "Link Expiration", icon: CircleHalfDottedClock, key: "expiresAt" },
+ { label: "iOS Targeting", icon: Apple, key: "ios" },
+ { label: "Android Targeting", icon: Robot, key: "android" },
+ { label: "Geo Targeting", icon: EarthPosition, key: "geo" },
+];
+
+export function FolderInfo({
+ folder,
+ linksCount,
+}: {
+ folder: FolderProps;
+ linksCount: number;
+}) {
+ const ref = useRef<HTMLDivElement>(null);
+ // const { displayProperties } = useContext(LinksDisplayContext);
+ // const isVisible = useInViewport(ref, { defaultValue: true });
+
+ return (
+ <div
+ ref={ref}
+ className="flex h-[32px] items-center gap-3 transition-[height] group-data-[variant=loose]/card-list:h-[60px]"
+ >
+ <div className="relative hidden shrink-0 items-center justify-center sm:flex">
+ <div className="absolute inset-0 shrink-0 rounded-full border border-gray-200 opacity-0 transition-opacity group-data-[variant=loose]/card-list:sm:opacity-100">
+ <div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-gray-100" />
+ </div>
+ <div className="relative pr-0.5 transition-[padding] group-data-[variant=loose]/card-list:sm:p-2">
+ <TokenAvatar id={folder.id} className="size-6" />
+ </div>
+ </div>
+ <div className="h-[24px] min-w-0 transition-[height] group-data-[variant=loose]/card-list:h-[44px]">
+ <div className="min-w-0 shrink grow-0">
+ <div className="flex flex-col">
+ <span className="font-semibold leading-6 text-gray-800">
+ {folder.name}
+ </span>
+ <div className="flex items-center gap-4">
+ <span className="flex items-center gap-1 text-sm text-gray-500">
+ <Link className="h-4 w-4" />
+ {linksCount} link{linksCount > 1 ? "s" : ""}
+ </span>
+ <Avatar user={null} className="h-5 w-5" />
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+}
+
+// const UserAvatar = ({
+// link,
+// compact,
+// }: {
+// link: ResponseLink;
+// compact?: boolean;
+// }) => {
+// const { user } = link;
+// const { slug } = useWorkspace();
+
+// return (
+// <Tooltip
+// content={
+// <div className="w-full p-3">
+// <Avatar user={user} className="h-8 w-8" />
+// <div className="mt-2 flex items-center gap-1.5">
+// <p className="text-sm font-semibold text-gray-700">
+// {user?.name || user?.email || "Anonymous User"}
+// </p>
+// {!slug && // this is only shown in admin mode (where there's no slug)
+// user?.email && (
+// <CopyButton
+// value={user.email}
+// icon={Mail}
+// className="[&>*]:h-3 [&>*]:w-3"
+// />
+// )}
+// </div>
+// <div className="flex flex-col gap-1 text-xs text-gray-500">
+// {user?.name && user.email && <p>{user.email}</p>}
+// </div>
+// </div>
+// }
+// >
+// <div>
+// <Avatar user={user} className="h-4 w-4" />
+// </div>
+// </Tooltip>
+// );
+// }