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 image + accordion

Tim Wilson88421358154a
diff --git a/apps/web/lib/actions/partners/upload-lander-image.ts b/apps/web/lib/actions/partners/upload-lander-image.ts
new file mode 100644
index 0000000000..e254e65527
--- /dev/null
+++ b/apps/web/lib/actions/partners/upload-lander-image.ts
@@ -0,0 +1,38 @@
+"use server";
+
+import { storage } from "@/lib/storage";
+import { nanoid, R2_URL } from "@dub/utils";
+import { z } from "zod";
+import { getProgramOrThrow } from "../../api/programs/get-program-or-throw";
+import { authActionClient } from "../safe-action";
+
+const schema = z.object({
+ workspaceId: z.string(),
+ programId: z.string(),
+});
+
+export const uploadLanderImageAction = authActionClient
+ .schema(schema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace } = ctx;
+ const { programId } = parsedInput;
+
+ try {
+ await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
+
+ const key = `programs/${programId}/lander/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/design/edit-list.tsx b/apps/web/ui/partners/design/edit-list.tsx
new file mode 100644
index 0000000000..02e18c4b23
--- /dev/null
+++ b/apps/web/ui/partners/design/edit-list.tsx
@@ -0,0 +1,7 @@
+"use client"
+
+export function EditList() {
+ return (
+
+ )
+}
\ No newline at end of file
diff --git a/apps/web/ui/partners/design/modals/accordion-block-modal.tsx b/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
index 210b371273..22aab613dc 100644
--- a/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
+++ b/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
@@ -1,3 +1,125 @@
+"use client";
+
+import { programLanderAccordionBlockSchema } from "@/lib/zod/schemas/program-lander";
+import { Button, Modal, useMediaQuery } from "@dub/ui";
+import { Dispatch, SetStateAction, useId } from "react";
+import { useFieldArray, useForm } from "react-hook-form";
+import { z } from "zod";
+
+type AccordionBlockData = z.infer<
+ typeof programLanderAccordionBlockSchema
+>["data"];
+
+type AccordionBlockModalProps = {
+ showModal: boolean;
+ setShowModal: Dispatch<SetStateAction<boolean>>;
+ defaultValues?: Partial<AccordionBlockData>;
+ onSubmit: (data: AccordionBlockData) => void;
+};
+
+export function AccordionBlockModal(props: AccordionBlockModalProps) {
+ return (
+ <Modal showModal={props.showModal} setShowModal={props.setShowModal}>
+ <AccordionBlockModalInner {...props} />
+ </Modal>
+ );
+}
+
+function AccordionBlockModalInner({
+ setShowModal,
+ onSubmit,
+ defaultValues,
+}: AccordionBlockModalProps) {
+ const id = useId();
+ const { isMobile } = useMediaQuery();
+ const { handleSubmit, register, control } = useForm<AccordionBlockData>({
+ defaultValues,
+ });
+
+ const { fields, append, prepend, remove, swap, move, insert } = useFieldArray(
+ {
+ control,
+ name: "items",
+ },
+ );
+
+ return (
+ <>
+ <div className="p-4 pt-3">
+ <h3 className="text-base font-semibold leading-6 text-neutral-800">
+ Add Accordion
+ </h3>
+ <form
+ className="mt-4 flex flex-col gap-6"
+ onSubmit={(e) => {
+ e.stopPropagation();
+ handleSubmit(async (data) => {
+ setShowModal(false);
+ onSubmit(data);
+ })(e);
+ }}
+ >
+ {/* Title */}
+ <div>
+ <label
+ htmlFor={`${id}-title`}
+ className="flex items-center gap-2 text-sm font-medium text-neutral-700"
+ >
+ Section heading
+ </label>
+ <div className="mt-2 rounded-md shadow-sm">
+ <input
+ id={`${id}-title`}
+ type="text"
+ placeholder="Title"
+ autoFocus={!isMobile}
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("title")}
+ />
+ </div>
+ </div>
+
+ <div>
+ {fields.map((field, index) => (
+ <div key={field.id}>
+ <input {...register(`items.${index}.title`)} />
+ <input {...register(`items.${index}.content`)} />
+ </div>
+ ))}
+ <button
+ type="button"
+ onClick={() =>
+ append({
+ id: crypto.randomUUID(),
+ title: "title",
+ content: "content",
+ })
+ }
+ >
+ add
+ </button>
+ </div>
+
+ <div className="flex items-center justify-end gap-2">
+ <Button
+ onClick={() => setShowModal(false)}
+ variant="secondary"
+ text="Cancel"
+ className="h-8 w-fit px-3"
+ />
+ <Button
+ type="submit"
+ variant="primary"
+ text="Add"
+ className="h-8 w-fit px-3"
+ />
+ </div>
+ </form>
+ </div>
+ </>
+ );
+}
+
export function AccordionBlockThumbnail() {
return (
<svg
diff --git a/apps/web/ui/partners/design/modals/add-block-modal.tsx b/apps/web/ui/partners/design/modals/add-block-modal.tsx
index 6c3b81bf09..7fd350c4a3 100644
--- a/apps/web/ui/partners/design/modals/add-block-modal.tsx
+++ b/apps/web/ui/partners/design/modals/add-block-modal.tsx
@@ -7,9 +7,12 @@ import { useWatch } from "react-hook-form";
import { v4 as uuid } from "uuid";
import { z } from "zod";
import { useBrandingFormContext } from "../branding-form";
-import { AccordionBlockThumbnail } from "./accordion-block-modal";
+import {
+ AccordionBlockModal,
+ AccordionBlockThumbnail,
+} from "./accordion-block-modal";
import { FilesBlockThumbnail } from "./files-block-modal";
-import { ImageBlockThumbnail } from "./image-block-modal";
+import { ImageBlockModal, ImageBlockThumbnail } from "./image-block-modal";
import { TextBlockModal, TextBlockThumbnail } from "./text-block-modal";
type AddBlockModalProps = {
@@ -47,7 +50,7 @@ const Blocks: {
id: "image",
label: "Image",
description: "Add nice visuals to accompany your content",
- modal: TextBlockModal,
+ modal: ImageBlockModal,
thumbnail: <ImageBlockThumbnail />,
},
{
@@ -61,7 +64,7 @@ const Blocks: {
id: "accordion",
label: "Accordion",
description: "Expanding and collapsing, great for FAQs",
- modal: TextBlockModal,
+ modal: AccordionBlockModal,
thumbnail: <AccordionBlockThumbnail />,
},
];
diff --git a/apps/web/ui/partners/design/modals/image-block-modal.tsx b/apps/web/ui/partners/design/modals/image-block-modal.tsx
index 68605609b1..f6280576b6 100644
--- a/apps/web/ui/partners/design/modals/image-block-modal.tsx
+++ b/apps/web/ui/partners/design/modals/image-block-modal.tsx
@@ -1,13 +1,16 @@
-import { Button, Modal, useEnterSubmit, useMediaQuery } from "@dub/ui";
-import { Dispatch, SetStateAction, useId } from "react";
-import { useForm } from "react-hook-form";
-
-type ImageBlockFormData = {
- url: string;
- alt: string;
- width: number;
- height: number;
-};
+import { uploadLanderImageAction } from "@/lib/actions/partners/upload-lander-image";
+import useProgram from "@/lib/swr/use-program";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { programLanderImageBlockSchema } from "@/lib/zod/schemas/program-lander";
+import { Button, FileUpload, Modal } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { useAction } from "next-safe-action/hooks";
+import { Dispatch, SetStateAction, useId, useState } from "react";
+import { Controller, useForm } from "react-hook-form";
+import { toast } from "sonner";
+import { z } from "zod";
+
+type ImageBlockFormData = z.infer<typeof programLanderImageBlockSchema>["data"];
type ImageBlockModalProps = {
showModal: boolean;
@@ -30,12 +33,57 @@ function ImageBlockModalInner({
defaultValues,
}: ImageBlockModalProps) {
const id = useId();
- const { isMobile } = useMediaQuery();
- const { handleSubmit, register } = useForm<ImageBlockFormData>({
+
+ const { id: workspaceId } = useWorkspace();
+ const { program } = useProgram();
+
+ const {
+ handleSubmit,
+ register,
+ control,
+ setValue,
+ formState: { errors, isSubmitting, isSubmitSuccessful },
+ } = useForm<ImageBlockFormData>({
defaultValues,
});
- const { handleKeyDown } = useEnterSubmit();
+ const [isUploading, setIsUploading] = useState(false);
+
+ const { executeAsync } = useAction(uploadLanderImageAction);
+
+ // Handle logo upload
+ const handleUpload = async (file: File) => {
+ setIsUploading(true);
+
+ try {
+ const result = await executeAsync({
+ workspaceId: workspaceId!,
+ programId: program!.id,
+ });
+
+ 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) throw new Error("Failed to upload to signed URL");
+
+ setValue("url", destinationUrl, { shouldDirty: true });
+ } catch (e) {
+ toast.error("Failed to upload image");
+ console.error("Failed to upload image", e);
+ } finally {
+ setIsUploading(false);
+ }
+ };
return (
<>
@@ -49,11 +97,78 @@ function ImageBlockModalInner({
e.stopPropagation();
handleSubmit(async (data) => {
setShowModal(false);
+
+ // Try to get the image dimensions
+ try {
+ const [width, height] = await new Promise<[number, number]>(
+ (resolve, reject) => {
+ const image = new Image();
+ image.src = data.url;
+ image.onload = () => resolve([image.width, image.height]);
+ image.onerror = () => reject();
+ },
+ );
+
+ data.width = width;
+ data.height = height;
+ } catch (e) {
+ console.error("Failed to get image dimensions for", data.url);
+ }
+
onSubmit(data);
})(e);
}}
>
- WIP
+ <div>
+ <label
+ htmlFor="logo-file"
+ className="mb-2 block text-sm font-medium text-neutral-700"
+ >
+ Image
+ </label>
+ <Controller
+ control={control}
+ name="url"
+ rules={{ required: true }}
+ render={({ field }) => (
+ <FileUpload
+ accept="programResourceImages"
+ className={cn(
+ "aspect-[4.2] w-full rounded-md border border-neutral-300",
+ errors.url && "border-red-300 ring-1 ring-red-500",
+ )}
+ iconClassName="size-5"
+ previewClassName="object-contain"
+ variant="plain"
+ imageSrc={field.value}
+ readFile
+ loading={isUploading}
+ onChange={({ file }) => handleUpload(file)}
+ content="SVG, JPG, or PNG, max size of 10MB"
+ maxFileSizeMB={10}
+ />
+ )}
+ />
+ {errors.url && (
+ <p className="mt-1 text-xs text-red-600">{errors.url.message}</p>
+ )}
+ </div>
+
+ <div>
+ <label
+ htmlFor={`${id}-alt`}
+ className="mb-2 block text-sm font-medium text-neutral-700"
+ >
+ Alt text
+ </label>
+ <input
+ id={`${id}-alt`}
+ type="text"
+ className="block w-full rounded-md border-neutral-300 shadow-sm focus:border-neutral-500 focus:ring-neutral-500 sm:text-sm"
+ {...register("alt")}
+ />
+ </div>
+
<div className="flex items-center justify-end gap-2">
<Button
onClick={() => setShowModal(false)}
@@ -66,6 +181,8 @@ function ImageBlockModalInner({
variant="primary"
text="Add"
className="h-8 w-fit px-3"
+ disabled={isUploading}
+ loading={isSubmitting || isSubmitSuccessful}
/>
</div>
</form>