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 additional file management

Tim Wilsonf295c7968abc
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/add-file-modal.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/add-file-modal.tsx
new file mode 100644
index 0000000000..8d3f88ff92
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/add-file-modal.tsx
@@ -0,0 +1,222 @@
+"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, FileContent, FileUpload, Modal } from "@dub/ui";
+import { cn, truncate } from "@dub/utils";
+import { useAction } from "next-safe-action/hooks";
+import { useParams } from "next/navigation";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
+import { Controller, useForm } from "react-hook-form";
+import { toast } from "sonner";
+import { z } from "zod";
+
+type AddFileModalProps = {
+ showAddFileModal: boolean;
+ setShowAddFileModal: Dispatch<SetStateAction<boolean>>;
+};
+
+// Define the form schema
+const fileFormSchema = z.object({
+ name: z.string().min(1, "Name is required"),
+ file: z.string().min(1, "File is required"),
+});
+
+type FileFormData = z.infer<typeof fileFormSchema>;
+
+function AddFileModal(props: AddFileModalProps) {
+ return (
+ <Modal
+ showModal={props.showAddFileModal}
+ setShowModal={props.setShowAddFileModal}
+ >
+ <AddFileModalInner {...props} />
+ </Modal>
+ );
+}
+
+function AddFileModalInner({ setShowAddFileModal }: AddFileModalProps) {
+ const { programId } = useParams();
+ const { id: workspaceId } = useWorkspace();
+ const { mutate } = useProgramResources();
+
+ const {
+ register,
+ handleSubmit,
+ control,
+ setValue,
+ getValues,
+ setError,
+ formState: { errors, isSubmitting, isSubmitSuccessful },
+ } = useForm<FileFormData>({
+ defaultValues: {
+ name: "",
+ file: "",
+ },
+ });
+
+ const [fileName, setFileName] = useState("");
+
+ const { executeAsync } = useAction(addProgramResourceAction, {
+ onSuccess: () => {
+ mutate();
+ setShowAddFileModal(false);
+ toast.success("File added successfully!");
+ },
+ onError({ error }) {
+ if (error.serverError) {
+ setError("root.serverError", {
+ message: error.serverError,
+ });
+ toast.error(error.serverError);
+ } else {
+ toast.error("Failed to add file");
+ }
+ },
+ });
+
+ 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 file</h3>
+ </div>
+
+ <form
+ onSubmit={handleSubmit(async (data: FileFormData) => {
+ await executeAsync({
+ workspaceId: workspaceId!,
+ programId: programId as string,
+ name: data.name,
+ resourceType: "file",
+ file: data.file,
+ });
+ })}
+ >
+ <div className="bg-neutral-50 p-4 sm:p-6">
+ <div className="space-y-4">
+ <div>
+ <label
+ htmlFor="file-upload"
+ className="mb-1 block text-sm font-medium text-neutral-700"
+ >
+ File
+ </label>
+ <Controller
+ control={control}
+ name="file"
+ rules={{ required: "File is required" }}
+ render={({ field }) => (
+ <FileUpload
+ accept="documents"
+ className={cn(
+ "aspect-[4.2] w-full rounded-md border border-neutral-300",
+ errors.file && "border-red-300 ring-1 ring-red-500",
+ )}
+ iconClassName="size-5"
+ variant="plain"
+ readFile
+ onChange={({ file, src }) => {
+ setFileName(file.name);
+ field.onChange(src);
+
+ // Set the file name to the file name without extension if no name is provided
+ const currentName = getValues("name");
+ if (!currentName && file.name) {
+ const nameWithoutExtension = file.name
+ .split(".")
+ .slice(0, -1)
+ .join(".");
+ setValue("name", nameWithoutExtension || file.name);
+ }
+ }}
+ icon={field.value ? FileContent : undefined}
+ content={
+ field.value
+ ? truncate(fileName, 25)
+ : "Any document file format, max size of 10MB"
+ }
+ maxFileSizeMB={10}
+ />
+ )}
+ />
+ {errors.file && (
+ <p className="mt-1 text-xs text-red-600">
+ {errors.file.message}
+ </p>
+ )}
+ </div>
+
+ <div>
+ <label
+ htmlFor="name"
+ className="mb-1 block text-sm font-medium text-neutral-700"
+ >
+ File name
+ </label>
+ <input
+ id="name"
+ type="text"
+ 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: "Name is required" })}
+ />
+ {errors.name && (
+ <p className="mt-1 text-xs text-red-600">
+ {errors.name.message}
+ </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={() => setShowAddFileModal(false)}
+ variant="secondary"
+ text="Cancel"
+ className="h-8 w-fit px-3"
+ type="button"
+ />
+ <Button
+ type="submit"
+ autoFocus
+ loading={isSubmitting || isSubmitSuccessful}
+ text="Add File"
+ className="h-8 w-fit px-3"
+ />
+ </div>
+ </form>
+ </>
+ );
+}
+
+export function useAddFileModal() {
+ const [showAddFileModal, setShowAddFileModal] = useState(false);
+
+ const AddFileModalCallback = useCallback(() => {
+ return (
+ <AddFileModal
+ showAddFileModal={showAddFileModal}
+ setShowAddFileModal={setShowAddFileModal}
+ />
+ );
+ }, [showAddFileModal, setShowAddFileModal]);
+
+ return useMemo(
+ () => ({
+ setShowAddFileModal,
+ AddFileModal: AddFileModalCallback,
+ }),
+ [setShowAddFileModal, AddFileModalCallback],
+ );
+}
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 09d3356d5d..fb6f55bb74 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
@@ -8,6 +8,7 @@ import { ThreeDots } from "@/ui/shared/icons";
import {
AnimatedSizeContainer,
Button,
+ FileContent,
LoadingSpinner,
Popover,
Trash,
@@ -18,6 +19,7 @@ import { useParams } from "next/navigation";
import { PropsWithChildren, ReactNode, useState } from "react";
import { toast } from "sonner";
import { useAddColorModal } from "./add-color-modal";
+import { useAddFileModal } from "./add-file-modal";
import { useAddLogoModal } from "./add-logo-modal";
export function ProgramResourcesPageClient() {
@@ -27,6 +29,7 @@ export function ProgramResourcesPageClient() {
const { resources, mutate } = useProgramResources();
const { setShowAddLogoModal, AddLogoModal } = useAddLogoModal();
const { setShowAddColorModal, AddColorModal } = useAddColorModal();
+ const { setShowAddFileModal, AddFileModal } = useAddFileModal();
const { executeAsync } = useAction(deleteProgramResourceAction, {
onSuccess: ({ input }) => {
@@ -58,6 +61,7 @@ export function ProgramResourcesPageClient() {
<>
<AddLogoModal />
<AddColorModal />
+ <AddFileModal />
<div className="flex flex-col gap-10">
<Section
resource="logo"
@@ -110,7 +114,23 @@ export function ProgramResourcesPageClient() {
resource="file"
title="Additional files"
description="Any document file format, max size 10 MB"
- ></Section>
+ onAdd={() => setShowAddFileModal(true)}
+ >
+ {resources?.files?.map((file) => (
+ <ResourceCard
+ key={file.id}
+ resourceType="file"
+ icon={
+ <div className="flex size-full items-center justify-center bg-neutral-50">
+ <FileContent className="size-4 text-neutral-800" />
+ </div>
+ }
+ title={file.name || "File"}
+ description={formatFileSize(file.size, 0)}
+ onDelete={() => handleDelete("file", file.id)}
+ />
+ ))}
+ </Section>
</div>
</>
);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page.tsx
index b5ca48a330..0109a5f495 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page.tsx
@@ -8,7 +8,7 @@ export default function ProgramResources() {
title="Resources"
description="Shared brand logos, colors, and additional documents for your partners"
>
- <MaxWidthWrapper>
+ <MaxWidthWrapper className="mb-10">
<ProgramResourcesPageClient />
</MaxWidthWrapper>
</PageContent>
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 7b4dcf253a..dbf22419f8 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
@@ -78,6 +78,9 @@ export const addProgramResourceAction = authActionClient
const base64Data = file.replace(/^data:.+;base64,/, "");
const fileSize = Math.ceil((base64Data.length * 3) / 4);
+ if (fileSize / 1024 / 1024 > 10)
+ throw new Error("File size is too large");
+
const newResource = programResourceFileSchema.parse({
id: nanoid(10),
name,
diff --git a/packages/ui/src/file-upload.tsx b/packages/ui/src/file-upload.tsx
index 3f5792d394..237a30c36b 100644
--- a/packages/ui/src/file-upload.tsx
+++ b/packages/ui/src/file-upload.tsx
@@ -4,7 +4,7 @@ import { DragEvent, ReactNode, useState } from "react";
import { toast } from "sonner";
import { CloudUpload, Icon, LoadingCircle } from "./icons";
-type AcceptedFileFormats = "any" | "images" | "csv";
+type AcceptedFileFormats = "any" | "images" | "csv" | "documents";
const acceptFileTypes: Record<
AcceptedFileFormats,
@@ -19,6 +19,18 @@ const acceptFileTypes: Record<
types: ["text/csv"],
errorMessage: "File type not supported (.csv only)",
},
+ documents: {
+ types: [
+ "application/pdf", // .pdf
+ "text/plain", // .txt
+ "application/msword", // .doc
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .docx
+ "application/vnd.ms-excel", // .xls
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xlsx
+ "text/csv", // .csv
+ ],
+ errorMessage: "File type not supported (document files only)",
+ },
};
const imageUploadVariants = cva(
diff --git a/packages/ui/src/icons/nucleo/file-content.tsx b/packages/ui/src/icons/nucleo/file-content.tsx
new file mode 100644
index 0000000000..f07c1b370a
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/file-content.tsx
@@ -0,0 +1,65 @@
+import { SVGProps } from "react";
+
+export function FileContent(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ {...props}
+ >
+ <g fill="currentColor">
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="5.75"
+ x2="7.75"
+ y1="6.75"
+ y2="6.75"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="5.75"
+ x2="12.25"
+ y1="9.75"
+ y2="9.75"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="5.75"
+ x2="12.25"
+ y1="12.75"
+ y2="12.75"
+ />
+ <path
+ d="M2.75,14.25V3.75c0-1.105,.895-2,2-2h5.586c.265,0,.52,.105,.707,.293l3.914,3.914c.188,.188,.293,.442,.293,.707v7.586c0,1.105-.895,2-2,2H4.75c-1.105,0-2-.895-2-2Z"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <path
+ d="M15.16,6.25h-3.41c-.552,0-1-.448-1-1V1.852"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index 85557ac649..2cdc89882f 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -73,6 +73,7 @@ export * from "./envelope-arrow-right";
export * from "./eye";
export * from "./eye-slash";
export * from "./feather-fill";
+export * from "./file-content";
export * from "./file-zip2";
export * from "./filter-bars";
export * from "./filter2";