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 link modal and resource management for program brand assets

Kiran K0668f814931f
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/add-link-modal.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/add-link-modal.tsx
new file mode 100644
index 0000000000..f9ae52baec
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/add-link-modal.tsx
@@ -0,0 +1,195 @@
+"use client";
+
+import { addProgramResourceAction } from "@/lib/actions/partners/program-resources/add-program-resource";
+import useProgramResources from "@/lib/swr/use-program-resources";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { Button, Modal } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { useAction } from "next-safe-action/hooks";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
+import { useForm } from "react-hook-form";
+import { toast } from "sonner";
+import { z } from "zod";
+
+type AddLinkModalProps = {
+ showAddLinkModal: boolean;
+ setShowAddLinkModal: Dispatch<SetStateAction<boolean>>;
+};
+
+const linkFormSchema = z.object({
+ name: z.string().min(1, "Display name is required"),
+ url: z.string().url("Please enter a valid URL"),
+});
+
+type LinkFormData = z.infer<typeof linkFormSchema>;
+
+function AddLinkModal(props: AddLinkModalProps) {
+ return (
+ <Modal
+ showModal={props.showAddLinkModal}
+ setShowModal={props.setShowAddLinkModal}
+ >
+ <AddLinkModalInner {...props} />
+ </Modal>
+ );
+}
+
+function AddLinkModalInner({ setShowAddLinkModal }: AddLinkModalProps) {
+ const { id: workspaceId } = useWorkspace();
+ const { mutate } = useProgramResources();
+
+ const {
+ register,
+ handleSubmit,
+ setError,
+ formState: { errors, isSubmitting, isSubmitSuccessful },
+ } = useForm<LinkFormData>({
+ defaultValues: {
+ name: "",
+ url: "",
+ },
+ });
+
+ const { executeAsync } = useAction(addProgramResourceAction, {
+ onSuccess: () => {
+ mutate();
+ setShowAddLinkModal(false);
+ toast.success("Link added successfully!");
+ },
+ onError({ error }) {
+ if (error.serverError) {
+ setError("root.serverError", {
+ message: error.serverError,
+ });
+ toast.error(error.serverError);
+ } else {
+ toast.error("Failed to add link");
+ }
+ },
+ });
+
+ return (
+ <>
+ <div className="space-y-2 border-b border-neutral-200 p-4 sm:p-6">
+ <h3 className="text-lg font-medium leading-none">Add link</h3>
+ </div>
+
+ <form
+ onSubmit={handleSubmit(async (data: LinkFormData) => {
+ await executeAsync({
+ workspaceId: workspaceId!,
+ name: data.name,
+ resourceType: "link",
+ url: data.url,
+ });
+ })}
+ >
+ <div className="bg-neutral-50 p-4 sm:p-6">
+ <div className="space-y-4">
+ <div>
+ <label
+ htmlFor="url"
+ className="mb-1 block text-sm font-medium text-neutral-700"
+ >
+ URL
+ </label>
+
+ <input
+ id="url"
+ type="url"
+ placeholder="https://getacme.link"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 shadow-sm focus:border-neutral-500 focus:ring-neutral-500 sm:text-sm",
+ errors.url &&
+ "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500",
+ )}
+ {...register("url", { required: "URL is required" })}
+ />
+
+ {errors.url && (
+ <p className="mt-1 text-xs text-red-600">
+ {errors.url.message}
+ </p>
+ )}
+ </div>
+
+ <div>
+ <label
+ htmlFor="name"
+ className="mb-1 block text-sm font-medium text-neutral-700"
+ >
+ Display name
+ </label>
+
+ <input
+ id="name"
+ type="text"
+ placeholder="Brand guidelines"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 shadow-sm focus:border-neutral-500 focus:ring-neutral-500 sm:text-sm",
+ errors.name &&
+ "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500",
+ )}
+ {...register("name", { required: "Display name is required" })}
+ />
+
+ {errors.name ? (
+ <p className="mt-1 text-xs text-red-600">
+ {errors.name.message}
+ </p>
+ ) : (
+ <p className="mt-2 text-xs text-neutral-500">
+ The name that shows to partners
+ </p>
+ )}
+ </div>
+ </div>
+ </div>
+
+ <div className="flex items-center justify-end gap-2 border-t border-neutral-200 bg-neutral-50 px-4 py-5 sm:px-6">
+ <Button
+ onClick={() => setShowAddLinkModal(false)}
+ variant="secondary"
+ text="Cancel"
+ className="h-8 w-fit px-3"
+ type="button"
+ />
+ <Button
+ type="submit"
+ autoFocus
+ loading={isSubmitting || isSubmitSuccessful}
+ text="Add link"
+ className="h-8 w-fit px-3"
+ />
+ </div>
+ </form>
+ </>
+ );
+}
+
+export function useAddLinkModal() {
+ const [showAddLinkModal, setShowAddLinkModal] = useState(false);
+
+ const AddLinkModalCallback = useCallback(() => {
+ return (
+ <AddLinkModal
+ showAddLinkModal={showAddLinkModal}
+ setShowAddLinkModal={setShowAddLinkModal}
+ />
+ );
+ }, [showAddLinkModal, setShowAddLinkModal]);
+
+ return useMemo(
+ () => ({
+ setShowAddLinkModal,
+ AddLinkModal: AddLinkModalCallback,
+ }),
+ [setShowAddLinkModal, AddLinkModalCallback],
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/index.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/index.tsx
index 52af418f76..c69f0bd087 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/index.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/resources/program-brand-assets/index.tsx
@@ -6,21 +6,27 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { ProgramResourceType } from "@/lib/zod/schemas/program-resources";
import { ResourceCard } from "@/ui/partners/resources/resource-card";
import { AnimatedSizeContainer, Button, FileContent } from "@dub/ui";
-import { capitalize, formatFileSize } from "@dub/utils";
+import {
+ capitalize,
+ formatFileSize,
+ getApexDomain,
+ GOOGLE_FAVICON_URL,
+} from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
import { toast } from "sonner";
import { SettingsRow } from "../../program-settings-row";
import { useAddColorModal } from "./add-color-modal";
import { useAddFileModal } from "./add-file-modal";
+import { useAddLinkModal } from "./add-link-modal";
import { useAddLogoModal } from "./add-logo-modal";
export function ProgramBrandAssets() {
const { id: workspaceId } = useWorkspace();
-
- const { resources, mutate, isLoading, isValidating } = useProgramResources();
+ const { resources, mutate, isLoading } = useProgramResources();
const { setShowAddLogoModal, AddLogoModal } = useAddLogoModal();
const { setShowAddColorModal, AddColorModal } = useAddColorModal();
const { setShowAddFileModal, AddFileModal } = useAddFileModal();
+ const { setShowAddLinkModal, AddLinkModal } = useAddLinkModal();
const { executeAsync } = useAction(deleteProgramResourceAction, {
onSuccess: ({ input }) => {
@@ -52,6 +58,7 @@ export function ProgramBrandAssets() {
<AddLogoModal />
<AddColorModal />
<AddFileModal />
+ <AddLinkModal />
<div className="rounded-lg border border-neutral-200 bg-white">
<div className="p-6">
<h2 className="inline-flex items-center gap-2 text-lg font-semibold text-neutral-900">
@@ -150,6 +157,51 @@ export function ProgramBrandAssets() {
</div>
</SettingsRow>
+ <SettingsRow
+ heading="Links"
+ description="Provide any additional links helpful to your partners"
+ >
+ <div className="flex flex-col gap-3">
+ <div className="flex items-center justify-end">
+ <Button
+ text="Add Link"
+ className="h-8 w-fit px-3"
+ onClick={() => setShowAddLinkModal(true)}
+ loading={isLoading}
+ />
+ </div>
+ <AnimatedSizeContainer
+ height
+ transition={{ duration: 0.2, ease: "easeInOut" }}
+ >
+ {resources?.links && resources.links.length > 0 && (
+ <div className="grid gap-2">
+ {resources?.links?.map((link) => (
+ <ResourceCard
+ key={link.id}
+ resourceType="link"
+ icon={
+ <div className="flex size-full items-center justify-center bg-neutral-50">
+ <img
+ src={`${GOOGLE_FAVICON_URL}${getApexDomain(link.url)}`}
+ alt="favicon"
+ className="size-6 rounded-full object-contain"
+ />
+ </div>
+ }
+ title={link.name}
+ description={link.url}
+ visitUrl={link.url}
+ copyText={link.url}
+ onDelete={() => handleDelete("link", link.id)}
+ />
+ ))}
+ </div>
+ )}
+ </AnimatedSizeContainer>
+ </div>
+ </SettingsRow>
+
<SettingsRow
heading="Additional Files"
description="Any document or zip file, max size 10 MB"
diff --git a/apps/web/lib/actions/partners/program-resources/add-program-resource.ts b/apps/web/lib/actions/partners/program-resources/add-program-resource.ts
index 30e0f340d8..cb82a16a32 100644
--- a/apps/web/lib/actions/partners/program-resources/add-program-resource.ts
+++ b/apps/web/lib/actions/partners/program-resources/add-program-resource.ts
@@ -7,6 +7,7 @@ import { uploadedImageAllowSVGSchema } from "@/lib/zod/schemas/misc";
import {
programResourceColorSchema,
programResourceFileSchema,
+ programResourceLinkSchema,
} from "@/lib/zod/schemas/program-resources";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
@@ -40,11 +41,18 @@ const colorResourceSchema = baseResourceSchema.extend({
color: z.string(), // Hex color code
});
+// Schema for link resources
+const linkResourceSchema = baseResourceSchema.extend({
+ resourceType: z.literal("link"),
+ url: z.string().url(),
+});
+
// Combined schema that can handle any resource type
const addResourceSchema = z.discriminatedUnion("resourceType", [
logoResourceSchema,
fileResourceSchema,
colorResourceSchema,
+ linkResourceSchema,
]);
export const addProgramResourceAction = authActionClient
@@ -72,6 +80,7 @@ export const addProgramResourceAction = authActionClient
logos: [],
colors: [],
files: [],
+ links: [],
};
const updatedResources = { ...currentResources };
@@ -137,6 +146,16 @@ export const addProgramResourceAction = authActionClient
...(updatedResources.colors || []),
newResource,
];
+ } else if (resourceType === "link") {
+ const { url } = parsedInput;
+
+ const newResource = programResourceLinkSchema.parse({
+ id: createId({ prefix: "pgr_" }),
+ name,
+ url,
+ });
+
+ updatedResources.links = [...(updatedResources.links || []), newResource];
} else {
throw new Error("Invalid resource type");
}
diff --git a/apps/web/lib/zod/schemas/program-resources.ts b/apps/web/lib/zod/schemas/program-resources.ts
index 6746bbab9d..58ab2bf0bb 100644
--- a/apps/web/lib/zod/schemas/program-resources.ts
+++ b/apps/web/lib/zod/schemas/program-resources.ts
@@ -1,6 +1,6 @@
import { z } from "zod";
-export const PROGRAM_RESOURCE_TYPES = ["logo", "file", "color"] as const;
+export const PROGRAM_RESOURCE_TYPES = ["logo", "file", "color", "link"] as const;
export type ProgramResourceType = (typeof PROGRAM_RESOURCE_TYPES)[number];
@@ -17,8 +17,15 @@ export const programResourceColorSchema = z.object({
color: z.string(),
});
+export const programResourceLinkSchema = z.object({
+ id: z.string(),
+ name: z.string(),
+ url: z.string().url(),
+});
+
export const programResourcesSchema = z.object({
logos: z.array(programResourceFileSchema),
colors: z.array(programResourceColorSchema),
files: z.array(programResourceFileSchema),
+ links: z.array(programResourceLinkSchema),
});
diff --git a/apps/web/ui/partners/resources/resource-card.tsx b/apps/web/ui/partners/resources/resource-card.tsx
index bd93114921..b1fb654237 100644
--- a/apps/web/ui/partners/resources/resource-card.tsx
+++ b/apps/web/ui/partners/resources/resource-card.tsx
@@ -1,7 +1,7 @@
"use client";
import { ProgramResourceType } from "@/lib/zod/schemas/program-resources";
-import { ThreeDots } from "@/ui/shared/icons";
+import { Link, ThreeDots } from "@/ui/shared/icons";
import {
Button,
buttonVariants,
@@ -37,6 +37,7 @@ export function ResourceCard({
onDelete,
downloadUrl,
copyText,
+ visitUrl,
}: {
resourceType: ProgramResourceType;
title: string;
@@ -45,6 +46,7 @@ export function ResourceCard({
onDelete?: () => Promise<boolean>;
downloadUrl?: string;
copyText?: string;
+ visitUrl?: string;
}) {
const [openPopover, setOpenPopover] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
@@ -52,7 +54,7 @@ export function ResourceCard({
const [copied, copyToClipboard] = useCopyToClipboard();
return (
- <div className="border-border-subtle flex w-full items-center justify-between gap-4 rounded-lg border p-4 shadow-sm">
+ <div className="border-border-subtle flex w-full items-center justify-between gap-4 overflow-hidden rounded-lg border p-4 shadow-sm">
<div className="flex min-w-0 items-center gap-4">
<div className="border-border-subtle flex size-10 shrink-0 items-center justify-center overflow-hidden rounded-md border">
{icon}
@@ -67,7 +69,7 @@ export function ResourceCard({
</div>
</div>
<div className="relative">
- {onDelete || (downloadUrl && copyText) ? (
+ {onDelete || (downloadUrl && copyText) || visitUrl ? (
<Popover
content={
<div className="grid w-full grid-cols-1 gap-px p-2 sm:w-48">
@@ -91,6 +93,23 @@ export function ResourceCard({
Download
</a>
)}
+
+ {visitUrl && (
+ <a
+ href={visitUrl}
+ target="_blank"
+ rel="noopener noreferrer"
+ className={cn(
+ buttonVariants({ variant: "outline" }),
+ "flex h-9 items-center justify-start gap-2 rounded-md px-2 text-sm font-medium",
+ )}
+ onClick={() => setOpenPopover(false)}
+ >
+ <Link className="size-4" />
+ Visit
+ </a>
+ )}
+
{copyText && (
<Button
text="Copy"