The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Unsplash Integration
Steven Tey2f9c097ed269
diff --git a/app/api/unsplash/download/route.ts b/app/api/unsplash/download/route.tsnew file mode 100644index 0000000000..b2f35236d6--- /dev/null+++ b/app/api/unsplash/download/route.ts@@ -0,0 +1,13 @@+import { NextResponse } from "next/server";+import { unsplash } from "../utils";++export async function POST(req: Request) {+ const { url } = await req.json();+ if (!url) return new Response("Missing url", { status: 400 });++ const response = await unsplash.photos.trackDownload({+ downloadLocation: url,+ });++ return NextResponse.json(response);+}diff --git a/app/api/unsplash/search/route.ts b/app/api/unsplash/search/route.tsnew file mode 100644index 0000000000..104a07fd99--- /dev/null+++ b/app/api/unsplash/search/route.ts@@ -0,0 +1,34 @@+import { NextResponse } from "next/server";+import { unsplash } from "../utils";+import { ipAddress } from "@vercel/edge";+import { LOCALHOST_IP } from "#/lib/constants";+import { ratelimit } from "#/lib/upstash";++export const runtime = "edge";++export async function GET(req: Request) {+ const searchParams = new URL(req.url).searchParams;+ const query = searchParams.get("query");++ if (!query) return new Response("Missing query", { status: 400 });++ const ip = ipAddress(req) || LOCALHOST_IP;+ const { success } = await ratelimit(10, "10 s").limit(ip);+ if (!success) {+ return new Response("Don't DDoS me pls 🥺", { status: 429 });+ }++ const response = await unsplash.search.getPhotos({+ query,+ });++ if (response.errors) {+ return new Response(JSON.stringify(response.errors), {+ status: 500,+ });+ }++ const data = response.response?.results;++ return NextResponse.json(data);+}diff --git a/app/api/unsplash/utils.ts b/app/api/unsplash/utils.tsnew file mode 100644index 0000000000..f10fc49f87--- /dev/null+++ b/app/api/unsplash/utils.ts@@ -0,0 +1,5 @@+import { createApi } from "unsplash-js";++export const unsplash = createApi({+ accessKey: process.env.UNSPLASH_ACCESS_KEY as string,+});diff --git a/app/ui/popover.tsx b/app/ui/popover.tsxindex 4d6d0ee9ed..998838dbfc 100644--- a/app/ui/popover.tsx+++ b/app/ui/popover.tsx@@ -24,7 +24,7 @@ export default function Popover({return (<Drawer.Root open={openPopover} onOpenChange={setOpenPopover}><div className="md:hidden">{children}</div>- <Drawer.Overlay className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur" />+ <Drawer.Overlay className="fixed inset-0 z-40 bg-gray-100 bg-opacity-10 backdrop-blur" /><Drawer.Portal><Drawer.Content className="fixed bottom-0 left-0 right-0 z-50 mt-24 rounded-t-[10px] border-t border-gray-200 bg-white"><div className="sticky top-0 z-20 flex w-full items-center justify-center rounded-t-[10px] bg-inherit">diff --git a/components/app/modals/add-edit-link-modal/og-section.tsx b/components/app/modals/add-edit-link-modal/og-section.tsxindex 2474322295..cc14b6737a 100644--- a/components/app/modals/add-edit-link-modal/og-section.tsx+++ b/components/app/modals/add-edit-link-modal/og-section.tsx@@ -13,6 +13,10 @@ import { LinkProps } from "#/lib/types";import Switch from "#/ui/switch";import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";+import { Link2 } from "lucide-react";+import Popover from "#/ui/popover";+import Unsplash from "@/components/shared/icons/unsplash";+import UnsplashSearch from "./unsplash-search";export default function OGSection({props,@@ -63,6 +67,8 @@ export default function OGSection({}}, [proxy]);+ const [openPopover, setOpenPopover] = useState(false);+return (<div className="grid gap-5 border-b border-gray-200 pb-5"><div className="flex items-center justify-between">@@ -98,19 +104,35 @@ export default function OGSection({{fileError ? (<p className="text-sm text-red-500">{fileError}</p>) : (- <button- className="text-sm text-gray-500"- type="button"- onClick={() => {- const image = window.prompt(- "Paste a URL to an image (max 5MB)",- "https://",- );- setData((prev) => ({ ...prev, image }));- }}- >- Paste a URL instead- </button>+ <div className="flex items-center justify-between">+ <button+ className="mr-1 flex h-6 w-6 items-center justify-center rounded-md transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200"+ type="button"+ onClick={() => {+ const image = window.prompt(+ "Paste a URL to an image (max 5MB)",+ "https://",+ );+ setData((prev) => ({ ...prev, image }));+ }}+ >+ <Link2 className="h-4 w-4 text-gray-500" />+ </button>+ <Popover+ content={+ <UnsplashSearch+ setData={setData}+ setOpenPopover={setOpenPopover}+ />+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <div className="flex h-6 w-6 cursor-pointer items-center justify-center rounded-md transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200">+ <Unsplash className="h-3 w-3 text-gray-500" />+ </div>+ </Popover>+ </div>)}</div><labeldiff --git a/components/app/modals/add-edit-link-modal/unsplash-search.tsx b/components/app/modals/add-edit-link-modal/unsplash-search.tsxnew file mode 100644index 0000000000..062829c46f--- /dev/null+++ b/components/app/modals/add-edit-link-modal/unsplash-search.tsx@@ -0,0 +1,95 @@+import { LinkProps } from "#/lib/types";+import { LoadingSpinner } from "#/ui/icons";+import { Dispatch, SetStateAction, useState } from "react";+import useSWR from "swr";+import { fetcher } from "#/lib/utils";+import { useDebounce } from "use-debounce";+import { Basic } from "unsplash-js/dist/methods/photos/types";+import { toast } from "sonner";++export default function UnsplashSearch({+ setData,+ setOpenPopover,+}: {+ setData: Dispatch<SetStateAction<LinkProps>>;+ 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);+ },+ },+ );+ return (+ <div className="h-[24rem] w-full overflow-auto p-3 md:w-[24rem]">+ <div className="relative mt-1 rounded-md shadow-sm">+ <input+ type="text"+ name="search"+ id="search"+ placeholder="Search for an image..."+ autoFocus+ 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-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"+ />+ </div>+ {data ? (+ <div className="mt-3 grid grid-cols-2 gap-3">+ {data.map((photo) => (+ <button+ key={photo.id}+ type="button"+ onClick={() => {+ setData((prev) => ({+ ...prev,+ image: 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.sh&utm_medium=referral`}+ >+ {photo.user.name}+ </a>+ </p>+ </button>+ ))}+ </div>+ ) : (+ <div className="flex h-full items-center justify-center">+ <LoadingSpinner />+ </div>+ )}+ </div>+ );+}diff --git a/components/shared/icons/unsplash.tsx b/components/shared/icons/unsplash.tsxnew file mode 100644index 0000000000..ad04fc87d3--- /dev/null+++ b/components/shared/icons/unsplash.tsx@@ -0,0 +1,17 @@+export default function Unsplash({ className }: { className?: string }) {+ return (+ <svg+ width="32"+ height="32"+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 32 32"+ className={className}+ >+ <path+ d="M10 9V0h12v9H10zm12 5h10v18H0V14h10v9h12v-9z"+ fill="currentColor"+ fillRule="nonzero"+ />+ </svg>+ );+}diff --git a/package.json b/package.jsonindex ac1ee0a934..2f52de8973 100644--- a/package.json+++ b/package.json@@ -89,6 +89,7 @@"tailwind-scrollbar-hide": "^1.1.7","tailwindcss-radix": "^2.8.0","topojson-client": "^3.1.0",+ "unsplash-js": "^7.0.18","use-debounce": "^8.0.4","vaul": "^0.0.6"},diff --git a/yarn.lock b/yarn.lockindex a81e2ca960..f36490b1d1 100644--- a/yarn.lock+++ b/yarn.lock@@ -2442,6 +2442,11 @@dependencies:"@types/estree" "*"+"@types/content-type@^1.1.3":+ version "1.1.5"+ resolved "https://registry.yarnpkg.com/@types/content-type/-/content-type-1.1.5.tgz#aa02dca40864749a9e2bf0161a6216da57e3ede5"+ integrity sha512-dgMN+syt1xb7Hk8LU6AODOfPlvz5z1CbXpPuJE5ZrX9STfBOIXF09pEB8N7a97WT9dbngt3ksDCm6GW6yMrxfQ==+"@types/d3-color@^1":version "1.4.2"resolved "https://registry.npmjs.org/@types/d3-color/-/d3-color-1.4.2.tgz"@@ -3612,6 +3617,11 @@ console-control-strings@^1.1.0:resolved "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz"integrity sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==+content-type@^1.0.4:+ version "1.0.5"+ resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.5.tgz#8b773162656d1d1086784c8f23a54ce6d73d7918"+ integrity sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==+contentlayer@^0.3.4:version "0.3.4"resolved "https://registry.yarnpkg.com/contentlayer/-/contentlayer-0.3.4.tgz#519ad446e38d533a2166a7bd3bd5188c5be019ab"@@ -8513,6 +8523,14 @@ universalify@^2.0.0:resolved "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz"integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==+unsplash-js@^7.0.18:+ version "7.0.18"+ resolved "https://registry.yarnpkg.com/unsplash-js/-/unsplash-js-7.0.18.tgz#7974fcf483b76045822438e1980145dbd42a15fd"+ integrity sha512-R3HTszj9EFWVqubcI+ncukGJ4JusWLBKGk5ZbT7ZYcUjNpa5VkHgTh2qu2pWEVX00Br7OI9z60zyoIMVKXhrWg==+ dependencies:+ "@types/content-type" "^1.1.3"+ content-type "^1.0.4"+unzipper@^0.10.11:version "0.10.11"resolved "https://registry.npmjs.org/unzipper/-/unzipper-0.10.11.tgz"