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.

File uploads

Tim Wilsone44a47e8ec32
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index d753680a6e..0b9b9169f4 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -29,7 +29,7 @@ export const PATCH = withWorkspace(async ({ workspace, params, req }) => {
const { bountyId } = params;
const programId = getDefaultProgramIdOrThrow(workspace);
- // TODO: [bounties] Persist performance logic to workflow and groupIds to bountyGroup
+ // TODO: [bounties] Persist performance logic to workflow
const { performanceCondition, groupIds, ...data } = updateBountySchema.parse(
await parseRequestBody(req),
);
diff --git a/apps/web/lib/actions/partners/upload-bounty-submission-file.ts b/apps/web/lib/actions/partners/upload-bounty-submission-file.ts
new file mode 100644
index 0000000000..305a4926e5
--- /dev/null
+++ b/apps/web/lib/actions/partners/upload-bounty-submission-file.ts
@@ -0,0 +1,35 @@
+"use server";
+
+import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
+import { storage } from "@/lib/storage";
+import { nanoid, R2_URL } from "@dub/utils";
+import { z } from "zod";
+import { authPartnerActionClient } from "../safe-action";
+
+const schema = z.object({
+ programId: z.string(),
+});
+
+export const uploadBountySubmissionFileAction = authPartnerActionClient
+ .schema(schema)
+ .action(async ({ ctx, parsedInput }) => {
+ const { partner } = ctx;
+ const { program } = await getProgramEnrollmentOrThrow({
+ partnerId: partner.id,
+ programId: parsedInput.programId,
+ });
+
+ try {
+ const key = `programs/${program.id}/bounties/submissions/${partner.id}/image_${nanoid(7)}`;
+
+ const signedUrl = await storage.getSignedUrl(key);
+
+ return {
+ key,
+ signedUrl,
+ destinationUrl: `${R2_URL}/${key}`,
+ };
+ } catch (e) {
+ throw new Error("Failed to get signed URL for upload.");
+ }
+ });
diff --git a/apps/web/ui/partners/bounties/claim-bounty-modal.tsx b/apps/web/ui/partners/bounties/claim-bounty-modal.tsx
index 1b33bef45a..e72a380be7 100644
--- a/apps/web/ui/partners/bounties/claim-bounty-modal.tsx
+++ b/apps/web/ui/partners/bounties/claim-bounty-modal.tsx
@@ -1,8 +1,21 @@
-import useWorkspace from "@/lib/swr/use-workspace";
+import { uploadBountySubmissionFileAction } from "@/lib/actions/partners/upload-bounty-submission-file";
+import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
import { BountyProps } from "@/lib/types";
-import { Button, Calendar6, Modal, useRouterStuff } from "@dub/ui";
-import { formatDate } from "@dub/utils";
+import { X } from "@/ui/shared/icons";
+import {
+ Button,
+ Calendar6,
+ FileUpload,
+ LoadingSpinner,
+ Modal,
+ useRouterStuff,
+} from "@dub/ui";
+import { cn, formatDate } from "@dub/utils";
+import { motion } from "framer-motion";
+import { useAction } from "next-safe-action/hooks";
import { Dispatch, SetStateAction, useState } from "react";
+import { toast } from "sonner";
+import { v4 as uuid } from "uuid";
import { BountyThumbnailImage } from "./bounty-thumbnail-image";
type ClaimBountyModalProps = {
@@ -14,11 +27,61 @@ function ClaimBountyModalContent({
setShowModal,
bounty,
}: ClaimBountyModalProps) {
- const { id: workspaceId } = useWorkspace();
+ const { programEnrollment } = useProgramEnrollment();
+
+ const [isFormOpen, setIsFormOpen] = useState(false);
+
+ const [files, setFiles] = useState<
+ {
+ id: string;
+ file?: File;
+ url?: string;
+ uploading: boolean;
+ }[]
+ >([]);
+
+ const { executeAsync } = useAction(uploadBountySubmissionFileAction);
+
+ const handleUpload = async (file: File) => {
+ if (!programEnrollment) return;
+
+ setFiles((prev) => [...prev, { id: uuid(), file, uploading: true }]);
+
+ // TODO: Partners upload URL
+ const result = await executeAsync({
+ programId: programEnrollment.programId,
+ });
+
+ if (!result?.data) throw new Error("Failed to get signed upload URL");
+
+ const { signedUrl, destinationUrl } = result.data;
+
+ const uploadResponse = await fetch(signedUrl, {
+ method: "PUT",
+ body: file,
+ headers: {
+ "Content-Type": file.type,
+ "Content-Length": file.size.toString(),
+ },
+ });
+
+ if (!uploadResponse.ok) {
+ const result = await uploadResponse.json();
+ toast.error(result.error.message || "Failed to upload screenshot.");
+ return;
+ }
+
+ toast.success(`${file.name} uploaded!`);
+ setFiles((prev) =>
+ prev.map((f) =>
+ f.file === file ? { ...f, uploading: false, url: destinationUrl } : f,
+ ),
+ );
+ };
return (
- <div>
- <div className="relative flex h-[132px] items-center justify-center bg-neutral-100 py-3">
+ <div className="scrollbar-hide max-h-[calc(100dvh-50px)] overflow-y-auto">
+ <div className="flex h-[132px] items-center justify-center bg-neutral-100 py-3">
<div className="relative size-full">
<BountyThumbnailImage bounty={bounty} />
</div>
@@ -53,13 +116,107 @@ function ClaimBountyModalContent({
</div>
)}
+ {/* Form */}
+ <motion.div
+ initial={false}
+ animate={{ height: isFormOpen ? "auto" : 0 }}
+ transition={{ duration: 0.15 }}
+ className={cn(
+ "overflow-hidden transition-opacity",
+ !isFormOpen && "opacity-0",
+ )}
+ >
+ <div className="border-border-subtle border-t p-6 max-sm:px-4">
+ <div>
+ <label htmlFor="slug" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-neutral-900">Files</h2>
+ </label>
+ <div
+ className={cn(
+ "mt-2 flex h-12 items-center gap-2 transition-[height]",
+ files.length === 0 && "h-24",
+ )}
+ >
+ {files.map((file, idx) => (
+ <div
+ key={file.id}
+ className="border-border-subtle group relative flex aspect-square h-full items-center justify-center rounded-md border bg-white"
+ >
+ {file.uploading ? (
+ <LoadingSpinner className="size-4" />
+ ) : (
+ <div className="relative size-full overflow-hidden rounded-md">
+ <img src={file.url} alt="object-cover" />
+ </div>
+ )}
+ <span className="sr-only">
+ {file.file?.name || `File ${idx + 1}`}
+ </span>
+ <button
+ type="button"
+ className={cn(
+ "absolute right-0 top-0 flex size-[1.125rem] -translate-y-1/2 translate-x-1/2 items-center justify-center",
+ "rounded-full border border-neutral-200 bg-white shadow-sm hover:bg-neutral-50 active:scale-95",
+ "scale-50 opacity-0 transition-[background-color,transform,opacity] group-hover:scale-100 group-hover:opacity-100",
+ )}
+ onClick={() => {
+ setFiles((prev) => prev.filter((s) => s.id !== file.id));
+ }}
+ >
+ <X className="size-2.5 text-neutral-400" />
+ </button>
+ </div>
+ ))}
+
+ <FileUpload
+ accept="images"
+ className={cn(
+ "border-border-subtle h-full w-auto rounded-md border",
+ files.length > 0 ? "aspect-square" : "aspect-[unset] w-full",
+ )}
+ iconClassName="size-5 shrink-0"
+ variant="plain"
+ content={
+ files.length > 0 ? null : "JPG or PNG, max size of 5MB"
+ }
+ onChange={async ({ file }) => await handleUpload(file)}
+ disabled={files.length >= 4}
+ />
+ </div>
+ </div>
+ </div>
+ </motion.div>
+
+ {/* Action buttons */}
<div className="border-border-subtle border-t p-5">
- <Button
- variant="primary"
- text="Claim bounty"
- className="rounded-lg"
- onClick={() => {}}
- />
+ <div
+ className={cn(
+ "flex items-center transition-all",
+ isFormOpen ? "gap-4" : "gap-0",
+ )}
+ >
+ <div
+ className={cn(
+ "shrink-0 overflow-hidden transition-[width]",
+ isFormOpen ? "w-[calc(50%-1rem)]" : "w-0",
+ )}
+ >
+ <Button
+ variant="secondary"
+ text="Cancel"
+ className="rounded-lg"
+ onClick={() => setIsFormOpen(false)}
+ />
+ </div>
+ <Button
+ variant="primary"
+ text={isFormOpen ? "Submit proof" : "Claim bounty"}
+ className="grow rounded-lg"
+ onClick={
+ isFormOpen ? () => toast.info("WIP") : () => setIsFormOpen(true)
+ }
+ />
+ </div>
</div>
</div>
);