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.

Added geo targeting + link comments

Steven Teyaabb610d4fec
diff --git a/app/dub.sh/changelog/[slug]/page.tsx b/app/dub.sh/changelog/[slug]/page.tsx
index c2279575ba..7962a66c9e 100644
--- a/app/dub.sh/changelog/[slug]/page.tsx
+++ b/app/dub.sh/changelog/[slug]/page.tsx
@@ -83,7 +83,7 @@ export default async function ChangelogPost({
priority // since it's above the fold
placeholder="blur"
blurDataURL={await getBlurDataURL(post.image)}
- className="border border-gray-100 md:rounded-2xl"
+ className="aspect-video border border-gray-100 object-cover md:rounded-2xl"
/>
<div className="mx-5 mb-10 flex items-center justify-between md:mx-0">
<Author username={post.author} />
diff --git a/app/dub.sh/changelog/page.tsx b/app/dub.sh/changelog/page.tsx
index 9cca988789..8b6466301b 100644
--- a/app/dub.sh/changelog/page.tsx
+++ b/app/dub.sh/changelog/page.tsx
@@ -75,7 +75,7 @@ export default function Changelog() {
priority={idx === 0} // since it's above the fold
placeholder="blur"
blurDataURL={await getBlurDataURL(post.image!)}
- className="border border-gray-100 md:rounded-2xl"
+ className="aspect-video border border-gray-100 object-cover md:rounded-2xl"
/>
</Link>
<Link
diff --git a/app/ui/modal.tsx b/app/ui/modal.tsx
index e81b3114e1..49de31d35e 100644
--- a/app/ui/modal.tsx
+++ b/app/ui/modal.tsx
@@ -87,7 +87,7 @@ export default function Modal({
onOpenAutoFocus={(e) => e.preventDefault()}
onCloseAutoFocus={(e) => e.preventDefault()}
className={cn(
- "fixed inset-0 z-50 m-auto hidden max-h-fit w-full max-w-md animate-scale-in overflow-hidden rounded-2xl border border-gray-200 bg-white p-0 shadow-xl md:block",
+ "fixed inset-0 z-40 m-auto hidden max-h-fit w-full max-w-md animate-scale-in overflow-hidden rounded-2xl border border-gray-200 bg-white p-0 shadow-xl md:block",
className,
)}
>
diff --git a/app/ui/modals/add-edit-link-modal/android-section.tsx b/app/ui/modals/add-edit-link-modal/android-section.tsx
index 8169d209ac..e4d1841e8e 100644
--- a/app/ui/modals/add-edit-link-modal/android-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/android-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
diff --git a/app/ui/modals/add-edit-link-modal/expiration-section.tsx b/app/ui/modals/add-edit-link-modal/expiration-section.tsx
index 25781532eb..3307c2e0c7 100644
--- a/app/ui/modals/add-edit-link-modal/expiration-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/expiration-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { getDateTimeLocal } from "#/lib/utils";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
diff --git a/app/ui/modals/add-edit-link-modal/index.tsx b/app/ui/modals/add-edit-link-modal/index.tsx
index bdaad2b019..96d7f2c8ad 100644
--- a/app/ui/modals/add-edit-link-modal/index.tsx
+++ b/app/ui/modals/add-edit-link-modal/index.tsx
@@ -25,7 +25,7 @@ import { LoadingCircle, Logo } from "#/ui/icons";
import Modal from "#/ui/modal";
import Tooltip, { TooltipContent } from "#/ui/tooltip";
import useProject from "#/lib/swr-app/use-project";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import {
getApexDomain,
getUrlWithoutUTMParams,
diff --git a/app/ui/modals/add-edit-link-modal/ios-section.tsx b/app/ui/modals/add-edit-link-modal/ios-section.tsx
index 95b944e2f9..8ed11e1e03 100644
--- a/app/ui/modals/add-edit-link-modal/ios-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/ios-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
diff --git a/app/ui/modals/add-edit-link-modal/og-section.tsx b/app/ui/modals/add-edit-link-modal/og-section.tsx
index be91b26bba..8cec1d3975 100644
--- a/app/ui/modals/add-edit-link-modal/og-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/og-section.tsx
@@ -9,7 +9,7 @@ import { motion } from "framer-motion";
import TextareaAutosize from "react-textarea-autosize";
import { UploadCloud } from "@/components/shared/icons";
import { LoadingCircle } from "#/ui/icons";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";
diff --git a/app/ui/modals/add-edit-link-modal/password-section.tsx b/app/ui/modals/add-edit-link-modal/password-section.tsx
index ba18bd5b16..9ec60fbb47 100644
--- a/app/ui/modals/add-edit-link-modal/password-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/password-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
diff --git a/app/ui/modals/add-edit-link-modal/preview.tsx b/app/ui/modals/add-edit-link-modal/preview.tsx
index 9f91bd9c42..d1ec8899ad 100644
--- a/app/ui/modals/add-edit-link-modal/preview.tsx
+++ b/app/ui/modals/add-edit-link-modal/preview.tsx
@@ -1,7 +1,7 @@
import BlurImage from "#/ui/blur-image";
import { Facebook, LinkedIn, Photo, Twitter } from "@/components/shared/icons";
import { LoadingCircle } from "#/ui/icons";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { getDomainWithoutWWW } from "#/lib/utils";
import { useMemo } from "react";
import { useDebounce } from "use-debounce";
diff --git a/app/ui/modals/add-edit-link-modal/rewrite-section.tsx b/app/ui/modals/add-edit-link-modal/rewrite-section.tsx
index c82b156374..846e27d016 100644
--- a/app/ui/modals/add-edit-link-modal/rewrite-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/rewrite-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { HOME_DOMAIN } from "#/lib/constants";
import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";
diff --git a/app/ui/modals/add-edit-link-modal/utm-section.tsx b/app/ui/modals/add-edit-link-modal/utm-section.tsx
index 088f44e22d..3483d0b160 100644
--- a/app/ui/modals/add-edit-link-modal/utm-section.tsx
+++ b/app/ui/modals/add-edit-link-modal/utm-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { motion } from "framer-motion";
import {
constructURLFromUTMParams,
diff --git a/components/app/links/link-card.tsx b/components/app/links/link-card.tsx
index e6e3618093..0388b4117b 100644
--- a/components/app/links/link-card.tsx
+++ b/components/app/links/link-card.tsx
@@ -13,7 +13,7 @@ import { Chart, Delete, ThreeDots } from "@/components/shared/icons";
import Popover from "#/ui/popover";
import Tooltip, { SimpleTooltipContent, TooltipContent } from "#/ui/tooltip";
import useProject from "#/lib/swr/use-project";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import {
cn,
fetcher,
@@ -25,7 +25,14 @@ import {
} from "#/lib/utils";
import useIntersectionObserver from "#/lib/hooks/use-intersection-observer";
import useDomains from "#/lib/swr/use-domains";
-import { Archive, CopyPlus, Edit3, EyeOff, QrCode } from "lucide-react";
+import {
+ Archive,
+ CopyPlus,
+ Edit3,
+ EyeOff,
+ MessageCircle,
+ QrCode,
+} from "lucide-react";
import punycode from "punycode/";
import { GOOGLE_FAVICON_URL, HOME_DOMAIN } from "#/lib/constants";
import useTags from "#/lib/swr/use-tags";
@@ -34,7 +41,8 @@ import { ModalContext } from "#/ui/modal-provider";
import Number from "#/ui/number";
export default function LinkCard({ props }: { props: LinkProps }) {
- const { key, domain, url, rewrite, createdAt, archived, tagId } = props;
+ const { key, domain, url, rewrite, createdAt, archived, tagId, comments } =
+ props;
const { tags } = useTags();
const tag = useMemo(() => tags?.find((t) => t.id === tagId), [tags, tagId]);
@@ -81,6 +89,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {
setShowAddEditLinkModal: setShowDuplicateLinkModal,
AddEditLinkModal: DuplicateLinkModal,
} = useAddEditLinkModal({
+ // @ts-expect-error
duplicateProps: {
...propsToDuplicate,
key: `${key}-copy`,
@@ -245,6 +254,25 @@ export default function LinkCard({ props }: { props: LinkProps }) {
</a>
)}
<CopyButton url={linkConstructor({ key, domain })} />
+ {comments && (
+ <Tooltip
+ content={
+ <div className="block max-w-sm px-4 py-2 text-center text-sm text-gray-700">
+ {comments}
+ </div>
+ }
+ >
+ <button
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowAddEditLinkModal(true);
+ }}
+ className="group rounded-full bg-gray-100 p-1.5 transition-all duration-75 hover:scale-105 active:scale-95"
+ >
+ <MessageCircle className="h-3.5 w-3.5 text-gray-700" />
+ </button>
+ </Tooltip>
+ )}
{tag?.color && (
<button
onClick={(e) => {
diff --git a/components/app/modals/add-edit-link-modal/android-section.tsx b/components/app/modals/add-edit-link-modal/android-section.tsx
index 8169d209ac..e4d1841e8e 100644
--- a/components/app/modals/add-edit-link-modal/android-section.tsx
+++ b/components/app/modals/add-edit-link-modal/android-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
diff --git a/components/app/modals/add-edit-link-modal/comments-section.tsx b/components/app/modals/add-edit-link-modal/comments-section.tsx
new file mode 100644
index 0000000000..08ab43a544
--- /dev/null
+++ b/components/app/modals/add-edit-link-modal/comments-section.tsx
@@ -0,0 +1,66 @@
+import { Dispatch, SetStateAction, useEffect, useState } from "react";
+import { type Link as LinkProps } from "@prisma/client";
+import TextareaAutosize from "react-textarea-autosize";
+import Switch from "#/ui/switch";
+import { motion } from "framer-motion";
+import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
+import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";
+
+export default function CommentsSection({
+ props,
+ data,
+ setData,
+}: {
+ props?: LinkProps;
+ data: LinkProps;
+ setData: Dispatch<SetStateAction<LinkProps>>;
+}) {
+ const { comments } = data;
+ const [enabled, setEnabled] = useState(!!comments);
+ useEffect(() => {
+ if (enabled) {
+ // if enabling, add previous comments if exists
+ setData({
+ ...data,
+ comments: props?.comments || comments,
+ });
+ } else {
+ // if disabling, remove comments
+ setData({ ...data, comments: null });
+ }
+ }, [enabled]);
+
+ return (
+ <div className="border-b border-gray-200 pb-5">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Comments</h2>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Use comments to add context to your short links – for you and your team."
+ cta="Learn more."
+ href={`${HOME_DOMAIN}/help/article/how-to-create-link#comments`}
+ />
+ }
+ />
+ </div>
+ <Switch fn={() => setEnabled(!enabled)} checked={enabled} />
+ </div>
+ {enabled && (
+ <motion.div className="mt-3" {...FADE_IN_ANIMATION_SETTINGS}>
+ <TextareaAutosize
+ name="comments"
+ minRows={3}
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ placeholder="Add comments"
+ value={comments || ""}
+ onChange={(e) => {
+ setData({ ...data, comments: e.target.value });
+ }}
+ />
+ </motion.div>
+ )}
+ </div>
+ );
+}
diff --git a/components/app/modals/add-edit-link-modal/expiration-section.tsx b/components/app/modals/add-edit-link-modal/expiration-section.tsx
index 7125fff0cb..e6bcf5907e 100644
--- a/components/app/modals/add-edit-link-modal/expiration-section.tsx
+++ b/components/app/modals/add-edit-link-modal/expiration-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { getDateTimeLocal } from "#/lib/utils";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
diff --git a/components/app/modals/add-edit-link-modal/geo-section.tsx b/components/app/modals/add-edit-link-modal/geo-section.tsx
new file mode 100644
index 0000000000..89e469adcc
--- /dev/null
+++ b/components/app/modals/add-edit-link-modal/geo-section.tsx
@@ -0,0 +1,191 @@
+import {
+ Dispatch,
+ SetStateAction,
+ useContext,
+ useEffect,
+ useMemo,
+ useState,
+} from "react";
+import { type Link as LinkProps } from "@prisma/client";
+import Switch from "#/ui/switch";
+import { motion } from "framer-motion";
+import {
+ COUNTRIES,
+ FADE_IN_ANIMATION_SETTINGS,
+ HOME_DOMAIN,
+} from "#/lib/constants";
+import {
+ InfoTooltip,
+ SimpleTooltipContent,
+ TooltipContent,
+} from "#/ui/tooltip";
+import useProject from "#/lib/swr/use-project";
+import { ModalContext } from "#/ui/modal-provider";
+import { cn } from "#/lib/utils";
+import { Trash } from "lucide-react";
+
+export default function GeoSection({
+ props,
+ data,
+ setData,
+}: {
+ props?: LinkProps;
+ data: LinkProps;
+ setData: Dispatch<SetStateAction<LinkProps>>;
+}) {
+ const { plan } = useProject();
+ const { setShowUpgradePlanModal } = useContext(ModalContext);
+ const { geo } = data;
+ const [enabled, setEnabled] = useState(!!geo);
+
+ useEffect(() => {
+ if (enabled) {
+ // if enabling, add previous geo data if exists
+ setData({
+ ...data,
+ geo: props?.geo || {
+ "": "",
+ },
+ });
+ } else {
+ // if disabling, remove geo data
+ setData({ ...data, geo: null });
+ }
+ }, [enabled]);
+
+ const addGeoSelector = () => {
+ setData((prev) => ({
+ ...prev,
+ geo: { ...((prev.geo as object) || {}), "": "" }, // Add an empty entry
+ }));
+ };
+
+ return (
+ <div className="border-b border-gray-200 pb-5">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Geo Targeting</h2>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Redirect your users to different links based on their location."
+ cta="Learn more about geo targeting."
+ href={`${HOME_DOMAIN}/help/article/how-to-create-link#geo-targeting`}
+ />
+ }
+ />
+ </div>
+ <Switch
+ fn={() => setEnabled(!enabled)}
+ checked={enabled}
+ // geo targeting is only available on Dub's Pro plan
+ {...(!plan || plan === "free"
+ ? {
+ disabledTooltip: (
+ <TooltipContent
+ title="Geo targeting is only available on Dub's Pro plan. Upgrade to Pro to use this feature."
+ cta="Upgrade to Pro"
+ onClick={() => setShowUpgradePlanModal(true)}
+ />
+ ),
+ }
+ : {})}
+ />
+ </div>
+ {enabled && (
+ <motion.div className="mt-3 grid gap-2" {...FADE_IN_ANIMATION_SETTINGS}>
+ {geo &&
+ Object.entries(geo).map(([country, url]) => (
+ <GeoSelector
+ key={country}
+ country={country}
+ url={url}
+ setData={setData}
+ />
+ ))}
+ <button
+ type="button"
+ onClick={addGeoSelector}
+ className="mt-2 rounded-md border border-gray-200 bg-white p-1 text-sm text-gray-400 transition-all duration-75 hover:border-gray-400 hover:text-gray-500 active:bg-gray-50"
+ >
+ Add location
+ </button>
+ </motion.div>
+ )}
+ </div>
+ );
+}
+
+const GeoSelector = ({
+ country,
+ url,
+ setData,
+}: {
+ country: string;
+ url: string;
+ setData: Dispatch<SetStateAction<LinkProps>>;
+}) => {
+ const removeGeoSelector = (countryToRemove: string) => {
+ setData((prev) => {
+ const updatedGeo = prev.geo || {};
+ delete updatedGeo[countryToRemove];
+ return { ...prev, geo: updatedGeo };
+ });
+ };
+
+ return (
+ <div className="flex justify-between space-x-2">
+ <div className="relative flex flex-1 rounded-md shadow-sm">
+ <select
+ id={`geo-selector-${country}`}
+ value={country}
+ onChange={(e) => {
+ removeGeoSelector(country);
+ setData((prev) => ({
+ ...prev,
+ geo: {
+ ...((prev.geo as object) || {}),
+ [e.target.value]: url,
+ },
+ }));
+ }}
+ className="flex w-28 items-center justify-center rounded-l-md border border-r-0 border-gray-300 bg-gray-50 pl-3 pr-7 text-center text-sm text-gray-500 focus:border-gray-300 focus:outline-none focus:ring-0"
+ >
+ <option value="" disabled selected={!country}>
+ Country
+ </option>
+ {Object.entries(COUNTRIES).map(([key, value]) => (
+ <option key={key} value={key}>
+ {value}
+ </option>
+ ))}
+ </select>
+ <input
+ type="url"
+ name="geo-url"
+ required
+ autoComplete="off"
+ className="block w-full rounded-r-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ value={url}
+ placeholder="URL"
+ onChange={(e) => {
+ setData((prev) => ({
+ ...prev,
+ geo: {
+ ...((prev.geo as object) || {}),
+ [country]: e.target.value,
+ },
+ }));
+ }}
+ />
+ </div>
+ <button
+ type="button"
+ onClick={() => removeGeoSelector(country)}
+ className="flex h-10 w-10 items-center justify-center rounded-md border border-gray-200 bg-white text-sm text-gray-400 transition-all duration-75 hover:border-gray-400 hover:text-gray-500 active:bg-gray-50"
+ >
+ <Trash size={16} className="text-gray-400" />
+ </button>
+ </div>
+ );
+};
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsx
index f87de7b73b..c5ea18d32f 100644
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -7,6 +7,7 @@ import {
useContext,
useEffect,
useMemo,
+ useRef,
useState,
} from "react";
import { mutate } from "swr";
@@ -17,7 +18,7 @@ import { LoadingCircle, Logo } from "#/ui/icons";
import Modal from "#/ui/modal";
import Tooltip, { TooltipContent } from "#/ui/tooltip";
import useProject from "#/lib/swr/use-project";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import {
cn,
getApexDomain,
@@ -35,11 +36,7 @@ import ExpirationSection from "./expiration-section";
import IOSSection from "./ios-section";
import AndroidSection from "./android-section";
import Preview from "./preview";
-import {
- DEFAULT_LINK_PROPS,
- GOOGLE_FAVICON_URL,
- HOME_DOMAIN,
-} from "#/lib/constants";
+import { DEFAULT_LINK_PROPS, GOOGLE_FAVICON_URL } from "#/lib/constants";
import useDomains from "#/lib/swr/use-domains";
import { toast } from "sonner";
import va from "@vercel/analytics";
@@ -47,6 +44,8 @@ import punycode from "punycode/";
import Button from "#/ui/button";
import { ModalContext } from "#/ui/modal-provider";
import RewriteSection from "./rewrite-section";
+import CommentsSection from "./comments-section";
+import GeoSection from "./geo-section";
function AddEditLinkModal({
showAddEditLinkModal,
@@ -272,6 +271,13 @@ function AddEditLinkModal({
return router.asPath.split("?")[0] === "/welcome";
}, [router.asPath]);
+ const keyRef = useRef<HTMLInputElement>(null);
+ useEffect(() => {
+ if (key && key.endsWith("-copy")) {
+ keyRef.current?.select();
+ }
+ }, [key]);
+
return (
<Modal
showModal={showAddEditLinkModal}
@@ -474,6 +480,7 @@ function AddEditLinkModal({
))}
</select>
<input
+ ref={keyRef}
type="text"
name="key"
id={`key-${randomIdx}`}
@@ -487,12 +494,10 @@ function AddEditLinkModal({
disabled={props && lockKey}
autoComplete="off"
className={cn(
- "block w-full rounded-r-md focus:outline-none sm:text-sm",
+ "block w-full rounded-r-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
{
"border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500":
keyError,
- "border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500":
- !keyError,
"cursor-not-allowed border border-gray-300 bg-gray-100 text-gray-500":
props && lockKey,
},
@@ -541,6 +546,7 @@ function AddEditLinkModal({
<div className="grid gap-5 px-4 md:px-16">
{slug && <TagsSection {...{ props, data, setData }} />}
+ <CommentsSection {...{ props, data, setData }} />
<OGSection
{...{ props, data, setData }}
generatingMetatags={generatingMetatags}
@@ -551,6 +557,7 @@ function AddEditLinkModal({
<ExpirationSection {...{ props, data, setData }} />
<IOSSection {...{ props, data, setData }} />
<AndroidSection {...{ props, data, setData }} />
+ <GeoSection {...{ props, data, setData }} />
</div>
<div
diff --git a/components/app/modals/add-edit-link-modal/ios-section.tsx b/components/app/modals/add-edit-link-modal/ios-section.tsx
index 95b944e2f9..8ed11e1e03 100644
--- a/components/app/modals/add-edit-link-modal/ios-section.tsx
+++ b/components/app/modals/add-edit-link-modal/ios-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
diff --git a/components/app/modals/add-edit-link-modal/og-section.tsx b/components/app/modals/add-edit-link-modal/og-section.tsx
index 37be7b2fc0..f6639b2e77 100644
--- a/components/app/modals/add-edit-link-modal/og-section.tsx
+++ b/components/app/modals/add-edit-link-modal/og-section.tsx
@@ -9,7 +9,7 @@ import { motion } from "framer-motion";
import TextareaAutosize from "react-textarea-autosize";
import { UploadCloud } from "@/components/shared/icons";
import { LoadingCircle } from "#/ui/icons";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";
diff --git a/components/app/modals/add-edit-link-modal/password-section.tsx b/components/app/modals/add-edit-link-modal/password-section.tsx
index ba18bd5b16..9ec60fbb47 100644
--- a/components/app/modals/add-edit-link-modal/password-section.tsx
+++ b/components/app/modals/add-edit-link-modal/password-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { motion } from "framer-motion";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "#/lib/constants";
diff --git a/components/app/modals/add-edit-link-modal/preview.tsx b/components/app/modals/add-edit-link-modal/preview.tsx
index 9f91bd9c42..d1ec8899ad 100644
--- a/components/app/modals/add-edit-link-modal/preview.tsx
+++ b/components/app/modals/add-edit-link-modal/preview.tsx
@@ -1,7 +1,7 @@
import BlurImage from "#/ui/blur-image";
import { Facebook, LinkedIn, Photo, Twitter } from "@/components/shared/icons";
import { LoadingCircle } from "#/ui/icons";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { getDomainWithoutWWW } from "#/lib/utils";
import { useMemo } from "react";
import { useDebounce } from "use-debounce";
diff --git a/components/app/modals/add-edit-link-modal/rewrite-section.tsx b/components/app/modals/add-edit-link-modal/rewrite-section.tsx
index 47160ce252..f1faad2c51 100644
--- a/components/app/modals/add-edit-link-modal/rewrite-section.tsx
+++ b/components/app/modals/add-edit-link-modal/rewrite-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import Switch from "#/ui/switch";
import { HOME_DOMAIN } from "#/lib/constants";
import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";
diff --git a/components/app/modals/add-edit-link-modal/tags-section.tsx b/components/app/modals/add-edit-link-modal/tags-section.tsx
index c95f5f87f5..23fb695875 100644
--- a/components/app/modals/add-edit-link-modal/tags-section.tsx
+++ b/components/app/modals/add-edit-link-modal/tags-section.tsx
@@ -9,7 +9,7 @@ import {
import { Check, ChevronDown, Tag, X } from "lucide-react";
import { Command, useCommandState } from "cmdk";
import TagBadge from "../../links/tag-badge";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import useTags from "#/lib/swr/use-tags";
import { useRouter } from "next/router";
import { toast } from "sonner";
diff --git a/components/app/modals/add-edit-link-modal/unsplash-search.tsx b/components/app/modals/add-edit-link-modal/unsplash-search.tsx
index d5dfa9b63a..b0c6aedb3c 100644
--- a/components/app/modals/add-edit-link-modal/unsplash-search.tsx
+++ b/components/app/modals/add-edit-link-modal/unsplash-search.tsx
@@ -1,4 +1,4 @@
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { LoadingSpinner } from "#/ui/icons";
import { Dispatch, SetStateAction, useState } from "react";
import useSWR from "swr";
diff --git a/components/app/modals/add-edit-link-modal/utm-section.tsx b/components/app/modals/add-edit-link-modal/utm-section.tsx
index e8cc22ea82..e8291a88fa 100644
--- a/components/app/modals/add-edit-link-modal/utm-section.tsx
+++ b/components/app/modals/add-edit-link-modal/utm-section.tsx
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { motion } from "framer-motion";
import {
constructURLFromUTMParams,
diff --git a/components/app/modals/archive-link-modal.tsx b/components/app/modals/archive-link-modal.tsx
index e445c04eb5..21d8904646 100644
--- a/components/app/modals/archive-link-modal.tsx
+++ b/components/app/modals/archive-link-modal.tsx
@@ -9,7 +9,7 @@ import {
import { mutate } from "swr";
import BlurImage from "#/ui/blur-image";
import Modal from "#/ui/modal";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { getApexDomain, getQueryString, linkConstructor } from "#/lib/utils";
import { GOOGLE_FAVICON_URL } from "#/lib/constants";
import { toast } from "sonner";
diff --git a/components/app/modals/delete-link-modal.tsx b/components/app/modals/delete-link-modal.tsx
index 37c1d066fb..d5549dfcb9 100644
--- a/components/app/modals/delete-link-modal.tsx
+++ b/components/app/modals/delete-link-modal.tsx
@@ -9,7 +9,7 @@ import {
import { mutate } from "swr";
import BlurImage from "#/ui/blur-image";
import Modal from "#/ui/modal";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { getApexDomain, getQueryString, linkConstructor } from "#/lib/utils";
import { toast } from "sonner";
import { GOOGLE_FAVICON_URL } from "#/lib/constants";
diff --git a/content/changelog/geo-targeting.mdx b/content/changelog/geo-targeting.mdx
new file mode 100644
index 0000000000..78a720475c
--- /dev/null
+++ b/content/changelog/geo-targeting.mdx
@@ -0,0 +1,13 @@
+---
+title: Geo Targeting
+publishedAt: 2023-08-20
+summary: "You can now redirect your users to different links based on their location."
+image: "https://d2vwwcvoksz7ty.cloudfront.net/changelog/geo-targeting.png"
+author: "steventey"
+---
+
+You can now redirect your users to different links based on their location.
+
+This is useful if you want to do location-based personalization. For example, you can redirect your UK users to a UK-specific landing page, with the rest of the world going to your global landing page.
+
+Learn more about [Geo Targeting](/help/article/how-to-create-link#geo-targeting).
diff --git a/content/changelog/link-comments.mdx b/content/changelog/link-comments.mdx
new file mode 100644
index 0000000000..29c51f9535
--- /dev/null
+++ b/content/changelog/link-comments.mdx
@@ -0,0 +1,11 @@
+---
+title: Link comments
+publishedAt: 2023-08-20
+summary: "You can now use comments to add context to your short links – for you and your team."
+image: "https://d2vwwcvoksz7ty.cloudfront.net/changelog/link-comments.png"
+author: "steventey"
+---
+
+You can now use comments to add context to your short links – for you and your team.
+
+Huge shoutout to [Alex Bass](https://twitter.com/alexhbass) from [Efficient](https://efficient.app/) for this [feature request](https://github.com/steven-tey/dub/issues/297)!
diff --git a/content/help/how-to-create-link.mdx b/content/help/how-to-create-link.mdx
index f610cee81c..041b6f1c87 100644
--- a/content/help/how-to-create-link.mdx
+++ b/content/help/how-to-create-link.mdx
@@ -39,6 +39,7 @@ Here are the difference between the two options:
| **Link history** | 30 days | 30 days - Unlimited ([Pro plan](pro-plan)) |
| **Tags** | Unavailable | Can be used to organize links |
| **Link cloaking** | Unavailable | Can be used to mask destination URL with your short link |
+| **Geo Targeting** | Unavailable | Available on the [Pro plan](pro-plan) |
## The Dub link builder
@@ -66,7 +67,8 @@ Inside the link builder are the following fields:
7. **Password Protection**: Protect your links with a password. [Learn more](#password-protection).
8. **Expiration Date**: Set an expiration date for your links. [Learn more](#expiration-date).
9. **Device Targeting**: Set a custom destination URL for iOS and Android devices. [Learn more](#device-targeting-ios--android).
-10. **Social Previews**: See how your links will look when shared on social media.
+10. **Geo Targeting** (Pro plan only): Redirect your users to different links based on their location. [Learn more](#geo-targeting).
+11. **Social Previews**: See how your links will look when shared on social media.
## Custom social media cards
@@ -191,3 +193,27 @@ For example, you can set a custom destination URL for iOS devices by toggling th
This is particularly useful if you want to send iOS users to the App Store to download your iOS app, and Android users to the Google Play Store to download your Android app.
In both cases, the main **Destination URL** will be used as the fallback URL that will be used if the user's device type does not match the targeting rules.
+
+## Geo Targeting
+
+<Note variant="success">
+ This feature is only available for [Pro plans and above](pro-plan).
+</Note>
+
+Geo Targeting is a powerful feature that lets you redirect your users to different links based on their location.
+
+This is useful if you want to do location-based personalization. For example, you can redirect your UK users to a UK-specific landing page, with the rest of the world going to your global landing page.
+
+To use this feature, toggle the "Geo Targeting" switch in the link builder. This will reveal the following fields:
+
+1. **Country**: The country that you want to target.
+2. **URL**: The URL that you want to redirect your users to if they are from the country that you have selected.
+
+<Image
+ alt="Use Geo Targeting to redirect your users to different links based on their location"
+ src="https://d2vwwcvoksz7ty.cloudfront.net/changelog/geo-targeting.png"
+ width={1404}
+ height={902}
+/>
+
+You can also add multiple geo-targeting rules by clicking on the "Add location" button.
diff --git a/lib/api/links.ts b/lib/api/links.ts
index 9f72cf9ec8..e4760b702d 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -1,7 +1,7 @@
import cloudinary from "cloudinary";
import { DEFAULT_REDIRECTS } from "#/lib/constants";
import prisma from "#/lib/prisma";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { redis } from "#/lib/upstash";
import { getParamsFromURL, nanoid, truncate, validKeyRegex } from "#/lib/utils";
import { isReservedKey } from "#/lib/edge-config";
@@ -201,6 +201,7 @@ export async function addLink(link: LinkProps) {
rewrite,
ios,
android,
+ geo,
} = link;
const hasPassword = password && password.length > 0 ? true : false;
const exat = expiresAt ? new Date(expiresAt).getTime() / 1000 : null;
@@ -225,6 +226,7 @@ export async function addLink(link: LinkProps) {
utm_campaign,
utm_term,
utm_content,
+ geo: geo || undefined,
},
}),
redis.set(
@@ -234,8 +236,9 @@ export async function addLink(link: LinkProps) {
password: hasPassword,
proxy,
...(rewrite && { rewrite: true }),
- ios,
- android,
+ ...(ios && { ios }),
+ ...(android && { android }),
+ ...(geo && { geo }),
},
{
nx: true,
@@ -287,6 +290,7 @@ export async function editLink(
rewrite,
ios,
android,
+ geo,
} = link;
const hasPassword = password && password.length > 0 ? true : false;
const exat = expiresAt ? new Date(expiresAt).getTime() : null;
@@ -317,6 +321,7 @@ export async function editLink(
utm_campaign,
utm_term,
utm_content,
+ geo: geo || undefined,
},
}),
// only upload image to cloudinary if proxy is true and there's an image
@@ -337,8 +342,9 @@ export async function editLink(
password: hasPassword,
proxy,
...(rewrite && { rewrite: true }),
- ios,
- android,
+ ...(ios && { ios }),
+ ...(android && { android }),
+ ...(geo && { geo }),
},
exat ? { exat } : {},
),
diff --git a/lib/auth.ts b/lib/auth.ts
index bb44c127c8..4db3d67905 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import prisma from "#/lib/prisma";
-import { LinkProps, PlanProps, ProjectProps, UserProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
+import { PlanProps, ProjectProps, UserProps } from "#/lib/types";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { createHash } from "crypto";
diff --git a/lib/constants/index.ts b/lib/constants/index.ts
index ae669fb33c..7d63bd0cce 100644
--- a/lib/constants/index.ts
+++ b/lib/constants/index.ts
@@ -1,3 +1,5 @@
+import { type Link as LinkProps } from "@prisma/client";
+
export const LOCALHOST_GEO_DATA = {
city: "San Francisco",
region: "CA",
@@ -140,10 +142,11 @@ export const SPECIAL_APEX_DOMAINS = new Set([
"web.app",
]);
-export const DEFAULT_LINK_PROPS = {
- key: "github",
- url: "https://github.com/steven-tey/dub",
- domain: "dub.sh",
+// @ts-expect-error because we're coercing the type here
+export const DEFAULT_LINK_PROPS: LinkProps = {
+ key: "",
+ url: "",
+ domain: "",
archived: false,
expiresAt: null,
password: null,
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 9ffa5cb7c1..cd5ef0f8f6 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -7,7 +7,7 @@ import {
import { detectBot, getFinalUrl, parse } from "#/lib/middleware/utils";
import { ratelimit, redis } from "#/lib/upstash";
import { recordClick } from "#/lib/tinybird";
-import { DUB_HEADERS, LOCALHOST_IP } from "../constants";
+import { DUB_HEADERS, LOCALHOST_GEO_DATA, LOCALHOST_IP } from "../constants";
import { ipAddress } from "@vercel/edge";
import { isBlacklistedReferrer } from "../edge-config";
@@ -39,6 +39,9 @@ export default async function LinkMiddleware(
}
}
+ const { country } =
+ process.env.VERCEL === "1" && req.geo ? req.geo : LOCALHOST_GEO_DATA;
+
const response = await redis.get<{
url: string;
password?: boolean;
@@ -46,7 +49,9 @@ export default async function LinkMiddleware(
rewrite?: boolean;
ios?: string;
android?: string;
+ geo?: object;
}>(`${domain}:${key}`);
+
const {
url: target,
password,
@@ -54,6 +59,7 @@ export default async function LinkMiddleware(
rewrite,
ios,
android,
+ geo,
} = response || {};
if (target) {
@@ -79,6 +85,12 @@ export default async function LinkMiddleware(
} else if (android && userAgent(req).os?.name === "Android") {
// redirect to Android link if it is specified and the user is on an Android device
return NextResponse.redirect(getFinalUrl(android, { req }), DUB_HEADERS);
+ } else if (geo && country && country in geo) {
+ // redirect to geo-specific link if it is specified and the user is in the specified country
+ return NextResponse.redirect(
+ getFinalUrl(geo[country], { req }),
+ DUB_HEADERS,
+ );
} else {
// regular redirect / rewrite
return rewrite
diff --git a/lib/swr/use-link.ts b/lib/swr/use-link.ts
index e14612ae59..b27dc328ba 100644
--- a/lib/swr/use-link.ts
+++ b/lib/swr/use-link.ts
@@ -1,6 +1,6 @@
import { useRouter } from "next/router";
import useSWR from "swr";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { fetcher } from "#/lib/utils";
export default function useLink() {
diff --git a/lib/swr/use-links.ts b/lib/swr/use-links.ts
index 7433547305..3a3912b26b 100644
--- a/lib/swr/use-links.ts
+++ b/lib/swr/use-links.ts
@@ -1,6 +1,6 @@
import { useRouter } from "next/router";
import useSWR from "swr";
-import { LinkProps } from "#/lib/types";
+import { type Link as LinkProps } from "@prisma/client";
import { fetcher, getQueryString } from "#/lib/utils";
export default function useLinks() {
diff --git a/lib/types.ts b/lib/types.ts
index f6bf6a5130..fc5849bfeb 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -1,38 +1,9 @@
import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
-
export interface SimpleLinkProps {
domain?: string;
key: string;
url: string;
}
-
-export interface LinkProps {
- id?: string;
- domain: string;
- key: string;
- url: string;
- archived: boolean;
- expiresAt: Date | null;
- password: string | null;
-
- proxy: boolean;
- title: string | null;
- description: string | null;
- image: string | null;
-
- rewrite: boolean;
-
- ios: string | null;
- android: string | null;
-
- clicks: number;
- userId?: string | null;
- tagId?: string | null;
-
- createdAt?: Date;
- updatedAt?: Date;
-}
-
export interface TagProps {
id: string;
name: string;
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 60489cbebf..30308a0375 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -41,7 +41,7 @@ export default withLinksAuth(
// POST /api/links – create a new link
} else if (req.method === "POST") {
- let { domain, key, url, rewrite } = req.body;
+ let { domain, key, url, rewrite, geo } = req.body;
if (!domain || !key || !url) {
return res.status(400).end("Missing domain or key or url.");
}
@@ -66,6 +66,15 @@ export default withLinksAuth(
}
}
+ // free plan restrictions
+ if (!project || project.plan === "free") {
+ if (geo) {
+ return res
+ .status(403)
+ .end("You can only use geo targeting on a Pro plan.");
+ }
+ }
+
key = processKey(key);
if (!key) {
return res.status(422).end("Invalid key.");
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 69f0d82a0b..292a985a1a 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -180,6 +180,7 @@ model Link {
// Custom device targeting
ios String? @db.LongText // custom link for iOS devices
android String? @db.LongText // custom link for Android devices
+ geo Json? @db.Json // custom link for specific countries
// User who created the link
user User? @relation(fields: [userId], references: [id])
@@ -201,6 +202,9 @@ model Link {
tag Tag? @relation(fields: [tagId], references: [id])
tagId String?
+ // Comments on the particular shortlink
+ comments String? @db.LongText
+
// TODO: might wanna add indices for utm_source, utm_medium, utm_campaign
@@unique([domain, key])