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.

WIP program resources

Tim Wilson8829adcc87a6
diff --git a/apps/web/app/api/programs/[programId]/resources/route.ts b/apps/web/app/api/programs/[programId]/resources/route.ts
new file mode 100644
index 0000000000..bf1371fb13
--- /dev/null
+++ b/apps/web/app/api/programs/[programId]/resources/route.ts
@@ -0,0 +1,35 @@
+import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { programResourcesSchema } from "@/lib/zod/schemas/program-resources";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+
+// GET /api/programs/[programId]/resources - get resources for a program
+export const GET = withWorkspace(async ({ workspace, params }) => {
+ const { programId } = params;
+
+ await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
+
+ const program = await prisma.program.findUnique({
+ where: {
+ id: programId,
+ workspaceId: workspace.id,
+ },
+ select: {
+ resources: true,
+ },
+ });
+
+ const resources = programResourcesSchema.parse(
+ program?.resources ?? {
+ logos: [],
+ colors: [],
+ files: [],
+ },
+ );
+
+ return NextResponse.json(resources);
+});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx
index e0475e28fe..b0b0cba31f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx
@@ -1,16 +1,29 @@
"use client";
+import useProgramResources from "@/lib/swr/use-program-resources";
import { Button } from "@dub/ui";
-import { PropsWithChildren } from "react";
+import { formatFileSize } from "@dub/utils";
+import { PropsWithChildren, ReactNode } from "react";
export function ProgramResourcesPageClient() {
+ const { resources, loading } = useProgramResources();
+
return (
<div className="flex flex-col gap-10">
<Section
resource="logo"
title="Brand logos"
description="SVG, JPG, or PNG, max size of 10 MB"
- ></Section>
+ >
+ {resources?.logos?.map((logo) => (
+ <ResourceCard
+ key={logo.id}
+ icon={<div />}
+ title={logo.name}
+ description={formatFileSize(logo.size)}
+ />
+ ))}
+ </Section>
<Section
resource="color"
title="Colors"
@@ -53,3 +66,28 @@ function Section({
</div>
);
}
+
+function ResourceCard({
+ title,
+ description,
+ icon,
+}: {
+ title: string;
+ description: string;
+ icon: ReactNode;
+}) {
+ return (
+ <div className="flex items-center justify-between gap-4 rounded border border-neutral-200">
+ <div className="flex items-center gap-4">
+ <div className="flex size-10 rounded-md border border-neutral-200">
+ {icon}
+ </div>
+ <div className="flex flex-col">
+ <span className="text-sm font-medium text-neutral-800">{title}</span>
+ <span className="text-xs text-neutral-500">{description}</span>
+ </div>
+ </div>
+ {/* TODO: Menu button */}
+ </div>
+ );
+}
diff --git a/apps/web/lib/swr/use-program-resources.ts b/apps/web/lib/swr/use-program-resources.ts
new file mode 100644
index 0000000000..19a38af1e0
--- /dev/null
+++ b/apps/web/lib/swr/use-program-resources.ts
@@ -0,0 +1,34 @@
+import { fetcher } from "@dub/utils";
+import { useParams } from "next/navigation";
+import useSWR from "swr";
+import { z } from "zod";
+import { programResourcesSchema } from "../zod/schemas/program-resources";
+import useWorkspace from "./use-workspace";
+
+export type ProgramResourcesProps = z.infer<typeof programResourcesSchema>;
+
+export default function useProgramResources() {
+ const { id: workspaceId } = useWorkspace();
+ const { programId } = useParams();
+
+ const {
+ data: resources,
+ error,
+ mutate,
+ } = useSWR<ProgramResourcesProps>(
+ programId && workspaceId
+ ? `/api/programs/${programId}/resources?workspaceId=${workspaceId}`
+ : null,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
+
+ return {
+ resources,
+ error,
+ mutate,
+ loading: programId && !resources && !error,
+ };
+}
diff --git a/apps/web/lib/zod/schemas/program-resources.ts b/apps/web/lib/zod/schemas/program-resources.ts
index 8f5370db2b..118f74694d 100644
--- a/apps/web/lib/zod/schemas/program-resources.ts
+++ b/apps/web/lib/zod/schemas/program-resources.ts
@@ -1,12 +1,14 @@
import { z } from "zod";
export const programResourceFileSchema = z.object({
+ id: z.string(),
name: z.string(),
size: z.number(),
url: z.string().url(),
});
export const programResourceColorSchema = z.object({
+ id: z.string(),
name: z.string(),
color: z.string(),
});
diff --git a/packages/utils/src/functions/format-file-size.ts b/packages/utils/src/functions/format-file-size.ts
new file mode 100644
index 0000000000..bfb54bc1df
--- /dev/null
+++ b/packages/utils/src/functions/format-file-size.ts
@@ -0,0 +1,8 @@
+export function formatFileSize(bytes: number) {
+ var i = bytes == 0 ? 0 : Math.floor(Math.log(bytes) / Math.log(1024));
+ return (
+ +(bytes / Math.pow(1024, i)).toFixed(2) * 1 +
+ " " +
+ ["B", "KB", "MB", "GB", "TB"][i]
+ );
+}
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index 6ef820f467..ecc893d39e 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -10,6 +10,7 @@ export * from "./deep-equal";
export * from "./domains";
export * from "./fetch-with-timeout";
export * from "./fetcher";
+export * from "./format-file-size";
export * from "./generate-random-string";
export * from "./hash-string";
export * from "./is-click-on-interactive-child";