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 link image previews

Tim Wilson240440f53ed1
diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsx
index 8462c084db..79db8676d0 100644
--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsx
+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsx
@@ -1,10 +1,12 @@
"use client";
+import useLinks from "@/lib/swr/use-links";
import useWorkspace from "@/lib/swr/use-workspace";
import { Button } from "@dub/ui";
import { Globe2 } from "@dub/ui/src/icons";
import { cn } from "@dub/utils";
import { useRouter } from "next/navigation";
+import { useEffect, useState } from "react";
export function DomainSelector() {
const router = useRouter();
@@ -51,6 +53,24 @@ function DomainOption({
onClick: () => void;
isSelected: boolean;
}) {
+ const { links } = useLinks({ sort: "createdAt", pageSize: 1 });
+
+ const [previewImage, setPreviewImage] = useState<string | null>(null);
+
+ useEffect(() => {
+ if (links?.length) {
+ try {
+ const url = links[0].url;
+ fetch(`/api/metatags?url=${url}`).then(async (res) => {
+ if (res.ok) {
+ const results = await res.json();
+ setPreviewImage(results.image);
+ }
+ });
+ } catch (_) {}
+ }
+ }, [links]);
+
return (
<div
className={cn(
@@ -61,13 +81,21 @@ function DomainOption({
onClick={onClick}
aria-selected={isSelected}
>
- <div className="flex w-full flex-col rounded-md border border-gray-300 bg-gray-100 p-2 [mask-image:linear-gradient(to_bottom,black_50%,transparent_95%)]">
+ <div className="flex w-full flex-col gap-2 rounded-md border border-gray-300 bg-gray-100 p-2 [mask-image:linear-gradient(to_bottom,black_50%,transparent_95%)]">
<div className="relative rounded border-gray-200 bg-white px-2 py-1.5 text-center text-sm font-medium text-gray-800">
<Globe2 className="absolute left-2 top-1/2 size-4 -translate-y-1/2" />
{example}
</div>
<div className="overflow-hidden rounded border border-gray-300">
- <div className="aspect-[1.9/1] w-full bg-gray-200" />
+ <div className="aspect-[1.9/1] w-full overflow-hidden bg-gray-200">
+ {previewImage && (
+ <img
+ src={previewImage}
+ alt=""
+ className="size-full object-cover"
+ />
+ )}
+ </div>
</div>
</div>
<span className="text-center text-sm font-medium text-gray-800">
diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/layout.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/layout.tsx
index d772e5aac9..5e940130aa 100644
--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/layout.tsx
+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/layout.tsx
@@ -6,7 +6,7 @@ export default function Layout({ children }: PropsWithChildren) {
<>
<div className="relative flex flex-col items-center">
<Wordmark className="mt-6 h-8" />
- <div className="mt-8 flex w-full flex-col items-center px-3 md:mt-24 lg:px-8">
+ <div className="mt-8 flex w-full flex-col items-center px-3 md:mt-20 lg:px-8">
{children}
</div>
</div>
diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx
index 6b0a0db3a0..ed7e83c9e0 100644
--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx
+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx
@@ -6,12 +6,15 @@ import { ShortLinkInput } from "@/ui/links/short-link-input";
import { useAvailableDomains } from "@/ui/links/use-available-domains";
import { UpgradeRequiredToast } from "@/ui/shared/upgrade-required-toast";
import { Button } from "@dub/ui";
+import { LoadingCircle, Photo } from "@dub/ui/src/icons";
+import { getUrlWithoutUTMParams } from "@dub/utils";
import { useRouter } from "next/navigation";
import posthog from "posthog-js";
-import { useEffect } from "react";
+import { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
+import { useDebounce } from "use-debounce";
type FormData = {
url: string;
@@ -28,6 +31,9 @@ export function Form() {
const { domains, loading, primaryDomain } = useAvailableDomains();
+ const [previewImage, setPreviewImage] = useState<string | null>(null);
+ const [loadingPreviewImage, setLoadingPreviewImage] = useState(false);
+
const {
register,
control,
@@ -54,9 +60,32 @@ export function Form() {
}
}, [loading, primaryDomain, setValue, link]);
+ const [debouncedUrl] = useDebounce(getUrlWithoutUTMParams(url), 500);
+
+ useEffect(() => {
+ if (debouncedUrl) {
+ try {
+ // If url is valid, continue to generate metatags, else return null
+ new URL(debouncedUrl);
+ setLoadingPreviewImage(true);
+ fetch(`/api/metatags?url=${debouncedUrl}`).then(async (res) => {
+ if (res.ok) {
+ const results = await res.json();
+ setPreviewImage(results.image);
+ }
+ });
+ } catch (_) {
+ setPreviewImage(null);
+ } finally {
+ // Timeout to prevent flickering
+ setTimeout(() => setLoadingPreviewImage(false), 200);
+ }
+ }
+ }, [debouncedUrl]);
+
return (
<form
- className="flex w-full flex-col gap-y-4"
+ className="flex w-full flex-col gap-y-6"
onSubmit={handleSubmit(async (data) => {
if (!workspaceId) {
toast.error("Failed to get workspace data.");
@@ -120,6 +149,31 @@ export function Form() {
/>
)}
/>
+ <div className="flex flex-col gap-2">
+ <span className="block text-sm font-medium text-gray-700">
+ Link Preview
+ </span>
+ <div className="relative aspect-[1.91/1] w-full overflow-hidden rounded-md border border-gray-300 bg-gray-100">
+ {previewImage ? (
+ <img
+ src={previewImage}
+ alt="Preview"
+ className="size-full rounded-[inherit] object-cover"
+ />
+ ) : loadingPreviewImage ? (
+ <div className="absolute inset-0 z-[5] flex items-center justify-center rounded-[inherit] bg-white">
+ <LoadingCircle />
+ </div>
+ ) : (
+ <div className="flex size-full flex-col items-center justify-center space-y-4 bg-white">
+ <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>
+ )}
+ </div>
+ </div>
<Button
type="submit"
text="Create link"
diff --git a/apps/web/ui/links/destination-url-input.tsx b/apps/web/ui/links/destination-url-input.tsx
index 3660a40929..61a647e1ee 100644
--- a/apps/web/ui/links/destination-url-input.tsx
+++ b/apps/web/ui/links/destination-url-input.tsx
@@ -33,7 +33,7 @@ export const DestinationUrlInput = forwardRef<
return (
<div>
<div className="flex items-center justify-between">
- <div className="flex items-center space-x-2">
+ <div className="flex items-center gap-2">
<label
htmlFor={inputId}
className="block text-sm font-medium text-gray-700"