The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
started working on og image proxy
Steven Teycb4e60e7c0b5
diff --git a/components/app/add-link-modal.tsx b/components/app/add-link-modal.tsxindex 55c518b2dc..b6f722f90a 100644--- a/components/app/add-link-modal.tsx+++ b/components/app/add-link-modal.tsx@@ -14,24 +14,26 @@ import {LoadingDots,LoadingCircle,AlertCircleFill,+ ChevronRight,} from "@/components/shared/icons";import { useDebounce } from "use-debounce";import TextareaAutosize from "react-textarea-autosize";import { mutate } from "swr";import Tooltip, { TooltipContent } from "@/components/shared/tooltip";+import useProject from "@/lib/swr/use-project";function AddLinkModal({showAddLinkModal,setShowAddLinkModal,- domain,}: {showAddLinkModal: boolean;setShowAddLinkModal: Dispatch<SetStateAction<boolean>>;- domain?: string;}) {const router = useRouter();const { slug } = router.query as { slug: string };+ const { project: { domain, plan } = {} } = useProject();+const [keyExistsError, setKeyExistsError] = useState(false);const [generatingSlug, setGeneratingSlug] = useState(false);const [generatingTitle, setGeneratingTitle] = useState(false);@@ -42,6 +44,8 @@ function AddLinkModal({url: "",title: "",timestamp: Date.now(),+ description: "",+ image: "",});const { key, url, title } = data;@@ -264,6 +268,14 @@ function AddLinkModal({</div></div>+ {plan === "pro" && slug && (+ <AdvancedSettings+ data={data}+ setData={setData}+ debouncedUrl={debouncedUrl}+ />+ )}+<buttondisabled={saving || keyExistsError}className={`${@@ -280,6 +292,95 @@ function AddLinkModal({);}+function AdvancedSettings({ data, setData, debouncedUrl }) {+ const [expanded, setExpanded] = useState(false);+ const [generatingDescription, setGeneratingDescription] = useState(false);++ const { url, description, image } = data;++ useEffect(() => {+ if (debouncedUrl.length > 0 && description.length === 0) {+ // only fetch title if user hasn't entered one+ generateTitleFromUrl(debouncedUrl);+ }+ }, [debouncedUrl]);++ const generateTitleFromUrl = useCallback(+ (debouncedUrl: string) => {+ setGeneratingDescription(true);+ fetch(`/api/edge/description?url=${debouncedUrl}`).then(async (res) => {+ if (res.status === 200) {+ const results = await res.json();+ setData((prev) => ({ ...prev, title: results }));+ setGeneratingDescription(false);+ }+ });+ },+ [debouncedUrl]+ );++ return (+ <div>+ <button+ type="button"+ className="flex items-center"+ onClick={() => setExpanded(!expanded)}+ >+ <ChevronRight+ className={`h-5 w-5 text-gray-600 ${+ expanded ? "rotate-90" : ""+ } transition-all`}+ />+ <p className="text-gray-600 text-sm">Advanced options</p>+ </button>++ {expanded && (+ <div className="mt-4">+ <div>+ <div className="flex justify-between items-center">+ <label+ htmlFor="description"+ className="block text-sm font-medium text-gray-700"+ >+ Description+ </label>+ <button+ className={`${+ url.length === 0+ ? "cursor-not-allowed text-gray-300"+ : "hover:text-black active:scale-95"+ } flex items-center space-x-2 text-gray-500 text-sm transition-all duration-75`}+ onClick={() => generateTitleFromUrl(url)}+ disabled={url.length === 0 || generatingDescription}+ type="button"+ >+ {generatingDescription && <LoadingCircle />}+ <p>+ {generatingDescription ? "Generating" : "Generate from URL"}+ </p>+ </button>+ </div>+ <div className="flex mt-1 rounded-md shadow-sm">+ <TextareaAutosize+ name="description"+ id="description"+ minRows={3}+ className="border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500 pr-10 block w-full rounded-md focus:outline-none sm:text-sm"+ placeholder="Dub is an open-source link shortener SaaS with built-in analytics + free custom domains."+ value={description}+ onChange={(e) => {+ setData({ ...data, description: e.target.value });+ }}+ aria-invalid="true"+ />+ </div>+ </div>+ </div>+ )}+ </div>+ );+}+function AddLinkButton({domainVerified,exceededUsage,@@ -292,7 +393,7 @@ function AddLinkButton({const router = useRouter();const { slug } = router.query as { slug?: string };- return !domainVerified && slug ? (+ return !domainVerified ? (<Tooltipcontent={<TooltipContent@@ -307,7 +408,7 @@ start adding links."Add</div></Tooltip>- ) : exceededUsage && slug ? (+ ) : exceededUsage ? (<Tooltipcontent={<TooltipContent@@ -334,11 +435,9 @@ start adding links."export function useAddLinkModal({domainVerified,exceededUsage,- domain,}: {domainVerified: boolean;exceededUsage: boolean;- domain?: string;}) {const [showAddLinkModal, setShowAddLinkModal] = useState(false);@@ -347,10 +446,9 @@ export function useAddLinkModal({<AddLinkModalshowAddLinkModal={showAddLinkModal}setShowAddLinkModal={setShowAddLinkModal}- domain={domain}/>);- }, [showAddLinkModal, setShowAddLinkModal, domain]);+ }, [showAddLinkModal, setShowAddLinkModal]);const AddLinkButtonCallback = useCallback(() => {return (diff --git a/lib/swr/use-project.ts b/lib/swr/use-project.tsnew file mode 100644index 0000000000..5e3092447e--- /dev/null+++ b/lib/swr/use-project.ts@@ -0,0 +1,23 @@+import useSWR from "swr";+import { fetcher } from "@/lib/utils";+import { useRouter } from "next/router";+import { ProjectProps } from "@/lib/types";++export default function useProject() {+ const router = useRouter();++ const { slug } = router.query as {+ slug: string;+ };++ const { data: project, error } = useSWR<ProjectProps>(+ router.isReady && `/api/projects/${slug}`,+ fetcher+ );++ return {+ project,+ loading: !error && !project,+ error,+ };+}diff --git a/lib/swr/use-usage.ts b/lib/swr/use-usage.tsnew file mode 100644index 0000000000..2e31932eb1--- /dev/null+++ b/lib/swr/use-usage.ts@@ -0,0 +1,33 @@+import useSWR from "swr";+import { useMemo } from "react";+import { fetcher } from "@/lib/utils";+import { useRouter } from "next/router";+import { ProjectProps } from "@/lib/types";++export default function useUsage(project: ProjectProps) {+ const router = useRouter();++ const { slug } = router.query as {+ slug: string;+ };++ const { data: usage, error } = useSWR(+ router.isReady &&+ project &&+ `/api/projects/${slug}/domains/${project.domain}/usage`,+ fetcher+ );++ const exceededUsage = useMemo(() => {+ if (usage && project) {+ return usage > project?.usageLimit;+ }+ }, [usage, project]);++ return {+ usage,+ exceededUsage,+ loading: !error && !usage,+ error,+ };+}diff --git a/lib/types.ts b/lib/types.tsindex 7c5e70ccbd..a6c376708d 100644--- a/lib/types.ts+++ b/lib/types.ts@@ -8,6 +8,8 @@ export interface LinkProps {url: string;title: string;timestamp: number;+ description?: string;+ image?: string;}export interface ProjectProps {diff --git a/lib/upstash.ts b/lib/upstash.tsindex 7d7d3bb9f4..1b24bd08e8 100644--- a/lib/upstash.ts+++ b/lib/upstash.ts@@ -147,6 +147,8 @@ export async function addLink(url: string,key?: string, // if key is provided, it will be usedtitle?: string, // if title is provided, it will be used+ description?: string,+ image?: string,userId?: string // only applicable for dub.sh links) {if (hostname === "dub.sh" && key && RESERVED_KEYS.includes(key)) {diff --git a/pages/api/edge/description.ts b/pages/api/edge/description.tsnew file mode 100644index 0000000000..e69de29bb2diff --git a/pages/api/projects/[slug]/domains/[domain]/links/index.ts b/pages/api/projects/[slug]/domains/[domain]/links/index.tsindex 2783ac9aa4..03a231ea79 100644--- a/pages/api/projects/[slug]/domains/[domain]/links/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/links/index.ts@@ -11,11 +11,18 @@ export default withProjectAuth(// POST /api/links – create a new link} else if (req.method === "POST") {const { domain } = req.query as { domain: string };- let { key, url, title } = req.body;+ let { key, url, title, description, image } = req.body;if (!domain || !url) {return res.status(400).json({ error: "Missing domain or url" });}- const response = await addLink(domain, url, key, title);+ const response = await addLink(+ domain,+ url,+ key,+ title,+ description,+ image+ );if (response === null) {return res.status(400).json({ error: "Key already exists" });}diff --git a/pages/app/[slug]/index.tsx b/pages/app/[slug]/index.tsxindex b8132877ad..9ddd2364e3 100644--- a/pages/app/[slug]/index.tsx+++ b/pages/app/[slug]/index.tsx@@ -1,42 +1,19 @@import AppLayout from "components/layout/app";-import { useRouter } from "next/router";-import useSWR from "swr";-import { fetcher } from "@/lib/utils";import { useMemo } from "react";-import { ProjectProps } from "@/lib/types";+import useProject from "@/lib/swr/use-project";+import useUsage from "@/lib/swr/use-usage";import MaxWidthWrapper from "@/components/shared/max-width-wrapper";import { useAddLinkModal } from "@/components/app/add-link-modal";import LinksContainer from "@/components/app/links-container";import ErrorPage from "next/error";export default function ProjectLinks() {- const router = useRouter();- const { slug } = router.query as {- slug: string;- };-- const { data: project, error } = useSWR<ProjectProps>(- router.isReady && `/api/projects/${slug}`,- fetcher- );-- const { data: usage } = useSWR(- router.isReady &&- project &&- `/api/projects/${slug}/domains/${project.domain}/usage`,- fetcher- );-- const exceededUsage = useMemo(() => {- if (usage && project) {- return usage > project?.usageLimit;- }- }, [usage, project]);+ const { project, error } = useProject();+ const { exceededUsage } = useUsage(project);const { AddLinkModal, AddLinkButton } = useAddLinkModal({domainVerified: project?.domainVerified,exceededUsage,- domain: project?.domain,});// handle error page