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.

separated add-edit-link-modal into individual components

Steven Tey45e0f670b56b
diff --git a/components/app/modals/add-edit-link-modal/advanced-settings.tsx b/components/app/modals/add-edit-link-modal/advanced-settings.tsx
new file mode 100644
index 0000000000..df346193df
--- /dev/null
+++ b/components/app/modals/add-edit-link-modal/advanced-settings.tsx
@@ -0,0 +1,100 @@
+import { Dispatch, SetStateAction, useCallback, useState } from "react";
+import TextareaAutosize from "react-textarea-autosize";
+import BlurImage from "@/components/shared/blur-image";
+import {
+ ChevronRight,
+ LoadingCircle,
+ UploadCloud,
+} from "@/components/shared/icons";
+import { LinkProps } from "@/lib/types";
+import { getDateTimeLocal } from "@/lib/utils";
+import OGSection from "./og-section";
+
+export default function AdvancedSettings({
+ data,
+ setData,
+}: {
+ data: LinkProps;
+ setData: Dispatch<SetStateAction<LinkProps>>;
+}) {
+ const [expanded, setExpanded] = useState(false);
+
+ const { password, expiresAt } = data;
+
+ return (
+ <div>
+ <div className="sm:px-16 px-4">
+ <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>
+ </div>
+
+ {expanded && (
+ <div className="mt-4 grid gap-5 bg-white border-t border-b border-gray-200 sm:px-16 px-4 py-8">
+ {/* OG Tags Section */}
+ <OGSection {...{ data, setData }} />
+
+ {/* Password Protection */}
+ <div>
+ <label
+ htmlFor="password"
+ className="block text-sm font-medium text-gray-700"
+ >
+ Password Protection
+ </label>
+ <div className="flex mt-1 rounded-md shadow-sm">
+ <input
+ name="password"
+ id="password"
+ type="password"
+ className="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"
+ value={password}
+ onChange={(e) => {
+ setData({ ...data, password: e.target.value });
+ }}
+ aria-invalid="true"
+ />
+ </div>
+ </div>
+
+ {/* Expire Link */}
+ <div>
+ <label
+ htmlFor="expiresAt"
+ className="flex justify-between text-sm font-medium text-gray-700"
+ >
+ <p>Auto-expire Link</p>
+ {expiresAt &&
+ new Date().getTime() > new Date(expiresAt).getTime() && (
+ <span className="bg-amber-500 px-2 py-0.5 text-xs text-white uppercase">
+ Expired
+ </span>
+ )}
+ </label>
+ <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 space-x-2 justify-center items-center mt-1 rounded-md shadow-sm border border-gray-300 text-gray-500 hover:border-gray-800 px-3 py-2 w-full focus:outline-none sm:text-sm transition-all"
+ />
+ </div>
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/components/app/modals/add-edit-link-modal.tsx b/components/app/modals/add-edit-link-modal/index.tsx
similarity index 56%
rename from components/app/modals/add-edit-link-modal.tsx
rename to components/app/modals/add-edit-link-modal/index.tsx
index 5feb35e9c3..de20cbcf7f 100644
--- a/components/app/modals/add-edit-link-modal.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -7,23 +7,21 @@ import {
useMemo,
useState,
} from "react";
-import TextareaAutosize from "react-textarea-autosize";
import { mutate } from "swr";
import { useDebounce } from "use-debounce";
import BlurImage from "@/components/shared/blur-image";
import {
AlertCircleFill,
- ChevronRight,
LoadingCircle,
LoadingDots,
- UploadCloud,
} from "@/components/shared/icons";
import Modal from "@/components/shared/modal";
import Tooltip, { TooltipContent } from "@/components/shared/tooltip";
import useProject from "@/lib/swr/use-project";
import useUsage from "@/lib/swr/use-usage";
import { LinkProps } from "@/lib/types";
-import { getApexDomain, getDateTimeLocal, linkConstructor } from "@/lib/utils";
+import { getApexDomain, linkConstructor } from "@/lib/utils";
+import AdvancedSettings from "./advanced-settings";
function AddEditLinkModal({
showAddEditLinkModal,
@@ -141,7 +139,7 @@ function AddEditLinkModal({
setShowModal={setShowAddEditLinkModal}
>
<div className="inline-block w-full sm:max-w-md max-h-[calc(100vh-50px)] overflow-scroll align-middle transition-all transform bg-white sm:border sm:border-gray-200 shadow-xl sm:rounded-2xl">
- {new Date().getTime() > new Date(expiresAt).getTime() && (
+ {expiresAt && new Date().getTime() > new Date(expiresAt).getTime() && (
<span className="absolute top-0 sm:top-5 right-5 bg-amber-500 px-2 py-0.5 text-xs text-white uppercase">
Expired
</span>
@@ -295,252 +293,6 @@ function AddEditLinkModal({
);
}
-function AdvancedSettings({
- data,
- setData,
-}: {
- data: LinkProps;
- setData: Dispatch<SetStateAction<LinkProps>>;
-}) {
- const [expanded, setExpanded] = useState(true);
- const [generatingTitle, setGeneratingTitle] = useState(false);
- const [generatingDescription, setGeneratingDescription] = useState(false);
-
- const { url, title, description, image, password, expiresAt } = 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 onChangePicture = useCallback(
- (e) => {
- const file = e.target.files[0];
- const reader = new FileReader();
- reader.onload = (e) => {
- setData((prev) => ({ ...prev, image: e.target.result as string }));
- };
- reader.readAsDataURL(file);
- },
- [setData],
- );
-
- return (
- <div>
- <div className="sm:px-16 px-4">
- <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>
- </div>
-
- {expanded && (
- <div className="mt-4 grid gap-5 bg-white border-t border-b border-gray-200 sm:px-16 px-4 py-8">
- {/* OG Tags */}
- <div>
- <div className="flex justify-between items-center">
- <label
- htmlFor="title"
- className="block text-sm font-medium text-gray-700"
- >
- OG 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-gray-500 text-sm 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="flex mt-1 rounded-md shadow-sm">
- <TextareaAutosize
- name="title"
- id="title"
- 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 - an open-source link shortener SaaS with built-in analytics + free custom domains."
- value={title}
- onChange={(e) => {
- setData({ ...data, title: e.target.value });
- }}
- aria-invalid="true"
- />
- </div>
- </div>
- <div>
- <div className="flex justify-between items-center">
- <label
- htmlFor="description"
- className="block text-sm font-medium text-gray-700"
- >
- OG 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={() => generateDescriptionFromUrl()}
- 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.length > 0 ? e.target.value : undefined,
- });
- }}
- aria-invalid="true"
- />
- </div>
- </div>
-
- <div>
- <p className="block text-sm font-medium text-gray-700">OG Image</p>
- <label
- htmlFor="image"
- className="group flex flex-col justify-center items-center mt-1 h-[10.5rem] cursor-pointer rounded-md border border-gray-300 bg-white hover:bg-gray-50 shadow-sm transition-all"
- >
- {image ? (
- typeof image === "string" ? (
- <BlurImage
- src={image}
- alt="Preview"
- width={1200}
- height={627}
- className="object-cover h-full w-full rounded-md hover:brightness-95 transition-all"
- />
- ) : (
- <img
- src={image}
- alt="Preview"
- className="object-cover h-full w-full rounded-md hover:brightness-95 transition-all"
- />
- )
- ) : (
- <>
- <UploadCloud className="h-7 w-7 text-gray-500 group-hover:scale-110 group-active:scale-95 transition-all duration-75" />
- <p className="text-gray-500 text-sm mt-2">
- Recommended: 1200 x 627 pixels
- </p>
- <span className="sr-only">OG Image upload</span>
- </>
- )}
- </label>
- <div className="flex mt-1 rounded-md shadow-sm">
- <input
- id="image"
- name="image"
- type="file"
- className="sr-only"
- onChange={onChangePicture}
- />
- </div>
- </div>
-
- {/* Password Protection */}
- <div>
- <label
- htmlFor="password"
- className="block text-sm font-medium text-gray-700"
- >
- Password Protection
- </label>
- <div className="flex mt-1 rounded-md shadow-sm">
- <input
- name="password"
- id="password"
- type="password"
- className="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"
- value={password}
- onChange={(e) => {
- setData({ ...data, password: e.target.value });
- }}
- aria-invalid="true"
- />
- </div>
- </div>
-
- {/* Expire Link */}
- <div>
- <label
- htmlFor="expiresAt"
- className="flex justify-between text-sm font-medium text-gray-700"
- >
- <p>Auto-expire Link</p>
- {new Date().getTime() > new Date(expiresAt).getTime() && (
- <span className="bg-amber-500 px-2 py-0.5 text-xs text-white uppercase">
- Expired
- </span>
- )}
- </label>
- <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 space-x-2 justify-center items-center mt-1 rounded-md shadow-sm border border-gray-300 text-gray-500 hover:border-gray-800 px-3 py-2 w-full focus:outline-none sm:text-sm transition-all"
- />
- </div>
- </div>
- )}
- </div>
- );
-}
-
function AddEditLinkButton({
setShowAddEditLinkModal,
}: {
diff --git a/components/app/modals/add-edit-link-modal/og-section.tsx b/components/app/modals/add-edit-link-modal/og-section.tsx
new file mode 100644
index 0000000000..da7b231738
--- /dev/null
+++ b/components/app/modals/add-edit-link-modal/og-section.tsx
@@ -0,0 +1,178 @@
+import { Dispatch, SetStateAction, useCallback, useState } from "react";
+import TextareaAutosize from "react-textarea-autosize";
+import BlurImage from "@/components/shared/blur-image";
+import { LoadingCircle, UploadCloud } from "@/components/shared/icons";
+import { LinkProps } from "@/lib/types";
+
+export default function OGSection({
+ data,
+ setData,
+}: {
+ data: LinkProps;
+ setData: Dispatch<SetStateAction<LinkProps>>;
+}) {
+ const [expanded, setExpanded] = useState(false);
+ 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 onChangePicture = useCallback(
+ (e) => {
+ const file = e.target.files[0];
+ const reader = new FileReader();
+ reader.onload = (e) => {
+ setData((prev) => ({ ...prev, image: e.target.result as string }));
+ };
+ reader.readAsDataURL(file);
+ },
+ [setData],
+ );
+ return (
+ <>
+ <div>
+ <div className="flex justify-between items-center">
+ <label
+ htmlFor="title"
+ className="block text-sm font-medium text-gray-700"
+ >
+ OG 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-gray-500 text-sm 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="flex mt-1 rounded-md shadow-sm">
+ <TextareaAutosize
+ name="title"
+ id="title"
+ 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 - an open-source link shortener SaaS with built-in analytics + free custom domains."
+ value={title}
+ onChange={(e) => {
+ setData({ ...data, title: e.target.value });
+ }}
+ aria-invalid="true"
+ />
+ </div>
+ </div>
+ <div>
+ <div className="flex justify-between items-center">
+ <label
+ htmlFor="description"
+ className="block text-sm font-medium text-gray-700"
+ >
+ OG 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={() => generateDescriptionFromUrl()}
+ 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.length > 0 ? e.target.value : undefined,
+ });
+ }}
+ aria-invalid="true"
+ />
+ </div>
+ </div>
+
+ <div>
+ <p className="block text-sm font-medium text-gray-700">OG Image</p>
+ <label
+ htmlFor="image"
+ className="group flex flex-col justify-center items-center mt-1 h-[10.5rem] cursor-pointer rounded-md border border-gray-300 bg-white hover:bg-gray-50 shadow-sm transition-all"
+ >
+ {image ? (
+ typeof image === "string" ? (
+ <BlurImage
+ src={image}
+ alt="Preview"
+ width={1200}
+ height={627}
+ className="object-cover h-full w-full rounded-md hover:brightness-95 transition-all"
+ />
+ ) : (
+ <img
+ src={image}
+ alt="Preview"
+ className="object-cover h-full w-full rounded-md hover:brightness-95 transition-all"
+ />
+ )
+ ) : (
+ <>
+ <UploadCloud className="h-7 w-7 text-gray-500 group-hover:scale-110 group-active:scale-95 transition-all duration-75" />
+ <p className="text-gray-500 text-sm mt-2">
+ Recommended: 1200 x 627 pixels
+ </p>
+ <span className="sr-only">OG Image upload</span>
+ </>
+ )}
+ </label>
+ <div className="flex mt-1 rounded-md shadow-sm">
+ <input
+ id="image"
+ name="image"
+ type="file"
+ className="sr-only"
+ onChange={onChangePicture}
+ />
+ </div>
+ </div>
+ </>
+ );
+}