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 extra controls to OG modal

Tim Wilsonff0c33f4c123
diff --git a/apps/web/lib/edge-config/get-feature-flags.ts b/apps/web/lib/edge-config/get-feature-flags.ts
index d649eb90b3..ba65e8f8dc 100644
--- a/apps/web/lib/edge-config/get-feature-flags.ts
+++ b/apps/web/lib/edge-config/get-feature-flags.ts
@@ -19,7 +19,7 @@ export const getFeatureFlags = async ({
const workspaceFeatures: Record<BetaFeatures, boolean> = {
referrals: false,
webhooks: false,
- newlinkbuilder: false,
+ newlinkbuilder: true,
};
if (!process.env.NEXT_PUBLIC_IS_DUB || !process.env.EDGE_CONFIG) {
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/link-preview.tsx b/apps/web/ui/modals/add-edit-link-modal-new/link-preview.tsx
index dbc5cabdf0..c846512345 100644
--- a/apps/web/ui/modals/add-edit-link-modal-new/link-preview.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal-new/link-preview.tsx
@@ -107,7 +107,7 @@ export function LinkPreview() {
icon={<Pen2 className="mx-px size-4" />}
className="h-7 w-fit px-1"
onClick={() => setShowOGModal(true)}
- //disabledTooltip={isValidUrl ? undefined : "Enter a URL to customize"}
+ disabledTooltip={isValidUrl ? undefined : "Enter a URL to customize"}
/>
</div>
<div className="mt-2.5 grid grid-cols-4 gap-2">
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx b/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx
index e59a973186..077cae45f6 100644
--- a/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx
@@ -1,7 +1,15 @@
import useWorkspace from "@/lib/swr/use-workspace";
+import { Link } from "@/ui/shared/icons";
import { UpgradeRequiredToast } from "@/ui/shared/upgrade-required-toast";
-import { Button, ButtonTooltip, FileUpload, Modal, Tooltip } from "@dub/ui";
-import { LoadingCircle, Magic } from "@dub/ui/src/icons";
+import {
+ Button,
+ ButtonTooltip,
+ FileUpload,
+ Modal,
+ Popover,
+ Tooltip,
+} from "@dub/ui";
+import { LoadingCircle, Magic, Unsplash } from "@dub/ui/src/icons";
import { resizeImage } from "@dub/utils";
import { useCompletion } from "ai/react";
import posthog from "posthog-js";
@@ -18,6 +26,8 @@ import { useForm, useFormContext } from "react-hook-form";
import TextareaAutosize from "react-textarea-autosize";
import { toast } from "sonner";
import { LinkFormData, LinkModalContext } from ".";
+import { usePromptModal } from "../prompt-modal";
+import UnsplashSearch from "./unsplash-search";
function OGModal({
showOGModal,
@@ -62,6 +72,24 @@ function OGModalInner({
const url = watchParent("url");
const { image, title, description } = watch();
+ 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: {
+ type: "url",
+ placeholder: "https://example.com/og.png",
+ },
+ onSubmit: (image) => {
+ if (!image) return;
+
+ setValue("image", image);
+ setValue("proxy", true);
+ },
+ });
+
+ const [openUnsplashPopover, setOpenUnsplashPopover] = useState(false);
const [resizing, setResizing] = useState(false);
const {
@@ -154,189 +182,238 @@ function OGModalInner({
}, [completionDescription]);
return (
- <Modal
- showModal={showOGModal}
- setShowModal={setShowOGModal}
- className="max-w-[500px]"
- >
- <form
- className="px-5 py-4"
- // For some reason using handleSubmit causes the link modal's form to submit
- onSubmit={(e) => {
- e.preventDefault();
- e.stopPropagation();
-
- setShowOGModal(false);
- (["image", "title", "description", "proxy"] as const).forEach((key) =>
- setValueParent(key, getValues(key)),
- );
- }}
+ <>
+ <PromptModal />
+ <Modal
+ showModal={showOGModal}
+ setShowModal={setShowOGModal}
+ className="max-w-[500px]"
>
- <div className="flex items-center justify-between">
- <h3 className="text-lg font-medium">Link Design</h3>
- <div className="max-md:hidden">
- <Tooltip
- content={
- <div className="px-2 py-1 text-xs text-gray-700">
- Press <strong className="font-medium text-gray-950">D</strong>{" "}
- to open this quickly
- </div>
- }
- side="right"
- >
- <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 text-xs text-gray-950">
- D
- </kbd>
- </Tooltip>
- </div>
- </div>
+ <form
+ className="px-5 py-4"
+ // For some reason using handleSubmit causes the link modal's form to submit
+ onSubmit={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
- <div className="scrollbar-hide -m-1 mt-6 flex max-h-[calc(100dvh-250px)] flex-col gap-6 overflow-y-auto p-1">
- <div>
- <div className="flex items-center justify-between">
- <span className="block text-sm font-medium text-gray-700">
- Image
- </span>
+ setShowOGModal(false);
+ (["image", "title", "description", "proxy"] as const).forEach(
+ (key) => setValueParent(key, getValues(key)),
+ );
+ }}
+ >
+ <div className="flex items-center justify-between">
+ <h3 className="text-lg font-medium">Link Design</h3>
+ <div className="max-md:hidden">
+ <Tooltip
+ content={
+ <div className="px-2 py-1 text-xs text-gray-700">
+ Press{" "}
+ <strong className="font-medium text-gray-950">D</strong> to
+ open this quickly
+ </div>
+ }
+ side="right"
+ >
+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 text-xs text-gray-950">
+ D
+ </kbd>
+ </Tooltip>
</div>
- <FileUpload
- accept="images"
- variant="default"
- imageSrc={image}
- onChange={async ({ file }) => {
- setResizing(true);
-
- const image = await resizeImage(file);
- setValue("image", image);
- setValue("proxy", true);
-
- // Delay to prevent flickering
- setTimeout(() => setResizing(false), 500);
- }}
- loading={generatingMetatags || resizing}
- clickToUpload={true}
- showHoverOverlay={false}
- accessibilityLabel="OG image upload"
- className="mt-2"
- content={
- <>
- <p>Drag and drop or click to upload.</p>
- <p className="mt-1">Recommended: 1200 x 630 pixels</p>
- </>
- }
- />
</div>
- {/* Title */}
- <div>
- <div className="flex items-center justify-between">
- <p className="block text-sm font-medium text-gray-700">Title</p>
- <div className="flex items-center gap-2">
- <p className="text-sm text-gray-500">
- {title?.length || 0}/120
- </p>
- <ButtonTooltip
- tooltipContent="Generate an optimized title using AI."
- onClick={generateTitle}
- disabled={generatingTitle || !title || exceededAI}
- className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"
- >
- {generatingTitle ? (
- <LoadingCircle />
- ) : (
- <Magic className="h-4 w-4" />
+ <div className="scrollbar-hide -m-1 mt-6 flex max-h-[calc(100dvh-250px)] flex-col gap-6 overflow-y-auto p-1">
+ <div>
+ <div className="flex items-center justify-between">
+ <span className="block text-sm font-medium text-gray-700">
+ Image
+ </span>
+ <div className="flex items-center gap-2 md:gap-4">
+ {image && (
+ <button
+ type="button"
+ className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
+ onClick={() => {
+ setValue("image", null);
+ setValue("proxy", false);
+ }}
+ >
+ Remove
+ </button>
)}
- </ButtonTooltip>
- </div>
- </div>
- <div className="relative mt-1 flex rounded-md shadow-sm">
- {generatingMetatags && (
- <div className="absolute flex h-full w-full items-center justify-center rounded-md border border-gray-300 bg-white">
- <LoadingCircle />
+ <ButtonTooltip
+ type="button"
+ className="text-gray-700 transition-colors hover:text-gray-950"
+ onClick={() => setShowPromptModal(true)}
+ tooltipContent="Paste a URL to an image"
+ >
+ <Link className="size-4 rotate-90" />
+ </ButtonTooltip>
+ <Popover
+ content={
+ <UnsplashSearch
+ onImageSelected={(image) => {
+ setValue("image", image);
+ setValue("proxy", true);
+ }}
+ setOpenPopover={setOpenUnsplashPopover}
+ />
+ }
+ openPopover={openUnsplashPopover}
+ setOpenPopover={setOpenUnsplashPopover}
+ >
+ <div>
+ <ButtonTooltip
+ onClick={() => setOpenUnsplashPopover(true)}
+ className="text-gray-700 transition-colors hover:text-gray-950"
+ tooltipContent="Choose an image from Unsplash"
+ >
+ <Unsplash className="size-3.5 text-gray-500" />
+ </ButtonTooltip>
+ </div>
+ </Popover>
</div>
- )}
- <TextareaAutosize
- name="title"
- id="title"
- minRows={2}
- maxLength={120}
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="Add a title..."
- value={title || ""}
- onChange={(e) => {
- setValue("title", e.target.value);
+ </div>
+ <FileUpload
+ accept="images"
+ variant="default"
+ imageSrc={image}
+ onChange={async ({ file }) => {
+ setResizing(true);
+
+ const image = await resizeImage(file);
+ setValue("image", image);
+ setValue("proxy", true);
+
+ // Delay to prevent flickering
+ setTimeout(() => setResizing(false), 500);
}}
- aria-invalid="true"
+ loading={generatingMetatags || resizing}
+ clickToUpload={true}
+ showHoverOverlay={false}
+ accessibilityLabel="OG image upload"
+ className="mt-2"
+ content={
+ <>
+ <p>Drag and drop or click to upload.</p>
+ <p className="mt-1">Recommended: 1200 x 630 pixels</p>
+ </>
+ }
/>
</div>
- </div>
- {/* Description */}
- <div>
- <div className="flex items-center justify-between">
- <p className="block text-sm font-medium text-gray-700">
- Description
- </p>
- <div className="flex items-center gap-2">
- <p className="text-sm text-gray-500">
- {description?.length || 0}/240
- </p>
- <ButtonTooltip
- tooltipContent="Generate an optimized description using AI."
- onClick={generateDescription}
- disabled={generatingDescription || !description}
- className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"
- >
- {generatingDescription ? (
+ {/* Title */}
+ <div>
+ <div className="flex items-center justify-between">
+ <p className="block text-sm font-medium text-gray-700">Title</p>
+ <div className="flex items-center gap-2">
+ <p className="text-sm text-gray-500">
+ {title?.length || 0}/120
+ </p>
+ <ButtonTooltip
+ tooltipContent="Generate an optimized title using AI."
+ onClick={generateTitle}
+ disabled={generatingTitle || !title || exceededAI}
+ className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"
+ >
+ {generatingTitle ? (
+ <LoadingCircle />
+ ) : (
+ <Magic className="h-4 w-4" />
+ )}
+ </ButtonTooltip>
+ </div>
+ </div>
+ <div className="relative mt-1 flex rounded-md shadow-sm">
+ {generatingMetatags && (
+ <div className="absolute flex h-full w-full items-center justify-center rounded-md border border-gray-300 bg-white">
<LoadingCircle />
- ) : (
- <Magic className="h-4 w-4" />
- )}
- </ButtonTooltip>
+ </div>
+ )}
+ <TextareaAutosize
+ name="title"
+ id="title"
+ minRows={2}
+ maxLength={120}
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ placeholder="Add a title..."
+ value={title || ""}
+ onChange={(e) => {
+ setValue("title", e.target.value);
+ }}
+ aria-invalid="true"
+ />
</div>
</div>
- <div className="relative mt-1 flex rounded-md shadow-sm">
- {generatingMetatags && (
- <div className="absolute flex h-full w-full items-center justify-center rounded-md border border-gray-300 bg-white">
- <LoadingCircle />
+
+ {/* Description */}
+ <div>
+ <div className="flex items-center justify-between">
+ <p className="block text-sm font-medium text-gray-700">
+ Description
+ </p>
+ <div className="flex items-center gap-2">
+ <p className="text-sm text-gray-500">
+ {description?.length || 0}/240
+ </p>
+ <ButtonTooltip
+ tooltipContent="Generate an optimized description using AI."
+ onClick={generateDescription}
+ disabled={generatingDescription || !description}
+ className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"
+ >
+ {generatingDescription ? (
+ <LoadingCircle />
+ ) : (
+ <Magic className="h-4 w-4" />
+ )}
+ </ButtonTooltip>
</div>
- )}
- <TextareaAutosize
- name="description"
- id="description"
- minRows={3}
- maxLength={240}
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="Add a description..."
- value={description || ""}
- onChange={(e) => setValue("description", e.target.value)}
- aria-invalid="true"
- />
+ </div>
+ <div className="relative mt-1 flex rounded-md shadow-sm">
+ {generatingMetatags && (
+ <div className="absolute flex h-full w-full items-center justify-center rounded-md border border-gray-300 bg-white">
+ <LoadingCircle />
+ </div>
+ )}
+ <TextareaAutosize
+ name="description"
+ id="description"
+ minRows={3}
+ maxLength={240}
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ placeholder="Add a description..."
+ value={description || ""}
+ onChange={(e) => setValue("description", e.target.value)}
+ aria-invalid="true"
+ />
+ </div>
</div>
</div>
- </div>
- <div className="mt-6 flex justify-end">
- <div className="flex items-center gap-2">
- <Button
- type="button"
- variant="secondary"
- text="Cancel"
- className="h-9 w-fit"
- onClick={() => {
- reset();
- setShowOGModal(false);
- }}
- />
- <Button
- type="submit"
- variant="primary"
- text="Save changes"
- className="h-9 w-fit"
- />
+ <div className="mt-6 flex justify-end">
+ <div className="flex items-center gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Cancel"
+ className="h-9 w-fit"
+ onClick={() => {
+ reset();
+ setShowOGModal(false);
+ }}
+ />
+ <Button
+ type="submit"
+ variant="primary"
+ text="Save changes"
+ className="h-9 w-fit"
+ />
+ </div>
</div>
- </div>
- </form>
- </Modal>
+ </form>
+ </Modal>
+ </>
);
}
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/unsplash-search.tsx b/apps/web/ui/modals/add-edit-link-modal-new/unsplash-search.tsx
new file mode 100644
index 0000000000..9543041e8e
--- /dev/null
+++ b/apps/web/ui/modals/add-edit-link-modal-new/unsplash-search.tsx
@@ -0,0 +1,105 @@
+import { LoadingSpinner, useMediaQuery } from "@dub/ui";
+import { fetcher } from "@dub/utils";
+import { Dispatch, SetStateAction, useState } from "react";
+import { toast } from "sonner";
+import useSWR from "swr";
+import { Basic } from "unsplash-js/dist/methods/photos/types";
+import { useDebounce } from "use-debounce";
+
+export default function UnsplashSearch({
+ onImageSelected,
+ setOpenPopover,
+}: {
+ onImageSelected: (image: string) => void;
+ setOpenPopover: Dispatch<SetStateAction<boolean>>;
+}) {
+ const [search, setSearch] = useState("");
+ const [debouncedQuery] = useDebounce(search, 500);
+ const { data } = useSWR<Basic[]>(
+ `/api/unsplash/search?query=${
+ debouncedQuery.length > 0 ? debouncedQuery : "beautiful landscape photos"
+ }`,
+ fetcher,
+ {
+ onError: (err) => {
+ toast.error(err.message);
+ },
+ },
+ );
+ const { isMobile } = useMediaQuery();
+
+ return (
+ <div
+ className="h-[24rem] w-full overflow-auto p-3 md:w-[24rem]"
+ // Fixes a Webkit issue where elements outside of the visible area are still interactable
+ style={{ WebkitClipPath: "inset(0 0 0 0)" }}
+ >
+ <div className="relative mt-1 rounded-md shadow-sm">
+ <input
+ type="text"
+ name="search"
+ id="search"
+ placeholder="Search for an image..."
+ autoFocus={!isMobile}
+ autoComplete="off"
+ value={search}
+ onChange={(e) => setSearch(e.target.value)}
+ className="block w-full rounded-md border-gray-300 py-1 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ />
+ </div>
+ {data ? (
+ data.length > 0 ? (
+ <div className="mt-3 grid grid-cols-2 gap-3">
+ {data.map((photo) => (
+ <button
+ key={photo.id}
+ type="button"
+ onClick={() => {
+ onImageSelected(photo.urls.regular);
+ setOpenPopover(false);
+ fetch("/api/unsplash/download", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ url: photo.links.download_location,
+ }),
+ });
+ }}
+ className="relative flex h-24 w-full items-center justify-center overflow-hidden rounded-md bg-gray-100 transition-all hover:brightness-75"
+ >
+ <img
+ src={photo.urls.small}
+ alt={photo.alt_description || "Unsplash image"}
+ className="absolute h-full w-full object-cover"
+ />
+ <p className="absolute bottom-0 left-0 right-0 line-clamp-1 w-full bg-black bg-opacity-10 p-1 text-xs text-white">
+ by{" "}
+ <a
+ className="underline underline-offset-2"
+ target="_blank"
+ rel="noopener noreferrer"
+ href={`${photo.user.links.html}?utm_source=dub.co&utm_medium=referral`}
+ >
+ {photo.user.name}
+ </a>
+ </p>
+ </button>
+ ))}
+ </div>
+ ) : (
+ <div className="flex h-[90%] items-center justify-center">
+ <p className="text-center text-sm text-gray-500">
+ No results found. <br /> Maybe try tweaking your search query?
+ </p>
+ </div>
+ )
+ ) : (
+ <div className="flex h-[90%] items-center justify-center">
+ <LoadingSpinner />
+ </div>
+ )}
+ </div>
+ );
+}