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.

FileUpload

Steven Teyb93782e07254
diff --git a/apps/web/ui/domains/add-edit-domain-form.tsx b/apps/web/ui/domains/add-edit-domain-form.tsx
index 85a067e171..1e109f29ad 100644
--- a/apps/web/ui/domains/add-edit-domain-form.tsx
+++ b/apps/web/ui/domains/add-edit-domain-form.tsx
@@ -7,8 +7,10 @@ import {
AnimatedSizeContainer,
Badge,
Button,
+ FileUpload,
InfoTooltip,
LoadingSpinner,
+ ShimmerDots,
SimpleTooltipContent,
Switch,
useMediaQuery,
@@ -24,11 +26,12 @@ import {
} from "lucide-react";
import posthog from "posthog-js";
import { useMemo, useState } from "react";
-import { useForm } from "react-hook-form";
+import { Controller, useForm } from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
import { useDebouncedCallback } from "use-debounce";
import { z } from "zod";
+import { QRCode } from "../shared/qr-code";
const domainFormSchema = z.object({
slug: z.string().min(1, "Domain is required"),
@@ -98,6 +101,7 @@ export function AddEditDomainForm({
const {
register,
+ control,
handleSubmit,
watch,
setValue,
@@ -351,13 +355,49 @@ export function AddEditDomainForm({
className="-m-1 overflow-hidden p-1"
>
<div className="relative mt-2 rounded-md shadow-sm">
- <input
- {...register(
- id as keyof z.infer<typeof domainFormSchema>,
- )}
- 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"
- placeholder="https://yourwebsite.com"
- />
+ {id === "logo" ? (
+ <div className="flex h-24 items-center justify-center overflow-hidden rounded-md border border-gray-300">
+ {!isMobile && (
+ <ShimmerDots className="pointer-events-none z-10 opacity-30 [mask-image:radial-gradient(40%_80%,transparent_50%,black)]" />
+ )}
+ <Controller
+ control={control}
+ name="logo"
+ rules={{ required: true }}
+ render={({ field }) => (
+ <FileUpload
+ accept="images"
+ iconClassName="size-5 text-neutral-700"
+ variant="plain"
+ imageSrc={field.value}
+ readFile
+ onChange={({ src }) => field.onChange(src)}
+ maxFileSizeMB={2}
+ targetResolution={{
+ width: 160,
+ height: 160,
+ }}
+ customPreview={
+ <QRCode
+ url="https://dub.co"
+ fgColor="#000"
+ logo={field.value}
+ scale={0.6}
+ />
+ }
+ />
+ )}
+ />
+ </div>
+ ) : (
+ <input
+ {...register(
+ id as keyof z.infer<typeof domainFormSchema>,
+ )}
+ 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"
+ placeholder="https://yourwebsite.com"
+ />
+ )}
</div>
</motion.div>
</div>
diff --git a/packages/ui/src/file-upload.tsx b/packages/ui/src/file-upload.tsx
index 085ce84f68..e03d0e1c57 100644
--- a/packages/ui/src/file-upload.tsx
+++ b/packages/ui/src/file-upload.tsx
@@ -1,9 +1,8 @@
import { cn, resizeImage } from "@dub/utils";
import { VariantProps, cva } from "class-variance-authority";
-import { UploadCloud } from "lucide-react";
import { DragEvent, ReactNode, useState } from "react";
import { toast } from "sonner";
-import { LoadingCircle } from "./icons";
+import { CloudUpload, LoadingCircle } from "./icons";
type AcceptedFileFormats = "any" | "images" | "csv";
@@ -58,7 +57,10 @@ export type FileUploadProps = FileUploadReadFileProps & {
className?: string;
iconClassName?: string;
previewClassName?: string;
-
+ /**
+ * Custom preview component to display instead of the default
+ */
+ customPreview?: ReactNode;
/**
* Image to display (generally for image uploads)
*/
@@ -109,6 +111,7 @@ export function FileUpload({
className,
iconClassName,
previewClassName,
+ customPreview,
accept = "any",
imageSrc,
loading = false,
@@ -228,7 +231,7 @@ export function FileUpload({
: cn(!disabled && "group-hover:bg-gray-50"),
)}
>
- <UploadCloud
+ <CloudUpload
className={cn(
"size-7 transition-all duration-75",
!disabled
@@ -256,16 +259,17 @@ export function FileUpload({
)}
<span className="sr-only">{accessibilityLabel}</span>
</div>
- {imageSrc && (
- <img
- src={imageSrc}
- alt="Preview"
- className={cn(
- "h-full w-full rounded-[inherit] object-cover",
- previewClassName,
- )}
- />
- )}
+ {imageSrc &&
+ (customPreview ?? (
+ <img
+ src={imageSrc}
+ alt="Preview"
+ className={cn(
+ "h-full w-full rounded-[inherit] object-cover",
+ previewClassName,
+ )}
+ />
+ ))}
{clickToUpload && (
<div className="sr-only mt-1 flex shadow-sm">
<input