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.

Add image uploads

Tim Wilson240205c01c08
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/branding/branding-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/branding/branding-settings.tsx
index a4fe6dc4fe..f26be0a6e3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/branding/branding-settings.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/branding/branding-settings.tsx
@@ -4,11 +4,12 @@ import { updateProgramAction } from "@/lib/actions/partners/update-program";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { ProgramProps } from "@/lib/types";
-import { Button } from "@dub/ui";
-import { LoadingSpinner } from "@dub/ui/icons";
+import { Button, FileUpload } from "@dub/ui";
+import { LoadingSpinner, Plus } from "@dub/ui/icons";
import { useAction } from "next-safe-action/hooks";
+import { flightRouterStateSchema } from "next/dist/server/app-render/types";
import { PropsWithChildren, useId } from "react";
-import { useForm } from "react-hook-form";
+import { Controller, useForm } from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
@@ -54,15 +55,21 @@ function BrandingSettingsForm({ program }: { program: ProgramProps }) {
});
const {
+ control,
handleSubmit,
reset,
formState: { isDirty, isValid, isSubmitting, errors },
} = form;
const { executeAsync } = useAction(updateProgramAction, {
- async onSuccess() {
- toast.success("Program updated successfully.");
- mutate(`/api/programs/${program.id}?workspaceId=${workspaceId}`);
+ async onSuccess(res) {
+ if (res.data?.ok) {
+ toast.success("Program updated successfully.");
+ mutate(`/api/programs/${program.id}?workspaceId=${workspaceId}`);
+ } else {
+ toast.error("Failed to update program.");
+ return false;
+ }
},
onError({ error }) {
console.error(error);
@@ -101,14 +108,54 @@ function BrandingSettingsForm({ program }: { program: ProgramProps }) {
<FormRow
label="Logo"
description="A square logo that will be used in various parts of your program"
- ></FormRow>
+ >
+ <Controller
+ control={control}
+ name="logo"
+ rules={{ required: flightRouterStateSchema }}
+ render={({ field }) => (
+ <FileUpload
+ accept="images"
+ className="size-14 rounded-lg border border-neutral-300"
+ iconClassName="size-4 text-neutral-800"
+ icon={Plus}
+ variant="plain"
+ imageSrc={field.value}
+ readFile
+ onChange={({ src }) => field.onChange(src)}
+ content={null}
+ maxFileSizeMB={2}
+ />
+ )}
+ />
+ </FormRow>
<Divider />
<FormRow
label="Wordmark"
description="A full-sized logo used on the program application form"
- ></FormRow>
+ >
+ <Controller
+ control={control}
+ name="wordmark"
+ rules={{ required: false }}
+ render={({ field }) => (
+ <FileUpload
+ accept="images"
+ className="h-14 w-36 rounded-lg border border-neutral-300"
+ iconClassName="size-4 text-neutral-800"
+ icon={Plus}
+ variant="plain"
+ imageSrc={field.value}
+ readFile
+ onChange={({ src }) => field.onChange(src)}
+ content={null}
+ maxFileSizeMB={2}
+ />
+ )}
+ />
+ </FormRow>
<Divider />
@@ -147,7 +194,7 @@ function FormRow({
children,
}: PropsWithChildren<{ label: string; description: string }>) {
return (
- <div className="flex items-center">
+ <div className="flex items-center gap-6">
<div>
<label className="text-sm font-medium text-neutral-800">{label}</label>
<p className="mt-2 text-xs text-neutral-500">{description}</p>
diff --git a/apps/web/lib/actions/partners/onboard-partner.ts b/apps/web/lib/actions/partners/onboard-partner.ts
index 5392a92c84..9548437423 100644
--- a/apps/web/lib/actions/partners/onboard-partner.ts
+++ b/apps/web/lib/actions/partners/onboard-partner.ts
@@ -48,7 +48,7 @@ export const onboardPartnerAction = authUserActionClient
: createId({ prefix: "pn_" });
const imageUrl = await storage
- .upload(`partners/${partnerId}/image_${nanoid(7)}`, image)
+ .upload(`programs/${partnerId}/image_${nanoid(7)}`, image)
.then(({ url }) => url);
const payload = {
diff --git a/apps/web/lib/actions/partners/update-program.ts b/apps/web/lib/actions/partners/update-program.ts
index 43410872d8..4114f8d8b8 100644
--- a/apps/web/lib/actions/partners/update-program.ts
+++ b/apps/web/lib/actions/partners/update-program.ts
@@ -1,6 +1,9 @@
"use server";
+import { isStored, storage } from "@/lib/storage";
import { prisma } from "@dub/prisma";
+import { nanoid, R2_URL } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { z } from "zod";
import { getProgramOrThrow } from "../../api/programs/get-program-or-throw";
import { createProgramSchema } from "../../zod/schemas/programs";
@@ -9,6 +12,8 @@ import { authActionClient } from "../safe-action";
const schema = createProgramSchema.partial().extend({
workspaceId: z.string(),
programId: z.string(),
+ logo: z.string().nullish(),
+ wordmark: z.string().nullish(),
});
export const updateProgramAction = authActionClient
@@ -25,28 +30,62 @@ export const updateProgramAction = authActionClient
cookieLength,
domain,
url,
+ logo,
+ wordmark,
} = parsedInput;
- await getProgramOrThrow({
- workspaceId: workspace.id,
- programId,
- });
-
- const program = await prisma.program.update({
- where: {
- id: programId,
- },
- data: {
- name,
- commissionType,
- commissionAmount,
- commissionDuration,
- commissionInterval,
- cookieLength,
- domain,
- url,
- },
- });
-
- return program;
+ try {
+ const program = await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
+
+ const [logoUrl, wordmarkUrl] = await Promise.all([
+ logo && !isStored(logo)
+ ? storage
+ .upload(`programs/${programId}/logo_${nanoid(7)}`, logo)
+ .then(({ url }) => url)
+ : null,
+ wordmark && !isStored(wordmark)
+ ? storage
+ .upload(`programs/${programId}/wordmark_${nanoid(7)}`, wordmark)
+ .then(({ url }) => url)
+ : null,
+ ]);
+
+ await prisma.program.update({
+ where: {
+ id: programId,
+ },
+ data: {
+ name,
+ commissionType,
+ commissionAmount,
+ commissionDuration,
+ commissionInterval,
+ cookieLength,
+ domain,
+ url,
+ logo: logoUrl ?? undefined,
+ wordmark: wordmarkUrl ?? undefined,
+ },
+ });
+
+ // Delete old logo/wordmark if they were updated
+ waitUntil(
+ Promise.all([
+ ...(logoUrl && program.logo
+ ? [storage.delete(program.logo.replace(`${R2_URL}/`, ""))]
+ : []),
+ ...(wordmarkUrl && program.wordmark
+ ? [storage.delete(program.wordmark.replace(`${R2_URL}/`, ""))]
+ : []),
+ ]),
+ );
+
+ return { ok: true, program };
+ } catch (e) {
+ console.error("Failed to update program", e);
+ return { ok: false };
+ }
});
diff --git a/packages/ui/src/file-upload.tsx b/packages/ui/src/file-upload.tsx
index abac2ea342..4605c54f3e 100644
--- a/packages/ui/src/file-upload.tsx
+++ b/packages/ui/src/file-upload.tsx
@@ -2,7 +2,7 @@ import { cn, resizeImage } from "@dub/utils";
import { VariantProps, cva } from "class-variance-authority";
import { DragEvent, ReactNode, useState } from "react";
import { toast } from "sonner";
-import { CloudUpload, LoadingCircle } from "./icons";
+import { CloudUpload, Icon, LoadingCircle } from "./icons";
type AcceptedFileFormats = "any" | "images" | "csv";
@@ -57,6 +57,9 @@ export type FileUploadProps = FileUploadReadFileProps & {
className?: string;
iconClassName?: string;
previewClassName?: string;
+
+ icon?: Icon;
+
/**
* Custom preview component to display instead of the default
*/
@@ -111,6 +114,7 @@ export function FileUpload({
className,
iconClassName,
previewClassName,
+ icon: Icon = CloudUpload,
customPreview,
accept = "any",
imageSrc,
@@ -231,7 +235,7 @@ export function FileUpload({
: cn(!disabled && "group-hover:bg-gray-50"),
)}
>
- <CloudUpload
+ <Icon
className={cn(
"size-7 transition-all duration-75",
!disabled