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 3 types of social image previews: Twitter, Facebook, LinkedIn

Steven Teyabe4c8e15ae2
diff --git a/components/app/links/link-filters/index.tsx b/components/app/links/link-filters/index.tsx
index a335b83445..882b76267d 100644
--- a/components/app/links/link-filters/index.tsx
+++ b/components/app/links/link-filters/index.tsx
@@ -2,7 +2,7 @@ import { useRouter } from "next/router";
import { useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import { ChevronDown, Filter } from "@/components/shared/icons";
-import { AnimationSettings } from "../../modals/add-edit-link-modal/advanced-settings";
+import { ANIMATION_SETTINGS } from "@/lib/constants";
import LinkSort from "./link-sort";
import StatusFilter from "./status-filter";
import UserFilter from "./user-filter";
@@ -32,7 +32,7 @@ export default function LinkFilters() {
{showFilters && (
<motion.div
className="flex w-full flex-col space-y-2 border-t border-gray-200 py-5 sm:hidden"
- {...AnimationSettings}
+ {...ANIMATION_SETTINGS}
>
<LinkSort />
<StatusFilter />
diff --git a/components/app/modals/add-edit-link-modal/advanced-settings.tsx b/components/app/modals/add-edit-link-modal/advanced-settings.tsx
deleted file mode 100644
index f876ce0173..0000000000
--- a/components/app/modals/add-edit-link-modal/advanced-settings.tsx
+++ /dev/null
@@ -1,57 +0,0 @@
-import { Dispatch, SetStateAction, useMemo, useState } from "react";
-import * as AccordionPrimitive from "@radix-ui/react-accordion";
-import { motion } from "framer-motion";
-import { CheckCircleFill, ChevronRight } from "@/components/shared/icons";
-import { LinkProps } from "@/lib/types";
-import { getParamsFromURL } from "@/lib/utils";
-import ExpirationSection from "./expiration-section";
-import OGSection from "./og-section";
-import PasswordSection from "./password-section";
-import UTMSection from "./utm-section";
-
-export const AnimationSettings = {
- initial: { height: 0 },
- animate: { height: "auto" },
- exit: { height: 0 },
- transition: { duration: 0.2, bounce: 0 },
-};
-
-export default function AdvancedSettings({
- data,
- setData,
-}: {
- data: LinkProps;
- setData: Dispatch<SetStateAction<LinkProps>>;
-}) {
- const { url, title, description, image, password, expiresAt } = data;
-
- const { utm_source, utm_medium, utm_campaign } = useMemo(() => {
- return getParamsFromURL(url);
- }, [url]);
-
- return (
- <div>
- <div className="relative">
- <div
- className="absolute inset-0 flex items-center px-4 sm:px-16"
- aria-hidden="true"
- >
- <div className="w-full border-t border-gray-300" />
- </div>
- <div className="relative flex justify-center">
- <span className="bg-white px-2 text-sm text-gray-500">Optional</span>
- </div>
- </div>
-
- <div className="mt-4 grid px-4 sm:px-16">
- <UTMSection {...{ data, setData }} />
-
- <OGSection {...{ data, setData }} />
-
- <PasswordSection {...{ data, setData }} />
-
- <ExpirationSection {...{ data, setData }} />
- </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 ca43127137..bbe07b5f14 100644
--- a/components/app/modals/add-edit-link-modal/expiration-section.tsx
+++ b/components/app/modals/add-edit-link-modal/expiration-section.tsx
@@ -1,9 +1,8 @@
import { Dispatch, SetStateAction } from "react";
-import { motion } from "framer-motion";
-import { X } from "@/components/shared/icons";
+import { X, QuestionCircle } from "@/components/shared/icons";
import { LinkProps } from "@/lib/types";
import { getDateTimeLocal } from "@/lib/utils";
-import { AnimationSettings } from "./advanced-settings";
+import Tooltip from "@/components/shared/tooltip";
export default function ExpirationSection({
data,
@@ -14,34 +13,44 @@ export default function ExpirationSection({
}) {
const { expiresAt } = data;
return (
- <motion.div key="expire" {...AnimationSettings}>
- <div>
- <label htmlFor="expiresAt" className="my-2 block text-sm text-gray-500">
- Automatically expires your link at a given date and time. Your link
- will be disabled but the data will still be kept.
+ <div>
+ <div className="flex items-center">
+ <label
+ htmlFor="expiresAt"
+ className="text-sm font-medium text-gray-900"
+ >
+ Expiration Date
</label>
- <div className="mb-3 flex space-x-2">
- <input
- type="datetime-local"
- id="expiresAt"
- name="expiresAt"
- min={getDateTimeLocal()}
- value={expiresAt ? getDateTimeLocal(expiresAt) : ""}
- step="60" // need to add step to prevent weird date bug (https://stackoverflow.com/q/19284193/10639526)
- onChange={(e) => {
- setData({ ...data, expiresAt: new Date(e.target.value) });
- }}
- className="flex w-full items-center justify-center space-x-2 rounded-md border border-gray-300 px-3 py-2 text-gray-500 shadow-sm transition-all hover:border-gray-800 focus:outline-none sm:text-sm"
- />
- <button
- onClick={() => setData({ ...data, expiresAt: null })}
- type="button"
- className="group flex h-10 w-10 items-center justify-center rounded-md border text-gray-500 transition-all hover:border-gray-800 hover:text-gray-800 focus:outline-none"
- >
- <X className="h-4 w-4 text-gray-400 transition-all group-hover:text-black" />
- </button>
- </div>
+ <Tooltip
+ content="Automatically expires your link at a given date and time. Your link
+ will be disabled but the data will still be kept."
+ >
+ <div className="flex h-4 w-8 justify-center">
+ <QuestionCircle className="h-4 w-4 text-gray-600" />
+ </div>
+ </Tooltip>
</div>
- </motion.div>
+ <div className="mt-1 flex space-x-2">
+ <input
+ type="datetime-local"
+ id="expiresAt"
+ name="expiresAt"
+ min={getDateTimeLocal()}
+ value={expiresAt ? getDateTimeLocal(expiresAt) : ""}
+ step="60" // need to add step to prevent weird date bug (https://stackoverflow.com/q/19284193/10639526)
+ onChange={(e) => {
+ setData({ ...data, expiresAt: new Date(e.target.value) });
+ }}
+ className="flex w-full items-center justify-center space-x-2 rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-500 shadow-sm transition-all hover:border-gray-800 focus:border-gray-800 focus:outline-none focus:ring-gray-500"
+ />
+ <button
+ onClick={() => setData({ ...data, expiresAt: null })}
+ type="button"
+ className="group flex h-10 w-10 items-center justify-center rounded-md border text-gray-500 transition-all hover:border-gray-800 hover:text-gray-800 focus:outline-none"
+ >
+ <X className="h-4 w-4 text-gray-400 transition-all group-hover:text-black" />
+ </button>
+ </div>
+ </div>
);
}
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsx
index 5b4948394f..7123086a60 100644
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -23,7 +23,11 @@ import useProject from "@/lib/swr/use-project";
import useUsage from "@/lib/swr/use-usage";
import { LinkProps } from "@/lib/types";
import { getApexDomain, getQueryString, linkConstructor } from "@/lib/utils";
-import AdvancedSettings from "./advanced-settings";
+import ExpirationSection from "./expiration-section";
+import OGSection from "./og-section";
+import PasswordSection from "./password-section";
+import UTMSection from "./utm-section";
+import Preview from "./preview";
function AddEditLinkModal({
showAddEditLinkModal,
@@ -63,28 +67,7 @@ function AddEditLinkModal({
createdAt: new Date(),
},
);
- const { id, key, url, archived, expiresAt } = data;
-
- const heroProps = useMemo(() => {
- if (props?.url) {
- const apexDomain = getApexDomain(props.url);
- return {
- avatar: `https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`,
- alt: apexDomain,
- copy: `Edit ${linkConstructor({
- key: props.key,
- domain,
- pretty: true,
- })}`,
- };
- } else {
- return {
- avatar: "/_static/logo.png",
- alt: "Dub.sh",
- copy: "Add a new link",
- };
- }
- }, [props]);
+ const { key, url, expiresAt } = data;
const [debouncedKey] = useDebounce(key, 500);
useEffect(() => {
@@ -114,6 +97,39 @@ function AddEditLinkModal({
setGeneratingSlug(false);
}, []);
+ const [generatingMetatags, setGeneratingMetatags] = useState(false);
+
+ const [debouncedUrl] = useDebounce(url, 500);
+ useEffect(() => {
+ try {
+ new URL(debouncedUrl);
+ if (
+ (!props?.image || debouncedUrl !== props?.url) &&
+ showAddEditLinkModal
+ ) {
+ setGeneratingMetatags(true);
+ // using a setTimeout to delay until the loading screen fades in (0.2 delay in framer motion as well)
+ setTimeout(() => {
+ setData((prev) => ({
+ ...prev,
+ title: null,
+ description: null,
+ image: null,
+ }));
+ }, 200);
+ fetch(`/api/edge/metatags?url=${debouncedUrl}`).then(async (res) => {
+ if (res.status === 200) {
+ const results = await res.json();
+ setData((prev) => ({ ...prev, ...results }));
+ }
+ setGeneratingMetatags(false);
+ });
+ }
+ } catch (e) {
+ console.log("not a valid url");
+ }
+ }, [debouncedUrl]);
+
const endpoint = useMemo(() => {
if (props?.key) {
return {
@@ -140,17 +156,17 @@ function AddEditLinkModal({
setShowModal={setShowAddEditLinkModal}
closeWithX={true}
>
- <div className="grid w-full grid-cols-5 divide-x divide-gray-100 bg-white shadow-xl transition-all sm:max-w-screen-lg sm:rounded-2xl sm:border sm:border-gray-200">
- {/* {!hideXButton && (
+ <div className="grid max-h-[80vh] w-full divide-x divide-gray-100 overflow-scroll bg-white shadow-xl transition-all scrollbar-hide sm:max-w-screen-lg sm:grid-cols-2 sm:rounded-2xl sm:border sm:border-gray-200">
+ {!hideXButton && (
<button
onClick={() => setShowAddEditLinkModal(false)}
className="group absolute top-0 right-0 m-3 hidden rounded-full p-2 text-gray-500 transition-all duration-75 hover:bg-gray-100 focus:outline-none active:bg-gray-200 sm:block"
>
<X className="h-5 w-5" />
</button>
- )} */}
+ )}
- <div className="col-span-3 max-h-[80vh] overflow-scroll rounded-l-2xl">
+ <div className="max-h-[80vh] overflow-scroll rounded-l-2xl">
{/* <div className="flex items-center justify-center space-x-3 border-b border-gray-200 py-5 px-4 sm:px-16">
<div className="relative flex w-full items-center">
<BlurImage
@@ -175,13 +191,27 @@ function AddEditLinkModal({
</div> */}
<div className="sticky top-0 z-10 flex flex-col items-center justify-center space-y-3 border-b border-gray-200 bg-white px-4 pt-10 pb-8 sm:px-16">
<BlurImage
- src={heroProps.avatar}
- alt={heroProps.alt}
+ src={
+ data.url || props
+ ? `https://www.google.com/s2/favicons?sz=64&domain_url=${getApexDomain(
+ data.url || props.url,
+ )}`
+ : "/_static/logo.png"
+ }
+ alt="Logo"
className="h-10 w-10 rounded-full"
width={20}
height={20}
/>
- <h3 className="text-lg font-medium">{heroProps.copy}</h3>
+ <h3 className="text-lg font-medium">
+ {props
+ ? `Edit ${linkConstructor({
+ key: props.key,
+ domain,
+ pretty: true,
+ })}`
+ : "Add a new link"}
+ </h3>
</div>
{/* {id && (
@@ -230,7 +260,7 @@ function AddEditLinkModal({
}
});
}}
- className="grid gap-6 bg-gray-50 py-8"
+ className="grid gap-6 bg-gray-50 pt-8"
>
<div className="grid gap-6 px-4 sm:px-16">
<div>
@@ -256,7 +286,7 @@ function AddEditLinkModal({
</button>
</div>
<div className="relative mt-1 flex rounded-md shadow-sm">
- <span className="inline-flex items-center whitespace-nowrap rounded-l-md border border-r-0 border-gray-300 bg-gray-50 px-5 text-gray-500 sm:text-sm">
+ <span className="inline-flex items-center whitespace-nowrap rounded-l-md border border-r-0 border-gray-300 bg-gray-50 px-5 text-sm text-gray-500">
{domain || "dub.sh"}
</span>
<input
@@ -270,7 +300,7 @@ function AddEditLinkModal({
keyExistsError
? "border-red-300 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
: "border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"
- } block w-full rounded-r-md pr-10 focus:outline-none sm:text-sm`}
+ } block w-full rounded-r-md pr-10 text-sm focus:outline-none`}
placeholder="github"
value={key}
onChange={(e) => {
@@ -319,7 +349,7 @@ function AddEditLinkModal({
urlError
? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
: "border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"
- } block w-full rounded-md focus:outline-none sm:text-sm`}
+ } block w-full rounded-md text-sm focus:outline-none`}
aria-invalid="true"
/>
{urlError && (
@@ -339,9 +369,32 @@ function AddEditLinkModal({
</div>
</div>
- <AdvancedSettings data={data} setData={setData} />
+ {/* Divider */}
+ <div className="relative py-5">
+ <div
+ className="absolute inset-0 flex items-center px-4 sm:px-16"
+ aria-hidden="true"
+ >
+ <div className="w-full border-t border-gray-300" />
+ </div>
+ <div className="relative flex justify-center">
+ <span className="bg-gray-50 px-2 text-sm text-gray-500">
+ Optional
+ </span>
+ </div>
+ </div>
- <div className="px-4 sm:px-16">
+ <div className="grid gap-8 px-4 sm:px-16">
+ <OGSection
+ {...{ data, setData }}
+ generatingMetatags={generatingMetatags}
+ />
+ <UTMSection {...{ data, setData }} />
+ <PasswordSection {...{ data, setData }} />
+ <ExpirationSection {...{ data, setData }} />
+ </div>
+
+ <div className="sticky bottom-0 bg-gray-50 px-4 py-8 sm:px-16">
<button
disabled={saving || keyExistsError}
className={`${
@@ -361,7 +414,9 @@ function AddEditLinkModal({
</div>
</form>
</div>
- <div className="col-span-2 rounded-r-2xl">preview</div>
+ <div className="max-h-[80vh] overflow-scroll rounded-r-2xl">
+ <Preview data={data} generatingMetatags={generatingMetatags} />
+ </div>
</div>
</Modal>
);
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 605f5bca0e..36ca56db61 100644
--- a/components/app/modals/add-edit-link-modal/og-section.tsx
+++ b/components/app/modals/add-edit-link-modal/og-section.tsx
@@ -2,43 +2,31 @@ import { Dispatch, SetStateAction, useCallback, useState } from "react";
import { motion } from "framer-motion";
import TextareaAutosize from "react-textarea-autosize";
import BlurImage from "@/components/shared/blur-image";
-import { LoadingCircle, UploadCloud } from "@/components/shared/icons";
+import {
+ LoadingCircle,
+ QuestionCircle,
+ UploadCloud,
+} from "@/components/shared/icons";
import { LinkProps } from "@/lib/types";
-import { AnimationSettings } from "./advanced-settings";
+import Tooltip from "@/components/shared/tooltip";
+
+const ANIMATION_SETTINGS = {
+ initial: { opacity: 0 },
+ animate: { opacity: 1 },
+ exit: { opacity: 0 },
+ transition: { duration: 0.2 },
+};
export default function OGSection({
data,
setData,
+ generatingMetatags,
}: {
data: LinkProps;
setData: Dispatch<SetStateAction<LinkProps>>;
+ generatingMetatags: boolean;
}) {
- const [generatingTitle, setGeneratingTitle] = useState(false);
- const [generatingDescription, setGeneratingDescription] = useState(false);
-
- const { url, title, description, image } = data;
-
- const generateTitleFromUrl = async () => {
- setGeneratingTitle(true);
- fetch(`/api/edge/title?url=${url}`).then(async (res) => {
- if (res.status === 200) {
- const results = await res.json();
- setData((prev) => ({ ...prev, title: results }));
- setGeneratingTitle(false);
- }
- });
- };
-
- const generateDescriptionFromUrl = async () => {
- setGeneratingDescription(true);
- fetch(`/api/edge/description?url=${url}`).then(async (res) => {
- if (res.status === 200) {
- const results = await res.json();
- setData((prev) => ({ ...prev, description: results }));
- setGeneratingDescription(false);
- }
- });
- };
+ const { title, description, image } = data;
const [fileSizeTooBig, setFileSizeTooBig] = useState(false);
@@ -60,108 +48,55 @@ export default function OGSection({
},
[setData],
);
- return (
- <motion.div key="og" className="grid gap-5" {...AnimationSettings}>
- <p className="mt-2 block px-5 text-sm text-gray-500">
- If you use custom social previews,{" "}
- <span className="font-semibold text-black">
- be sure to set all 3 sections (title, description, image)
- </span>
- , or the default OG tags of the target URL will be used.
- </p>
- <div className="border-t border-gray-200 px-5 pt-5 pb-2.5">
- <div className="flex items-center justify-between">
- <label
- htmlFor="title"
- className="block text-sm font-medium text-gray-700"
- >
- Link Title
- </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-sm text-gray-500 transition-all duration-75`}
- onClick={() => generateTitleFromUrl()}
- disabled={url.length === 0 || generatingTitle}
- type="button"
- >
- {generatingTitle && <LoadingCircle />}
- <p>{generatingTitle ? "Generating" : "Generate from URL"}</p>
- </button>
- </div>
- <div className="mt-1 flex rounded-md shadow-sm">
- <TextareaAutosize
- name="title"
- id="title"
- minRows={3}
- className="block w-full rounded-md border-gray-300 pr-10 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="Dub - Open Source Bitly Alternative"
- value={title || ""}
- onChange={(e) => {
- setData({ ...data, title: e.target.value });
- }}
- aria-invalid="true"
- />
- </div>
- </div>
- <div className="border-t border-gray-200 px-5 pt-5 pb-2.5">
- <div className="flex items-center justify-between">
- <label
- htmlFor="description"
- className="block text-sm font-medium text-gray-700"
- >
- Link 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-sm text-gray-500 transition-all duration-75`}
- onClick={() => generateDescriptionFromUrl()}
- disabled={url.length === 0 || generatingDescription}
- type="button"
- >
- {generatingDescription && <LoadingCircle />}
- <p>{generatingDescription ? "Generating" : "Generate from URL"}</p>
- </button>
- </div>
- <div className="mt-1 flex rounded-md shadow-sm">
- <TextareaAutosize
- name="description"
- id="description"
- minRows={3}
- className="block w-full rounded-md border-gray-300 pr-10 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="Dub is open-source link management tool for modern marketing teams to create, share, and track short links."
- value={description || ""}
- onChange={(e) => {
- setData({
- ...data,
- description:
- e.target.value.length > 0 ? e.target.value : undefined,
- });
- }}
- aria-invalid="true"
- />
- </div>
+ const randomIdx = Math.floor(Math.random() * 100);
+
+ return (
+ <div className="grid gap-5">
+ <div className="flex items-center">
+ <h2 className="text-sm font-medium text-gray-900">
+ Custom Social Media Cards
+ </h2>
+ <Tooltip
+ content={
+ <div className="max-w-sm p-3">
+ <p className="mt-2 block text-sm text-gray-500">
+ If you use custom social previews,{" "}
+ <span className="font-semibold text-black">
+ be sure to set all 3 sections (title, description, image)
+ </span>
+ , or the default OG tags of the target URL will be used.
+ </p>
+ </div>
+ }
+ >
+ <div className="flex h-4 w-8 justify-center">
+ <QuestionCircle className="h-4 w-4 text-gray-600" />
+ </div>
+ </Tooltip>
</div>
- <div className="border-t border-gray-200 px-5 pt-5 pb-2.5">
+ <div>
<div className="flex items-center justify-between">
- <p className="block text-sm font-medium text-gray-700">Link Image</p>
+ <p className="block text-sm font-medium text-gray-700">Image</p>
{fileSizeTooBig && (
<p className="text-sm text-red-500">File size too big (max 1MB)</p>
)}
</div>
<label
- htmlFor="image"
- className="group mt-1 flex h-[10.5rem] cursor-pointer flex-col items-center justify-center rounded-md border border-gray-300 bg-white shadow-sm transition-all hover:bg-gray-50"
+ htmlFor={`image-${randomIdx}`}
+ className="group relative mt-1 flex h-[14rem] cursor-pointer flex-col items-center justify-center rounded-md border border-gray-300 bg-white shadow-sm transition-all hover:bg-gray-50"
>
+ {generatingMetatags && (
+ <motion.div
+ className="absolute flex h-full w-full items-center justify-center rounded-md bg-white"
+ {...ANIMATION_SETTINGS}
+ >
+ <LoadingCircle />
+ </motion.div>
+ )}
{image ? (
- typeof image === "string" ? (
+ image.startsWith("https://res.cloudinary.com") ? (
<BlurImage
src={image}
alt="Preview"
@@ -188,7 +123,7 @@ export default function OGSection({
</label>
<div className="mt-1 flex rounded-md shadow-sm">
<input
- id="image"
+ id={`image-${randomIdx}`}
name="image"
type="file"
accept="image/*"
@@ -197,6 +132,72 @@ export default function OGSection({
/>
</div>
</div>
- </motion.div>
+
+ <div>
+ <label
+ htmlFor={`title-${randomIdx}`}
+ className="block text-sm font-medium text-gray-700"
+ >
+ Title
+ </label>
+ <div className="relative mt-1 flex rounded-md shadow-sm">
+ {generatingMetatags && (
+ <motion.div
+ className="absolute flex h-full w-full items-center justify-center rounded-md border border-gray-300 bg-white"
+ {...ANIMATION_SETTINGS}
+ >
+ <LoadingCircle />
+ </motion.div>
+ )}
+ <TextareaAutosize
+ name="title"
+ id={`title-${randomIdx}`}
+ minRows={3}
+ className="block w-full rounded-md border-gray-300 pr-10 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
+ placeholder="Dub - Open Source Bitly Alternative"
+ value={title || ""}
+ onChange={(e) => {
+ setData({ ...data, title: e.target.value });
+ }}
+ aria-invalid="true"
+ />
+ </div>
+ </div>
+
+ <div>
+ <label
+ htmlFor={`description-${randomIdx}`}
+ className="block text-sm font-medium text-gray-700"
+ >
+ Description
+ </label>
+ <div className="relative mt-1 flex rounded-md shadow-sm">
+ {generatingMetatags && (
+ <motion.div
+ className="absolute flex h-full w-full items-center justify-center rounded-md border border-gray-300 bg-white"
+ {...ANIMATION_SETTINGS}
+ >
+ <LoadingCircle />
+ </motion.div>
+ )}
+ <TextareaAutosize
+ name="description"
+ id={`description-${randomIdx}`}
+ minRows={3}
+ className="block w-full rounded-md border-gray-300 pr-10 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
+ placeholder="Dub is open-source link management tool for modern marketing teams to create, share, and track short links."
+ value={description || ""}
+ onChange={(e) => {
+ setData({
+ ...data,
+ description:
+ e.target.value.length > 0 ? e.target.value : undefined,
+ });
+ }}
+ aria-invalid="true"
+ />
+ </div>
+ </div>
+ </div>
);
}
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 ec2b82d6c0..02a05373d1 100644
--- a/components/app/modals/add-edit-link-modal/password-section.tsx
+++ b/components/app/modals/add-edit-link-modal/password-section.tsx
@@ -1,7 +1,7 @@
import { Dispatch, SetStateAction } from "react";
-import { motion } from "framer-motion";
import { LinkProps } from "@/lib/types";
-import { AnimationSettings } from "./advanced-settings";
+import Tooltip from "@/components/shared/tooltip";
+import { QuestionCircle } from "@/components/shared/icons";
export default function PasswordSection({
data,
@@ -12,17 +12,26 @@ export default function PasswordSection({
}) {
const { password } = data;
return (
- <motion.div key="password" {...AnimationSettings}>
- <label htmlFor="password" className="my-2 block text-sm text-gray-500">
- Protect your links with a password. Users will need to enter the
- password to access the link.
- </label>
- <div className="mb-3 flex rounded-md shadow-sm">
+ <div>
+ <div className="flex items-center">
+ <label htmlFor="password" className="text-sm font-medium text-gray-900">
+ Password Protection
+ </label>
+ <Tooltip
+ content="Protect your links with a password. Users will need to enter the
+ password to access the link."
+ >
+ <div className="flex h-4 w-8 justify-center">
+ <QuestionCircle className="h-4 w-4 text-gray-600" />
+ </div>
+ </Tooltip>
+ </div>
+ <div className="mt-1 flex rounded-md shadow-sm">
<input
name="password"
id="password"
type="password"
- 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"
+ className="block w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
value={password || ""}
onChange={(e) => {
setData({ ...data, password: e.target.value });
@@ -30,6 +39,6 @@ export default function PasswordSection({
aria-invalid="true"
/>
</div>
- </motion.div>
+ </div>
);
}
diff --git a/components/app/modals/add-edit-link-modal/preview.tsx b/components/app/modals/add-edit-link-modal/preview.tsx
new file mode 100644
index 0000000000..631ceedb0d
--- /dev/null
+++ b/components/app/modals/add-edit-link-modal/preview.tsx
@@ -0,0 +1,218 @@
+import BlurImage from "@/components/shared/blur-image";
+import { Facebook, LinkedIn, Photo, Twitter } from "@/components/shared/icons";
+import { LinkProps } from "@/lib/types";
+import { useMemo } from "react";
+
+export default function Preview({
+ data,
+ generatingMetatags,
+}: {
+ data: LinkProps;
+ generatingMetatags: boolean;
+}) {
+ const { title, description, image, url, password } = data;
+ let hostname;
+ try {
+ hostname = new URL(url).hostname.replace(/^www\./, "");
+ } catch (e) {
+ hostname = url;
+ }
+
+ const { previewTitle, previewDescription, previewHostname } = useMemo(() => {
+ if (password) {
+ return {
+ previewTitle: "Password Required",
+ previewDescription:
+ "This link is password protected. Please enter the password to view it.",
+ previewHostname: "dub.sh",
+ };
+ } else {
+ return {
+ previewTitle: title,
+ previewDescription: description,
+ previewHostname: hostname,
+ };
+ }
+ }, [password, title, description, hostname]);
+
+ const previewImage = useMemo(() => {
+ if (password) {
+ return (
+ <BlurImage
+ src="/_static/password-protected.png"
+ alt="Preview"
+ width={1200}
+ height={627}
+ className="h-[250px] w-full border-b border-gray-300 object-cover"
+ />
+ );
+ }
+ if (image) {
+ if (image.startsWith("https://res.cloudinary.com")) {
+ return (
+ <BlurImage
+ src={image}
+ alt="Preview"
+ width={1200}
+ height={627}
+ className="h-[250px] w-full border-b border-gray-300 object-cover"
+ />
+ );
+ } else {
+ return (
+ <img
+ src={image}
+ alt="Preview"
+ className="h-[250px] w-full border-b border-gray-300 object-cover"
+ />
+ );
+ }
+ } else {
+ return (
+ <div className="flex h-[250px] w-full flex-col items-center justify-center space-y-4 border-b border-gray-300 bg-gray-100">
+ <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>
+ );
+ }
+ }, [image, password]);
+
+ return (
+ <div className="h-[1200px]">
+ <div className="flex items-center justify-between border-b border-gray-200 px-5 py-10">
+ <h2 className="text-lg font-medium">Preview</h2>
+ <div className="flex items-center"></div>
+ </div>
+ <div className="grid gap-5 p-5">
+ {/* Twitter */}
+ <div>
+ <div className="relative mb-2">
+ <div
+ className="absolute inset-0 flex items-center"
+ aria-hidden="true"
+ >
+ <div className="w-full border-t border-gray-200" />
+ </div>
+ <div className="relative flex justify-center">
+ <div className="flex items-center space-x-2 bg-white px-3">
+ <Twitter className="h-4 w-4 text-[#1DA1F2]" />
+ <p className="text-sm text-gray-400">Twitter</p>
+ </div>
+ </div>
+ </div>
+ <div className="overflow-hidden rounded-md border border-gray-300">
+ {previewImage}
+ <div className="grid gap-1 p-3">
+ {previewHostname ? (
+ <p className="text-sm text-[#536471]">{previewHostname}</p>
+ ) : (
+ <div className="mb-1 h-4 w-24 rounded-md bg-gray-100" />
+ )}
+ {previewTitle ? (
+ <h3 className="truncate text-sm text-[#0f1419]">
+ {previewTitle}
+ </h3>
+ ) : (
+ <div className="mb-1 h-4 w-full rounded-md bg-gray-100" />
+ )}
+ {previewDescription ? (
+ <p className="text-sm text-[#536471] line-clamp-2">
+ {previewDescription}
+ </p>
+ ) : (
+ <div className="grid gap-2">
+ <div className="h-4 w-full rounded-md bg-gray-100" />
+ <div className="h-4 w-48 rounded-md bg-gray-100" />
+ </div>
+ )}
+ </div>
+ </div>
+ </div>
+
+ {/* Facebook */}
+ <div>
+ <div className="relative mb-2">
+ <div
+ className="absolute inset-0 flex items-center"
+ aria-hidden="true"
+ >
+ <div className="w-full border-t border-gray-200" />
+ </div>
+ <div className="relative flex justify-center">
+ <div className="flex items-center space-x-2 bg-white px-3">
+ <Facebook className="h-4 w-4" />
+ <p className="text-sm text-gray-400">Facebook</p>
+ </div>
+ </div>
+ </div>
+ <div className="border border-gray-300">
+ {previewImage}
+ <div className="grid gap-1 bg-[#f2f3f5] p-3">
+ {previewHostname ? (
+ <p className="text-[0.8rem] uppercase text-[#606770]">
+ {previewHostname}
+ </p>
+ ) : (
+ <div className="mb-1 h-4 w-24 rounded-md bg-gray-200" />
+ )}
+ {previewTitle ? (
+ <h3 className="truncate font-semibold text-[#1d2129]">
+ {previewTitle}
+ </h3>
+ ) : (
+ <div className="mb-1 h-5 w-full rounded-md bg-gray-200" />
+ )}
+ {previewDescription ? (
+ <p className="text-sm text-[#606770] line-clamp-2">
+ {previewDescription}
+ </p>
+ ) : (
+ <div className="grid gap-2">
+ <div className="h-4 w-full rounded-md bg-gray-200" />
+ <div className="h-4 w-48 rounded-md bg-gray-200" />
+ </div>
+ )}
+ </div>
+ </div>
+ </div>
+
+ {/* LinkedIn */}
+ <div>
+ <div className="relative mb-2">
+ <div
+ className="absolute inset-0 flex items-center"
+ aria-hidden="true"
+ >
+ <div className="w-full border-t border-gray-200" />
+ </div>
+ <div className="relative flex justify-center">
+ <div className="flex items-center space-x-2 bg-white px-3">
+ <LinkedIn className="h-4 w-4" />
+ <p className="text-sm text-gray-400">LinkedIn</p>
+ </div>
+ </div>
+ </div>
+ <div className="overflow-hidden rounded-[2px] shadow-[0_0_0_1px_rgba(0,0,0,0.15),0_2px_3px_rgba(0,0,0,0.2)]">
+ {previewImage}
+ <div className="grid gap-1 bg-white p-3">
+ {previewTitle ? (
+ <h3 className="truncate font-semibold text-[#000000E6]">
+ {previewTitle}
+ </h3>
+ ) : (
+ <div className="mb-1 h-5 w-full rounded-md bg-gray-200" />
+ )}
+ {previewHostname ? (
+ <p className="text-xs text-[#00000099]">{previewHostname}</p>
+ ) : (
+ <div className="mb-1 h-4 w-24 rounded-md bg-gray-200" />
+ )}
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+}
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 49ae4abdbe..e291334938 100644
--- a/components/app/modals/add-edit-link-modal/utm-section.tsx
+++ b/components/app/modals/add-edit-link-modal/utm-section.tsx
@@ -1,16 +1,13 @@
import { Dispatch, SetStateAction, useMemo } from "react";
-import { motion } from "framer-motion";
-import TextareaAutosize from "react-textarea-autosize";
import { LinkProps } from "@/lib/types";
import { constructURLFromUTMParams, getParamsFromURL } from "@/lib/utils";
-import { AnimationSettings } from "./advanced-settings";
const paramsMetadata = [
- { display: "Source", key: "utm_source", examples: "twitter, facebook" },
- { display: "Medium", key: "utm_medium", examples: "social, email" },
- { display: "Campaign", key: "utm_campaign", examples: "summer_sale" },
- { display: "Term", key: "utm_term", examples: "blue_shoes" },
- { display: "Content", key: "utm_content", examples: "logolink" },
+ { display: "UTM Source", key: "utm_source", examples: "twitter, facebook" },
+ { display: "UTM Medium", key: "utm_medium", examples: "social, email" },
+ { display: "UTM Campaign", key: "utm_campaign", examples: "summer_sale" },
+ { display: "UTM Term", key: "utm_term", examples: "blue_shoes" },
+ { display: "UTM Content", key: "utm_content", examples: "logolink" },
];
export default function UTMSection({
@@ -35,18 +32,12 @@ export default function UTMSection({
}, [url]);
return (
- <motion.div key="og" className="grid gap-5" {...AnimationSettings}>
- <p className="mt-2 block px-5 text-sm text-gray-500">
- If you use custom OG tags,{" "}
- <span className="font-semibold text-black">
- be sure to set the source, medium, and campaign
- </span>
- .
- </p>
- <div className="grid gap-3 border-t border-gray-200 px-5 pt-5 pb-2.5">
+ <div>
+ <h2 className="text-sm font-medium text-gray-900">UTM Builder</h2>
+ <div className="mt-1 grid gap-2">
{paramsMetadata.map(({ display, key, examples }) => (
<div key={key} className="relative mt-1 flex rounded-md shadow-sm">
- <span className="flex w-40 items-center justify-center whitespace-nowrap rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-gray-500 sm:text-sm">
+ <span className="flex w-60 items-center justify-center whitespace-nowrap rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-sm text-gray-500">
{display}
</span>
<input
@@ -56,7 +47,7 @@ export default function UTMSection({
disabled={!isValidUrl}
className={`${
isValidUrl ? "" : "cursor-not-allowed bg-gray-100"
- } 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`}
+ } block w-full rounded-r-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500`}
placeholder={examples}
value={params[key] || ""}
onChange={(e) => {
@@ -72,6 +63,6 @@ export default function UTMSection({
</div>
))}
</div>
- </motion.div>
+ </div>
);
}
diff --git a/components/shared/icons/facebook.tsx b/components/shared/icons/facebook.tsx
new file mode 100644
index 0000000000..9162cfc8c7
--- /dev/null
+++ b/components/shared/icons/facebook.tsx
@@ -0,0 +1,23 @@
+export default function Facebook({
+ className,
+ fill = "#1977f3",
+}: {
+ className?: string;
+ fill?: string;
+}) {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="1365.12"
+ height="1365.12"
+ viewBox="0 0 14222 14222"
+ className={className}
+ >
+ <circle cx="7111" cy="7112" r="7111" fill={fill} />
+ <path
+ d="M9879 9168l315-2056H8222V5778c0-562 275-1111 1159-1111h897V2917s-814-139-1592-139c-1624 0-2686 984-2686 2767v1567H4194v2056h1806v4969c362 57 733 86 1111 86s749-30 1111-86V9168z"
+ fill="#fff"
+ />
+ </svg>
+ );
+}
diff --git a/components/shared/icons/index.tsx b/components/shared/icons/index.tsx
index 551db27e52..ee5b24b0f8 100644
--- a/components/shared/icons/index.tsx
+++ b/components/shared/icons/index.tsx
@@ -11,6 +11,8 @@ export { default as Divider } from "./divider";
export { default as Logo } from "./logo";
export { default as Github } from "./github";
export { default as Twitter } from "./twitter";
+export { default as Facebook } from "./facebook";
+export { default as LinkedIn } from "./linkedin";
export { default as ChevronRight } from "./chevron-right";
export { default as ChevronLeft } from "./chevron-left";
export { default as ChevronDown } from "./chevron-down";
diff --git a/components/shared/icons/linkedin.tsx b/components/shared/icons/linkedin.tsx
new file mode 100644
index 0000000000..bd02370314
--- /dev/null
+++ b/components/shared/icons/linkedin.tsx
@@ -0,0 +1,22 @@
+export default function LinkedIn({
+ className,
+ fill = "#027ab5",
+}: {
+ className?: string;
+ fill?: string;
+}) {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ className={className}
+ >
+ <path
+ fill={fill}
+ d="M19 0h-14c-2.761 0-5 2.239-5 5v14c0 2.761 2.239 5 5 5h14c2.762 0 5-2.239 5-5v-14c0-2.761-2.238-5-5-5zm-11 19h-3v-11h3v11zm-1.5-12.268c-.966 0-1.75-.79-1.75-1.764s.784-1.764 1.75-1.764 1.75.79 1.75 1.764-.783 1.764-1.75 1.764zm13.5 12.268h-3v-5.604c0-3.368-4-3.113-4 0v5.604h-3v-11h3v1.765c1.396-2.586 7-2.777 7 2.476v6.759z"
+ />
+ </svg>
+ );
+}
diff --git a/components/shared/tooltip.tsx b/components/shared/tooltip.tsx
index 0a6937e9c5..5350ce849e 100644
--- a/components/shared/tooltip.tsx
+++ b/components/shared/tooltip.tsx
@@ -40,6 +40,7 @@ export default function Tooltip({
return (
<>
<button
+ type="button"
className={`${fullWidth ? "w-full" : "inline-flex"} sm:hidden`}
onClick={() => setOpenTooltip(true)}
>
diff --git a/lib/constants/index.ts b/lib/constants/index.ts
index 3141a8efcd..d8e3a62783 100644
--- a/lib/constants/index.ts
+++ b/lib/constants/index.ts
@@ -13,6 +13,13 @@ export const FRAMER_MOTION_LIST_ITEM_VARIANTS = {
show: { scale: 1, opacity: 1, transition: { type: "spring" } },
};
+export const ANIMATION_SETTINGS = {
+ initial: { height: 0 },
+ animate: { height: "auto" },
+ exit: { height: 0 },
+ transition: { duration: 0.2, bounce: 0 },
+};
+
export const HOME_HOSTNAMES = new Set([
// comment for better diffs
"dub.sh",
diff --git a/lib/utils.ts b/lib/utils.ts
index 694786c36d..92ea28ab53 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -81,48 +81,6 @@ export function linkConstructor({
return pretty ? link.replace(/^https?:\/\//, "") : link;
}
-export const getTitleFromUrl = async (url: string) => {
- const controller = new AbortController();
- const timeoutId = setTimeout(() => controller.abort(), 2000); // timeout if it takes longer than 2 seconds
- const title = await fetch(url, { signal: controller.signal })
- .then((res) => {
- clearTimeout(timeoutId);
- return res.text();
- })
- .then((body: string) => {
- let match = body.match(/<title>([^<]*)<\/title>/); // regular expression to parse contents of the <title> tag
- if (!match || typeof match[1] !== "string") return "No title found"; // if no title found, return "No title found"
- return match[1];
- })
- .catch((err) => {
- console.log(err);
- return "No title found"; // if there's an error, return "No title found"
- });
- return title;
-};
-
-export const getDescriptionFromUrl = async (url: string) => {
- const controller = new AbortController();
- const timeoutId = setTimeout(() => controller.abort(), 2000); // timeout if it takes longer than 2 seconds
- const description = await fetch(url, { signal: controller.signal })
- .then((res) => {
- clearTimeout(timeoutId);
- return res.text();
- })
- .then((body: string) => {
- let match = body.match(/<meta name="description" content="(.*?)"\/>/g); // regular expression to parse contents of the description meta tag
- if (!match || typeof match[0] !== "string") return "No description found"; // if no title found, return "No title found"
- let description = match[0].match(/content="(.*)"\/>/).pop();
- if (!description) return "No description found";
- return description;
- })
- .catch((err) => {
- console.log(err);
- return "No description found"; // if there's an error, return "No title found"
- });
- return description;
-};
-
export const timeAgo = (timestamp: Date, timeOnly?: boolean): string => {
if (!timestamp) return "never";
return `${ms(Date.now() - new Date(timestamp).getTime())}${
@@ -274,6 +232,15 @@ export const constructURLFromUTMParams = (
}
};
+export const isValidUrl = (url: string) => {
+ try {
+ new URL(url);
+ return true;
+ } catch (e) {
+ return false;
+ }
+};
+
export const getQueryString = (router: NextRouter) => {
const { slug: omit, ...queryWithoutSlug } = router.query as {
slug: string;
diff --git a/pages/api/edge/description.ts b/pages/api/edge/description.ts
deleted file mode 100644
index 03a806b871..0000000000
--- a/pages/api/edge/description.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import type { NextRequest } from "next/server";
-import { getDescriptionFromUrl } from "@/lib/utils";
-
-export const config = {
- runtime: "experimental-edge",
-};
-
-export default async function handler(req: NextRequest) {
- if (req.method === "GET") {
- const url = req.nextUrl.searchParams.get("url");
- if (!url) {
- return new Response("Missing url", { status: 400 });
- }
- const description = await getDescriptionFromUrl(url);
- return new Response(JSON.stringify(description), { status: 200 });
- } else {
- return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
- }
-}
diff --git a/pages/api/edge/metatags.ts b/pages/api/edge/metatags.ts
new file mode 100644
index 0000000000..b6eb6a2d75
--- /dev/null
+++ b/pages/api/edge/metatags.ts
@@ -0,0 +1,61 @@
+import type { NextRequest } from "next/server";
+import { unescape } from "html-escaper";
+
+export const config = {
+ runtime: "experimental-edge",
+};
+
+export default async function handler(req: NextRequest) {
+ if (req.method === "GET") {
+ const url = req.nextUrl.searchParams.get("url");
+ try {
+ new URL(url).hostname;
+ } catch (e) {
+ return new Response("Invalid URL", { status: 400 });
+ }
+ const metatags = await getMetadataFromUrl(url);
+ return new Response(JSON.stringify(metatags), { status: 200 });
+ } else {
+ return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
+ }
+}
+
+const getMetadataFromUrl = async (url: string) => {
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), 5000); // timeout if it takes longer than 5 seconds
+ const metatags = await fetch(url, {
+ signal: controller.signal,
+ headers: {
+ "User-Agent": "dub-bot/1.0",
+ },
+ })
+ .then((res) => {
+ clearTimeout(timeoutId);
+ return res.text();
+ })
+ .then((body: string) => {
+ const obj = {};
+ body.replace(
+ /<meta\s+(?:name|property|itemprop|http-equiv)="([^"]+)"\s+content="([^"]+)"/g,
+ // @ts-ignore
+ (_, key, value) => {
+ obj[key] = unescape(value);
+ },
+ );
+ console.log(obj);
+ return {
+ hostname: new URL(url).hostname,
+ title: obj["title"] || obj["og:title"] || obj["twitter:title"],
+ description:
+ obj["description"] ||
+ obj["og:description"] ||
+ obj["twitter:description"],
+ image: obj["og:image"] || obj["twitter:image"],
+ };
+ })
+ .catch((err) => {
+ console.log(err);
+ return {}; // if there's an error, return ""
+ });
+ return metatags;
+};
diff --git a/pages/api/edge/title.ts b/pages/api/edge/title.ts
deleted file mode 100644
index 1a33bd8164..0000000000
--- a/pages/api/edge/title.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import type { NextRequest } from "next/server";
-import { getTitleFromUrl } from "@/lib/utils";
-
-export const config = {
- runtime: "experimental-edge",
-};
-
-export default async function handler(req: NextRequest) {
- if (req.method === "GET") {
- const url = req.nextUrl.searchParams.get("url");
- if (!url) {
- return new Response("Missing url", { status: 400 });
- }
- const title = await getTitleFromUrl(url);
- return new Response(JSON.stringify(title), { status: 200 });
- } else {
- return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
- }
-}
diff --git a/yarn.lock b/yarn.lock
index 50cb705910..9cf6a86feb 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5974,7 +5974,7 @@ tabbable@^6.0.0:
tailwind-scrollbar-hide@^1.1.7:
version "1.1.7"
- resolved "https://registry.npmjs.org/tailwind-scrollbar-hide/-/tailwind-scrollbar-hide-1.1.7.tgz"
+ resolved "https://registry.yarnpkg.com/tailwind-scrollbar-hide/-/tailwind-scrollbar-hide-1.1.7.tgz#90b481fb2e204030e3919427416650c54f56f847"
integrity sha512-X324n9OtpTmOMqEgDUEA/RgLrNfBF/jwJdctaPZDzB3mppxJk7TLIDmOreEDm1Bq4R9LSPu4Epf8VSdovNU+iA==
tailwindcss@^3.1.2: