The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add new options to image previews
Tim Wilsonaeb2d8ffae5d
diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsxindex cca1b46cc9..31de0dfa14 100644--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx@@ -751,7 +751,11 @@ function AddEditLinkModal({</form></div><div className="scrollbar-hide rounded-r-2xl md:max-h-[95vh] md:overflow-auto">- <Preview data={data} generatingMetatags={generatingMetatags} />+ <Preview+ data={data}+ setData={setData}+ generatingMetatags={generatingMetatags}+ /></div></div></Modal>diff --git a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx b/apps/web/ui/modals/add-edit-link-modal/og-section.tsxindex b21543ec14..96b7854dca 100644--- a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/og-section.tsx@@ -37,7 +37,7 @@ export default function OGSection({const { id: workspaceId, exceededAI, mutate } = useWorkspace();const { setShowPromptModal, PromptModal } = usePromptModal({- title: "Choose image from URL",+ title: "Use image from URL",description:"Paste an image URL to use for your link's social media cards.",label: "Image URL",@@ -225,12 +225,14 @@ export default function OGSection({</Popover></div></div>- <ImageDrop- src={image}- onChange={(image) => setData((prev) => ({ ...prev, image }))}- loading={generatingMetatags}- accessibilityLabel="OG image upload"- />+ <div className="mt-1">+ <ImageDrop+ src={image}+ onChange={(image) => setData((prev) => ({ ...prev, image }))}+ loading={generatingMetatags}+ accessibilityLabel="OG image upload"+ />+ </div></div><div>diff --git a/apps/web/ui/modals/add-edit-link-modal/preview.tsx b/apps/web/ui/modals/add-edit-link-modal/preview.tsxindex 1c279f8e9c..be299dafe6 100644--- a/apps/web/ui/modals/add-edit-link-modal/preview.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/preview.tsx@@ -1,25 +1,40 @@-import { isStored } from "@/lib/storage";+import useWorkspace from "@/lib/swr/use-workspace";import { LinkProps } from "@/lib/types";import {- BlurImage,Facebook,+ ImageDrop,LinkedIn,LoadingCircle,Photo,Popover,+ SimpleTooltipContent,+ Tooltip,Twitter,+ useRouterStuff,} from "@dub/ui";import { Button } from "@dub/ui/src/button";-import { getDomainWithoutWWW } from "@dub/utils";-import { Edit2, Upload } from "lucide-react";-import { useMemo, useState } from "react";+import { getDomainWithoutWWW, resizeImage } from "@dub/utils";+import { Edit2, Link2, Upload } from "lucide-react";+import {+ ChangeEvent,+ Dispatch,+ PropsWithChildren,+ SetStateAction,+ useCallback,+ useMemo,+ useRef,+ useState,+} from "react";import { useDebounce } from "use-debounce";+import { usePromptModal } from "../prompt-modal";export default function Preview({data,+ setData,generatingMetatags,}: {data: LinkProps;+ setData: Dispatch<SetStateAction<LinkProps>>;generatingMetatags: boolean;}) {const { title, description, image, url, password } = data;@@ -29,45 +44,8 @@ export default function Preview({return getDomainWithoutWWW(debouncedUrl);}, [password, debouncedUrl]);- const previewImage = useMemo(() => {- if (generatingMetatags) {- return (- <div className="flex h-[250px] w-full flex-col items-center justify-center space-y-4 bg-gray-100">- <LoadingCircle />- </div>- );- }- if (image) {- if (isStored(image)) {- return (- <BlurImage- src={image}- alt="Preview"- width={1200}- height={627}- className="h-[250px] w-full object-cover"- />- );- } else {- return (- <img- src={image}- alt="Preview"- className="h-[250px] w-full object-cover"- />- );- }- } else {- return (- <div className="flex h-[250px] w-full flex-col items-center justify-center space-y-4 bg-gray-100">- <Photo className="h-8 w-8 text-gray-400" />- <p className="text-sm text-gray-400">- Enter a link to generate a preview.- </p>- </div>- );- }- }, [image, generatingMetatags]);+ const onImageChange = (image: string) =>+ setData((prev) => ({ ...prev, image }));return (<div>@@ -92,8 +70,11 @@ export default function Preview({</div></div><div className="group relative overflow-hidden rounded-2xl border border-gray-300">- <EditPreviewPopover />- {previewImage}+ <ImagePreview+ image={image}+ onImageChange={onImageChange}+ generatingMetatags={generatingMetatags}+ />{title && (<div className="absolute bottom-2 left-2 rounded-md bg-[#414142] px-1.5 py-px"><h3 className="max-w-sm truncate text-sm text-white">@@ -124,8 +105,11 @@ export default function Preview({</div></div><div className="relative border border-gray-300">- <EditPreviewPopover />- {previewImage}+ <ImagePreview+ image={image}+ onImageChange={onImageChange}+ generatingMetatags={generatingMetatags}+ /><div className="grid gap-1 border-t border-gray-300 bg-[#f2f3f5] p-3">{hostname ? (<p className="text-[0.8rem] uppercase text-[#606770]">@@ -172,8 +156,11 @@ export default function Preview({</div></div><div className="relative overflow-hidden rounded-[2px] shadow-[0_0_0_1px_rgba(0,0,0,0.15),0_2px_3px_rgba(0,0,0,0.2)]">- <EditPreviewPopover />- {previewImage}+ <ImagePreview+ image={image}+ onImageChange={onImageChange}+ generatingMetatags={generatingMetatags}+ /><div className="grid gap-1 border-t border-gray-300 bg-white p-3">{title ? (<h3 className="truncate font-semibold text-[#000000E6]">@@ -195,47 +182,165 @@ export default function Preview({);}-const EditPreviewPopover = () => {+const ImagePreview = ({+ image,+ onImageChange,+ generatingMetatags,+}: {+ image: string | null;+ onImageChange: (image: string) => void;+ generatingMetatags?: boolean;+}) => {+ const { plan } = useWorkspace();+ const isFreePlan = plan === "free";++ const inputFileRef = useRef<HTMLInputElement>(null);+const [openPopover, setOpenPopover] = useState(false);+ const [resizing, setResizing] = useState(false);++ const { setShowPromptModal, PromptModal } = usePromptModal({+ title: "Use image from URL",+ description:+ "Paste an image URL to use for your link's social media cards.",+ label: "Image URL",+ inputProps: {+ placeholder: "https://example.com/og.png",+ },+ onSubmit: (image) => {+ if (image) onImageChange(image);+ },+ });++ const onInputFileChange = useCallback(+ async (e: ChangeEvent<HTMLInputElement>) => {+ const file = e.target.files && e.target.files[0];+ if (!file) return;++ setResizing(true);++ const src = await resizeImage(file);+ onImageChange(src);++ // Delay to prevent flickering+ setTimeout(() => {+ setResizing(false);+ }, 500);+ },+ [],+ );++ const previewImage = useMemo(() => {+ if (generatingMetatags || resizing) {+ return (+ <div className="flex h-[250px] w-full flex-col items-center justify-center space-y-4 bg-gray-100">+ <LoadingCircle />+ </div>+ );+ }+ if (image) {+ return (+ <ImageDrop+ variant="plain"+ src={image}+ onChange={onImageChange}+ loading={generatingMetatags}+ clickToUpload={false}+ showHoverOverlay={false}+ />+ );+ } else {+ return (+ <div className="flex h-[250px] w-full flex-col items-center justify-center space-y-4 bg-gray-100">+ <Photo className="h-8 w-8 text-gray-400" />+ <p className="text-sm text-gray-400">+ Enter a link to generate a preview.+ </p>+ </div>+ );+ }+ }, [image, generatingMetatags, resizing]);return (- <Popover- content={- <div className="grid w-full gap-px p-2 sm:w-40">- <Button- text="Upload"- variant="outline"- icon={<Upload className="h-4 w-4" />}- className="h-9 px-2 font-medium"- />- <Button- text="Upload"- variant="outline"- icon={<Upload className="h-4 w-4" />}- className="h-9 px-2 font-medium"- />- <Button- text="Upload"- variant="outline"- onClick={() => {- setOpenPopover(false);- }}- icon={<Upload className="h-4 w-4" />}- className="h-9 px-2 font-medium"- />+ <>+ <Popover+ align="end"+ content={+ <div className="grid w-full gap-px p-2">+ <Button+ text="Upload image"+ variant="outline"+ icon={<Upload className="h-4 w-4" />}+ className="h-9 justify-start px-2 font-medium disabled:border-none disabled:bg-transparent"+ onClick={() => {+ inputFileRef.current?.click();+ setOpenPopover(false);+ }}+ />+ <Button+ text="Use image from URL"+ variant="outline"+ icon={<Link2 className="h-4 w-4" />}+ className="h-9 justify-start px-2 font-medium"+ onClick={() => setShowPromptModal(true)}+ />+ </div>+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <div className="absolute right-2 top-2 z-10">+ <ProGuard isFreePlan={isFreePlan}>+ <div>+ <Button+ variant="secondary"+ onClick={() => setOpenPopover(!openPopover)}+ icon={<Edit2 className="h-3 w-3" />}+ className="h-8 w-8 rounded-md p-0 transition-all hover:bg-gray-100"+ disabled={isFreePlan}+ />+ </div>+ </ProGuard></div>+ </Popover>+ {previewImage}+ <input+ ref={inputFileRef}+ onChange={onInputFileChange}+ type="file"+ className="hidden"+ />+ <PromptModal />+ </>+ );+};++const ProGuard = ({+ isFreePlan,+ children,+}: PropsWithChildren<{ isFreePlan: boolean }>) => {+ const { queryParams } = useRouterStuff();++ return isFreePlan ? (+ <Tooltip+ content={+ <SimpleTooltipContent+ title="Custom Social Media Cards are a Pro feature."+ cta="Upgrade to Pro"+ href={+ queryParams({+ set: {+ upgrade: "pro",+ },+ getNewPath: true,+ }) as string+ }+ />}- openPopover={openPopover}- setOpenPopover={setOpenPopover}>- <div className="absolute right-2 top-2 z-10">- <Button- variant="secondary"- onClick={() => setOpenPopover(!openPopover)}- icon={<Edit2 className="h-3 w-3" />}- className="h-8 w-8 rounded-md p-0 transition-all hover:bg-gray-100"- />- </div>- </Popover>+ {children}+ </Tooltip>+ ) : (+ children);};diff --git a/packages/ui/src/image-drop.tsx b/packages/ui/src/image-drop.tsxindex 267e0c855b..e7b9185ac1 100644--- a/packages/ui/src/image-drop.tsx+++ b/packages/ui/src/image-drop.tsx@@ -3,6 +3,23 @@ import { UploadCloud } from "lucide-react";import { DragEvent, useId, useState } from "react";import { LoadingCircle, LoadingSpinner } from "./icons";+import { cva, VariantProps } from "class-variance-authority";++const imageDropVariants = cva(+ "group relative isolate flex aspect-[1200/630] w-full flex-col items-center justify-center overflow-hidden bg-white transition-all hover:bg-gray-50",+ {+ variants: {+ variant: {+ default: "rounded-md border border-gray-300 shadow-sm",+ plain: "",+ },+ },+ defaultVariants: {+ variant: "default",+ },+ },+);+export type ImageDropProps = {src: string | null;onChange?: (src: string) => void;@@ -13,6 +30,16 @@ export type ImageDropProps = {*/loading?: boolean;+ /**+ * Whether to allow clicking on the area to upload+ */+ clickToUpload?: boolean;++ /**+ * Whether to show instruction overlay when hovered+ */+ showHoverOverlay?: boolean;+/*** Desired resolution to suggest and automatically resize to*/@@ -22,13 +49,16 @@ export type ImageDropProps = {* Accessibility label for screen readers*/accessibilityLabel?: string;-};+} & VariantProps<typeof imageDropVariants>;export function ImageDrop({src,onChange,+ variant,className,loading = false,+ clickToUpload = true,+ showHoverOverlay = true,targetResolution = { width: 1200, height: 630 },accessibilityLabel = "Image upload",}: ImageDropProps) {@@ -61,7 +91,8 @@ export function ImageDrop({<labelhtmlFor={inputId}className={cn(- "group relative isolate mt-1 flex aspect-[1200/630] w-full cursor-pointer flex-col items-center justify-center overflow-hidden rounded-md border border-gray-300 bg-white shadow-sm transition-all hover:bg-gray-50",+ imageDropVariants({ variant }),+ clickToUpload && "cursor-pointer",className,)}>@@ -99,7 +130,9 @@ export function ImageDrop({"absolute inset-0 z-[3] flex flex-col items-center justify-center rounded-[inherit] bg-white transition-all",dragActive &&"cursor-copy border-2 border-black bg-gray-50 opacity-100",- src ? "opacity-0 group-hover:opacity-100" : "group-hover:bg-gray-50",+ src+ ? cn("opacity-0", showHoverOverlay && "group-hover:opacity-100")+ : "group-hover:bg-gray-50",)}>{resizing ? (@@ -118,7 +151,7 @@ export function ImageDrop({)}/><p className="mt-2 text-center text-sm text-gray-500">- Drag and drop or click to upload.+ Drag and drop {clickToUpload && "or click"} to upload.</p><p className="mt-2 text-center text-sm text-gray-500">Recommended: {targetResolution.width} x {targetResolution.height}{" "}@@ -135,15 +168,17 @@ export function ImageDrop({className="h-full w-full rounded-[inherit] object-cover"/>)}- <div className="sr-only mt-1 flex shadow-sm">- <input- id={inputId}- name="image"- type="file"- accept="image/*"- onChange={onImageChange}- />- </div>+ {clickToUpload && (+ <div className="sr-only mt-1 flex shadow-sm">+ <input+ id={inputId}+ name="image"+ type="file"+ accept="image/*"+ onChange={onImageChange}+ />+ </div>+ )}</label>);}