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.

Moved edit tag to filters section

Steven Tey6e5b20a48fa7
diff --git a/app/ui/modal-provider.tsx b/app/ui/modal-provider.tsx
index eb5a50c851..84a2bf37db 100644
--- a/app/ui/modal-provider.tsx
+++ b/app/ui/modal-provider.tsx
@@ -13,18 +13,15 @@ import useProject from "#/lib/swr/use-project";
import Cookies from "js-cookie";
import { useAddProjectModal } from "@/components/app/modals/add-project-modal";
import { useAcceptInviteModal } from "@/components/app/modals/accept-invite-modal";
-import { useTagLinkModal } from "@/components/app/modals/tag-link-modal";
import { useAddEditDomainModal } from "@/components/app/modals/add-edit-domain-modal";
import { useGoogleOauthModal } from "@/components/app/modals/google-oauth-modal";
export const ModalContext = createContext<{
setShowAddProjectModal: Dispatch<SetStateAction<boolean>>;
setShowAddEditDomainModal: Dispatch<SetStateAction<boolean>>;
- setShowTagLinkModal: Dispatch<SetStateAction<boolean>>;
}>({
setShowAddProjectModal: () => {},
setShowAddEditDomainModal: () => {},
- setShowTagLinkModal: () => {},
});
export default function ModalProvider({ children }: { children: ReactNode }) {
@@ -34,7 +31,6 @@ export default function ModalProvider({ children }: { children: ReactNode }) {
useAcceptInviteModal();
const { setShowAddEditDomainModal, AddEditDomainModal } =
useAddEditDomainModal({});
- const { setShowTagLinkModal, TagLinkModal } = useTagLinkModal({});
const { error, loading } = useProject();
const { data: session } = useSession();
@@ -61,7 +57,6 @@ export default function ModalProvider({ children }: { children: ReactNode }) {
value={{
setShowAddProjectModal,
setShowAddEditDomainModal,
- setShowTagLinkModal,
}}
>
<GoogleOauthModal />
@@ -70,7 +65,6 @@ export default function ModalProvider({ children }: { children: ReactNode }) {
<AcceptInviteModal />
)}
<AddEditDomainModal />
- <TagLinkModal />
{children}
</ModalContext.Provider>
);
diff --git a/components/app/links/link-filters.tsx b/components/app/links/link-filters.tsx
index 53db44b2ba..fb3cdef792 100644
--- a/components/app/links/link-filters.tsx
+++ b/components/app/links/link-filters.tsx
@@ -8,13 +8,13 @@ import {
useState,
} from "react";
import { nFormatter, setQueryString } from "#/lib/utils";
-import { ChevronRight, XCircle, Search } from "lucide-react";
+import { ChevronRight, XCircle, Search, Check, Trash } from "lucide-react";
import useDomains from "#/lib/swr/use-domains";
import { AnimatePresence, motion } from "framer-motion";
import { SWIPE_REVEAL_ANIMATION_SETTINGS } from "#/lib/constants";
import { useDebouncedCallback } from "use-debounce";
import useLinks from "#/lib/swr/use-links";
-import { LoadingSpinner } from "#/ui/icons";
+import { LoadingCircle, LoadingSpinner } from "#/ui/icons";
import useLinksCount from "#/lib/swr/use-links-count";
import punycode from "punycode/";
import Switch from "#/ui/switch";
@@ -22,8 +22,12 @@ import { useSession } from "next-auth/react";
import { toast } from "sonner";
import { ModalContext } from "#/ui/modal-provider";
import useTags from "#/lib/swr/use-tags";
-import TagBadge from "@/components/app/links/tag-badge";
+import TagBadge, { COLORS_LIST } from "@/components/app/links/tag-badge";
import { TagProps } from "#/lib/types";
+import { ThreeDots } from "@/components/shared/icons";
+import Popover from "@/components/shared/popover";
+import IconMenu from "@/components/shared/icon-menu";
+import { mutate } from "swr";
export default function LinkFilters() {
const { primaryDomain } = useDomains();
@@ -243,8 +247,7 @@ const TagsFilter = ({
tagsCount: { tagId: string; _count: number }[];
}) => {
const router = useRouter();
- const { slug } = router.query as { slug?: string };
- const [collapsed, setCollapsed] = useState(true);
+ const [collapsed, setCollapsed] = useState(tags.length === 0 ? true : false);
const [search, setSearch] = useState("");
const [showMore, setShowMore] = useState(false);
@@ -312,7 +315,7 @@ const TagsFilter = ({
.map(({ id, name, color, count }) => (
<div
key={id}
- className="relative flex cursor-pointer items-center space-x-3 rounded-md bg-gray-50 transition-all hover:bg-gray-100"
+ className="group relative flex cursor-pointer items-center space-x-3 rounded-md bg-gray-50 transition-all hover:bg-gray-100"
>
<input
id={id}
@@ -329,7 +332,7 @@ const TagsFilter = ({
className="flex w-full cursor-pointer justify-between px-3 py-1.5 pl-0 text-sm font-medium text-gray-700"
>
<TagBadge name={name} color={color} />
- <p className="text-gray-500">{nFormatter(count)}</p>
+ <TagPopover tag={{ id, name, color }} count={count} />
</label>
</div>
))}
@@ -348,6 +351,149 @@ const TagsFilter = ({
);
};
+const TagPopover = ({ tag, count }: { tag: TagProps; count: number }) => {
+ const router = useRouter();
+ const { slug } = router.query as { slug: string };
+ const [data, setData] = useState(tag);
+ const [openPopover, setOpenPopover] = useState(false);
+ const [processing, setProcessing] = useState(false);
+
+ const handleEdit = async (e) => {
+ e.stopPropagation();
+ e.preventDefault();
+ setProcessing(true);
+ fetch(`/api/projects/${slug}/tags/${tag.id}`, {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(data),
+ }).then((res) => {
+ setProcessing(false);
+ if (res.ok) {
+ toast.success("Tag updated");
+ mutate(`/api/projects/${slug}/tags`);
+ } else {
+ toast.error("Something went wrong");
+ }
+ });
+ };
+
+ const handleDelete = async () => {
+ setProcessing(true);
+ fetch(`/api/projects/${slug}/tags/${tag.id}`, {
+ method: "DELETE",
+ }).then((res) => {
+ setProcessing(false);
+ if (res.ok) {
+ toast.success("Tag deleted");
+ mutate(`/api/projects/${slug}/tags`);
+ } else {
+ toast.error("Something went wrong");
+ }
+ });
+ };
+
+ return processing ? (
+ <LoadingCircle />
+ ) : (
+ <Popover
+ content={
+ <div
+ data-exclude-click
+ className="flex w-48 flex-col divide-y divide-gray-200"
+ >
+ <div className="p-2">
+ <form
+ onClick={(e) => e.stopPropagation()} // prevent triggering <Command.Item> onClick
+ onKeyDown={(e) => e.stopPropagation()} // prevent triggering <Command.Item> onKeyDown
+ onSubmit={handleEdit}
+ className="relative py-1"
+ >
+ <div className="my-2 flex items-center justify-between px-3">
+ <p className="text-xs text-gray-500">Edit Tag</p>
+ {data !== tag && (
+ <button className="text-xs text-gray-500">Save</button>
+ )}
+ </div>
+ <input
+ type="text"
+ autoFocus
+ required
+ onKeyDown={(e) => {
+ // if ESC key pressed, close popover
+ if (e.key === "Escape") {
+ setOpenPopover(false);
+ }
+ }}
+ value={data.name}
+ onChange={(e) => setData({ ...data, name: e.target.value })}
+ className="block w-full rounded-md border-gray-300 py-1 pr-7 text-sm text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
+ />
+ <div className="grid grid-cols-3 gap-3 p-3 pb-0">
+ {COLORS_LIST.map(({ color, css }) => (
+ <button
+ key={color}
+ type="button"
+ className={`mx-auto flex h-6 w-6 items-center justify-center rounded-full transition-all duration-75 hover:scale-110 active:scale-90 ${css}`}
+ onClick={(e) => {
+ e.stopPropagation();
+ setData({ ...data, color });
+ }}
+ >
+ {data.color === color && <Check className="h-4 w-4" />}
+ </button>
+ ))}
+ </div>
+ </form>
+ </div>
+ <div className="p-2">
+ <button
+ onClick={(e) => {
+ e.stopPropagation();
+ confirm(
+ "Are you sure you want to delete this tag? All tagged links will be untagged, but they won't be deleted.",
+ ) && handleDelete();
+ }}
+ className="flex w-full items-center space-x-2 rounded-md p-2 text-red-600 transition-colors hover:bg-red-100 active:bg-red-200"
+ >
+ <IconMenu
+ text="Delete Tag"
+ icon={<Trash className="h-4 w-4 text-red-600" />}
+ />
+ </button>
+ </div>
+ </div>
+ }
+ align="end"
+ desktopOnly
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ >
+ <button
+ type="button"
+ onClick={() => setOpenPopover(!openPopover)}
+ className={`${
+ openPopover ? "bg-gray-200" : "hover:bg-gray-200"
+ } -mr-1 flex h-6 w-5 items-center justify-center rounded-md transition-colors`}
+ >
+ <ThreeDots
+ className={`h-4 w-4 text-gray-500 ${
+ openPopover ? "" : "hidden group-hover:block"
+ }`}
+ />
+ <p
+ className={`text-gray-500 ${
+ openPopover ? "hidden" : "group-hover:hidden"
+ }`}
+ >
+ {nFormatter(count)}
+ </p>
+ </button>
+ </Popover>
+ );
+};
+
const MyLinksFilter = () => {
const router = useRouter();
const { userId } = router.query as { userId?: string };
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 2e202fa74a..b5a5464ea6 100644
--- a/components/app/modals/add-edit-link-modal/tags-section.tsx
+++ b/components/app/modals/add-edit-link-modal/tags-section.tsx
@@ -6,18 +6,16 @@ import {
useRef,
useState,
} from "react";
-import { Check, Search, Trash, X } from "lucide-react";
-import { Command } from "cmdk";
-import TagBadge, { COLORS_LIST } from "../../links/tag-badge";
-import { LinkProps, TagProps } from "#/lib/types";
+import { Check, Search, X } from "lucide-react";
+import { Command, useCommandState } from "cmdk";
+import TagBadge from "../../links/tag-badge";
+import { LinkProps } from "#/lib/types";
import useTags from "#/lib/swr/use-tags";
import { useRouter } from "next/router";
import { toast } from "sonner";
import { mutate } from "swr";
import { LoadingCircle } from "#/ui/icons";
-import Popover from "@/components/shared/popover";
-import IconMenu from "@/components/shared/icon-menu";
-import { ChevronDown, ThreeDots } from "@/components/shared/icons";
+import { ChevronDown } from "@/components/shared/icons";
export default function TagsSection({
data,
@@ -26,6 +24,8 @@ export default function TagsSection({
data: LinkProps;
setData: Dispatch<SetStateAction<LinkProps>>;
}) {
+ const router = useRouter();
+ const { slug } = router.query;
const { tagId } = data;
const { tags } = useTags();
@@ -36,9 +36,29 @@ export default function TagsSection({
}, [tagId, tags]);
const [inputValue, setInputValue] = useState("");
-
const [creatingTag, setCreatingTag] = useState(false);
+ const createTag = async (tag: string) => {
+ setCreatingTag(true);
+ fetch(`/api/projects/${slug}/tags`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ tag }),
+ }).then(async (res) => {
+ if (res.ok) {
+ mutate(`/api/projects/${slug}/tags`);
+ const newTag = await res.json();
+ setData({ ...data, tagId: newTag.id });
+ toast.success(`Successfully created tag!`);
+ setCreatingTag(false);
+ } else {
+ toast.error(`Error creating tag!`);
+ }
+ });
+ };
+
const commandRef = useRef<HTMLDivElement | null>(null);
const [openCommandList, setOpenCommandList] = useState(false);
@@ -54,6 +74,35 @@ export default function TagsSection({
}
}, [commandRef, openCommandList]);
+ const CommandInput = () => {
+ const isEmpty = useCommandState((state) => state.filtered.count === 0);
+ return (
+ <Command.Input
+ placeholder="Choose a tag"
+ // hack to focus on the input when the dropdown opens
+ autoFocus={openCommandList}
+ // when focus on the input. only show the dropdown if there are tags and the tagValue is not empty
+ onFocus={() => tags && tags.length > 0 && setOpenCommandList(true)}
+ value={inputValue}
+ onValueChange={setInputValue}
+ onKeyDown={(e) => {
+ if (e.key === "Escape") {
+ setOpenCommandList(false);
+
+ // listen for cases where empty results and enter is pressed
+ } else if (e.key === "Enter" && isEmpty) {
+ setOpenCommandList(false);
+ createTag(inputValue);
+ // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
+ } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
+ setOpenCommandList(true);
+ }
+ }}
+ className="block w-full rounded-md border-none px-0 text-sm text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-0"
+ />
+ );
+ };
+
return (
<div className="border-b border-gray-200 pb-5">
<Command ref={commandRef} className="relative" loop>
@@ -65,39 +114,13 @@ export default function TagsSection({
{selectedTag ? (
<TagBadge key={selectedTag.id} {...selectedTag} />
) : (
- <Command.Input
- placeholder="Choose a tag"
- value={inputValue}
- onValueChange={setInputValue}
- // only show the dropdown if there are tags and the tagValue is not empty
- onFocus={() =>
- tags && tags.length > 0 && setOpenCommandList(true)
- }
- onKeyDown={(e) => {
- if (e.key === "Escape") {
- e.stopPropagation();
- setOpenCommandList(false);
- } else if (e.key === "Enter") {
- if (openCommandList) {
- // if dropdown is openCommandList, close it
- setOpenCommandList(false);
- } else {
- // if dropdown is already closed, submit form
- }
- // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
- } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
- setOpenCommandList(true);
- }
- }}
- className="block w-full rounded-md border-none px-0 text-sm text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-0"
- />
+ <CommandInput />
)}
{selectedTag ? (
<button
- onClick={(e) => {
- e.stopPropagation();
+ onClick={() => {
setData({ ...data, tagId: null });
- setOpenCommandList(true);
+ setInputValue("");
}}
className="absolute inset-y-0 right-0 my-auto"
>
@@ -113,25 +136,22 @@ export default function TagsSection({
style={{
animationFillMode: "forwards", // to keep the last frame of the animation
}}
- className="absolute z-20 h-[300px] w-full animate-input-select-slide-up overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all sm:h-auto sm:max-h-[300px] sm:animate-input-select-slide-down"
+ className="absolute z-20 h-[300px] w-full animate-input-select-slide-up overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all sm:h-[calc(var(--cmdk-list-height)+17px)] sm:max-h-[300px] sm:animate-input-select-slide-down"
>
<Command.Empty>
- <button
- type="button"
- onClick={() => setOpenCommandList(false)}
- className="flex w-full cursor-pointer items-center rounded-md bg-gray-100 px-4 py-2 text-sm text-gray-900 hover:text-gray-900 aria-selected:bg-gray-100 aria-selected:text-gray-900"
- >
- {inputValue.length > 0 ? (
- <>
- Create tag{" "}
- <span className="ml-1.5 rounded-md bg-blue-100 px-2 py-0.5 text-blue-600">
- {inputValue}
- </span>
- </>
- ) : (
- <p className="py-0.5">Start typing to create tag...</p>
- )}
- </button>
+ {inputValue.length > 0 ? (
+ <button
+ onClick={() => createTag(inputValue)}
+ className="flex w-full cursor-pointer items-center rounded-md bg-gray-100 px-4 py-2 text-sm text-gray-900 hover:text-gray-900 aria-selected:bg-gray-100 aria-selected:text-gray-900"
+ >
+ Create tag{" "}
+ <span className="ml-1.5 rounded-md bg-blue-100 px-2 py-0.5 text-blue-600">
+ {inputValue}
+ </span>
+ </button>
+ ) : (
+ <p className="py-0.5">Start typing to create tag...</p>
+ )}
</Command.Empty>
{tags?.map((tag) => (
<Command.Item
@@ -155,131 +175,3 @@ export default function TagsSection({
</div>
);
}
-
-const TagPopover = ({ tag }: { tag: TagProps }) => {
- const router = useRouter();
- const { slug } = router.query as { slug: string };
- const [data, setData] = useState(tag);
- const [openPopover, setOpenPopover] = useState(false);
- const [processing, setProcessing] = useState(false);
-
- const handleEdit = async (e) => {
- e.stopPropagation();
- e.preventDefault();
- setProcessing(true);
- fetch(`/api/projects/${slug}/tags/${tag.id}`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- }).then((res) => {
- setProcessing(false);
- if (res.ok) {
- toast.success("Tag updated");
- mutate(`/api/projects/${slug}/tags`);
- } else {
- toast.error("Something went wrong");
- }
- });
- };
-
- const handleDelete = async () => {
- setProcessing(true);
- fetch(`/api/projects/${slug}/tags/${tag.id}`, {
- method: "DELETE",
- }).then((res) => {
- setProcessing(false);
- if (res.ok) {
- toast.success("Tag deleted");
- mutate(`/api/projects/${slug}/tags`);
- } else {
- toast.error("Something went wrong");
- }
- });
- };
-
- return processing ? (
- <LoadingCircle />
- ) : (
- <Popover
- content={
- <div className="flex w-48 flex-col divide-y divide-gray-200">
- <div className="p-2">
- <form
- onClick={(e) => e.stopPropagation()} // prevent triggering <Command.Item> onClick
- onKeyDown={(e) => e.stopPropagation()} // prevent triggering <Command.Item> onKeyDown
- onSubmit={handleEdit}
- className="relative py-1"
- >
- <div className="my-2 flex items-center justify-between px-3">
- <p className="text-xs text-gray-500">Edit Tag</p>
- {data !== tag && (
- <button className="text-xs text-gray-500">Save</button>
- )}
- </div>
- <input
- type="text"
- autoFocus
- required
- value={data.name}
- onChange={(e) => setData({ ...data, name: e.target.value })}
- className="block w-full rounded-md border-gray-300 py-1 pr-7 text-sm text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
- />
- <div className="grid grid-cols-3 gap-3 p-3 pb-0">
- {COLORS_LIST.map(({ color, css }) => (
- <button
- key={color}
- type="button"
- className={`mx-auto flex h-6 w-6 items-center justify-center rounded-full transition-all duration-75 hover:scale-110 active:scale-90 ${css}`}
- onClick={(e) => {
- e.stopPropagation();
- setData({ ...data, color });
- }}
- >
- {data.color === color && <Check className="h-4 w-4" />}
- </button>
- ))}
- </div>
- </form>
- </div>
- <div className="p-2">
- <button
- onClick={(e) => {
- e.stopPropagation();
- confirm(
- "Are you sure you want to delete this tag? All tagged links will be untagged, but they won't be deleted.",
- ) && handleDelete();
- }}
- className="flex w-full items-center space-x-2 rounded-md p-2 text-red-600 transition-colors hover:bg-red-100 active:bg-red-200"
- >
- <IconMenu
- text="Delete Tag"
- icon={<Trash className="h-4 w-4 text-red-600" />}
- />
- </button>
- </div>
- </div>
- }
- align="end"
- desktopOnly
- openPopover={openPopover}
- setOpenPopover={setOpenPopover}
- >
- <button
- type="button"
- onClick={(e) => {
- e.stopPropagation();
- setOpenPopover(!openPopover);
- }}
- className={`${
- openPopover
- ? "bg-gray-200"
- : "hidden hover:bg-gray-200 group-hover:block"
- } rounded-md p-1 transition-colors`}
- >
- <ThreeDots className="h-4 w-4 text-gray-500" />
- </button>
- </Popover>
- );
-};
diff --git a/components/app/modals/tag-link-modal.tsx b/components/app/modals/tag-link-modal.tsx
deleted file mode 100644
index 950ee4a791..0000000000
--- a/components/app/modals/tag-link-modal.tsx
+++ /dev/null
@@ -1,422 +0,0 @@
-import { useRouter } from "next/router";
-import {
- Dispatch,
- SetStateAction,
- SyntheticEvent,
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from "react";
-import { mutate } from "swr";
-import BlurImage from "#/ui/blur-image";
-import Modal from "@/components/shared/modal";
-import { LinkProps, TagProps } from "#/lib/types";
-import { getApexDomain, getQueryString, linkConstructor } from "#/lib/utils";
-import { GOOGLE_FAVICON_URL } from "#/lib/constants";
-import { toast } from "sonner";
-import { Check, ChevronDown, Search, Trash, X } from "lucide-react";
-import { Command } from "cmdk";
-import Button from "#/ui/button";
-import useTags from "#/lib/swr/use-tags";
-import TagBadge, { COLORS_LIST } from "@/components/app/links/tag-badge";
-import va from "@vercel/analytics";
-import { ThreeDots } from "@/components/shared/icons";
-import Popover from "@/components/shared/popover";
-import IconMenu from "@/components/shared/icon-menu";
-import { LoadingCircle } from "#/ui/icons";
-
-function TagLinkModal({
- showTagLinkModal,
- setShowTagLinkModal,
- props,
-}: {
- showTagLinkModal: boolean;
- setShowTagLinkModal: Dispatch<SetStateAction<boolean>>;
- props?: LinkProps;
-}) {
- const router = useRouter();
- const { slug } = router.query;
- const [tagging, setTagging] = useState(false);
- const apexDomain = props?.url ? getApexDomain(props.url) : "dub.sh";
-
- const { domain, tagId } = props || {};
-
- const { tags } = useTags();
- const [tagValue, setTagValue] = useState(
- tags?.find((t) => t.id === tagId)?.name || "",
- );
-
- const saveDisabled = useMemo(() => {
- return tagging ||
- // if the tagId's value is the same as the tagValue, then we don't need to save
- (tagId && tags?.find((t) => t.id === tagId)?.name === tagValue)
- ? true
- : false;
- }, [tagging, tagId, tags, tagValue]);
-
- const type = useMemo(() => {
- if (props) {
- return {
- title: `Tag ${linkConstructor({
- key: props.key,
- domain,
- pretty: true,
- })}`,
- description: "Assign a tag to your short link",
- endpoint: `/api/links/${encodeURIComponent(
- props.key,
- )}/tag?slug=${slug}&domain=${domain}`,
- method: tagValue.length > 0 ? "POST" : "DELETE",
- };
- } else {
- return {
- title: "Add a Tag",
- description: "Tags can be used to group short links together.",
- endpoint: `/api/projects/${slug}/tags`,
- method: "POST",
- };
- }
- }, [props, slug, domain, tagValue]);
-
- const handleSubmit = useCallback(
- async (e?: SyntheticEvent) => {
- e?.preventDefault();
- setTagging(true);
- fetch(type.endpoint, {
- method: type.method,
- headers: {
- "Content-Type": "application/json",
- },
- ...(tagValue.length > 0 && {
- body: JSON.stringify({ tag: tagValue }),
- }),
- }).then(async (res) => {
- setTagging(false);
- if (res.status === 200) {
- mutate(`/api/projects/${slug}/tags`);
- if (props) {
- mutate(`/api/links${getQueryString(router)}`);
- mutate(
- `/api/links/_count${getQueryString(router, {
- groupBy: "tagId",
- })}`,
- );
- setShowTagLinkModal(false);
- toast.success(`Successfully updated shortlink!`);
- tagValue.length > 0 && va.track("Tagged Link");
- } else {
- toast.success(`Successfully added tag!`);
- setOpenCommandList(true);
- va.track("Added Tag");
- }
- } else {
- const error = await res.text();
- toast.error(error);
- }
- });
- },
- [props, tagValue, slug, domain, router],
- );
-
- const commandRef = useRef<HTMLDivElement | null>(null);
- const [openCommandList, setOpenCommandList] = useState(false);
-
- useEffect(() => {
- const handleClickOutside = (e) => {
- if (
- commandRef.current &&
- !commandRef.current.contains(e.target) &&
- // if the target is not part of a div that has
- // the data-exclude-click attribute
- e.target.closest("[data-exclude-click]") === null
- ) {
- setOpenCommandList(false);
- }
- };
- if (showTagLinkModal && openCommandList) {
- document.addEventListener("click", handleClickOutside);
- return () => document.removeEventListener("click", handleClickOutside);
- }
- }, [commandRef, showTagLinkModal, openCommandList]);
-
- return (
- <Modal showModal={showTagLinkModal} setShowModal={setShowTagLinkModal}>
- <div className="inline-block w-full bg-white align-middle shadow-xl transition-all sm:max-w-md sm:rounded-2xl sm:border sm:border-gray-200">
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
- <BlurImage
- src={`${GOOGLE_FAVICON_URL}${apexDomain}`}
- alt={apexDomain}
- className="h-10 w-10 rounded-full"
- width={20}
- height={20}
- />
- <h3 className="text-lg font-medium">{type.title}</h3>
- <p className="text-sm text-gray-500">{type.description}</p>
- </div>
-
- <form
- className="flex flex-col space-y-4 bg-gray-50 px-4 py-8 text-left sm:rounded-b-2xl sm:px-16"
- onSubmit={handleSubmit}
- >
- <Command
- ref={commandRef}
- className="relative"
- loop
- shouldFilter={
- tagValue.length > 0 && !tags?.find((t) => t.name === tagValue)
- }
- >
- <div className="relative mt-1 w-full rounded-md">
- <Search className="absolute inset-y-0 left-0 my-auto h-7 w-7 pl-3 text-gray-400" />
- <Command.Input
- placeholder={props ? "Choose tag" : "Add a tag"}
- required={!props}
- // only show the dropdown if there are tags and the tagValue is not empty
- onFocus={() =>
- tags && tags.length > 0 && setOpenCommandList(true)
- }
- value={tagValue}
- onValueChange={setTagValue}
- onKeyDown={(e) => {
- if (e.key === "Escape") {
- e.stopPropagation();
- setOpenCommandList(false);
- } else if (e.key === "Enter") {
- if (openCommandList) {
- // if dropdown is openCommandList, close it
- setOpenCommandList(false);
- } else {
- // if dropdown is already closed, submit form
- handleSubmit();
- }
- // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
- } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
- setOpenCommandList(true);
- }
- }}
- className="block w-full rounded-md border-gray-300 px-10 text-sm text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
- />
- {tagValue ? (
- <button
- onClick={(e) => {
- e.stopPropagation();
- setTagValue("");
- setOpenCommandList(true);
- }}
- className="absolute inset-y-0 right-0 my-auto"
- >
- <X className="h-7 w-7 pr-3 text-gray-400" />
- </button>
- ) : (
- <ChevronDown className="absolute inset-y-0 right-0 my-auto h-7 w-7 pr-3 text-gray-400" />
- )}
- </div>
- {openCommandList && (
- <Command.List
- data-exclude-click
- style={{
- animationFillMode: "forwards", // to keep the last frame of the animation
- }}
- className="absolute z-10 h-[300px] w-full animate-input-select-slide-up overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all sm:h-auto sm:max-h-[300px] sm:animate-input-select-slide-down"
- >
- <Command.Empty>
- <button
- type="button"
- onClick={() => setOpenCommandList(false)}
- className="flex w-full cursor-pointer items-center rounded-md bg-gray-100 px-4 py-2 text-sm text-gray-900 hover:text-gray-900 aria-selected:bg-gray-100 aria-selected:text-gray-900"
- >
- {tagValue.length > 0 ? (
- <>
- Create tag{" "}
- <span className="ml-1.5 rounded-md bg-blue-100 px-2 py-0.5 text-blue-600">
- {tagValue}
- </span>
- </>
- ) : (
- <p className="py-0.5">Start typing to create tag...</p>
- )}
- </button>
- </Command.Empty>
- {tags?.map((tag) => (
- <Command.Item
- key={tag.id}
- value={tag.name}
- onSelect={() => {
- setTagValue(tag.name);
- setOpenCommandList(false);
- }}
- className="group flex cursor-pointer items-center justify-between rounded-md px-4 py-2 text-sm text-gray-900 hover:bg-gray-100 hover:text-gray-900 active:bg-gray-200 aria-selected:bg-gray-100 aria-selected:text-gray-900"
- >
- <TagBadge {...tag} />
- <TagPopover key={tag.id} tag={tag} />
- </Command.Item>
- ))}
- </Command.List>
- )}
- </Command>
-
- <Button
- loading={tagging}
- disabled={saveDisabled}
- text="Save changes"
- />
- </form>
- </div>
- </Modal>
- );
-}
-
-const TagPopover = ({ tag }: { tag: TagProps }) => {
- const router = useRouter();
- const { slug } = router.query as { slug: string };
- const [data, setData] = useState(tag);
- const [openPopover, setOpenPopover] = useState(false);
- const [processing, setProcessing] = useState(false);
-
- const handleEdit = async (e) => {
- e.stopPropagation();
- e.preventDefault();
- setProcessing(true);
- fetch(`/api/projects/${slug}/tags/${tag.id}`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(data),
- }).then((res) => {
- setProcessing(false);
- if (res.ok) {
- toast.success("Tag updated");
- mutate(`/api/projects/${slug}/tags`);
- } else {
- toast.error("Something went wrong");
- }
- });
- };
-
- const handleDelete = async () => {
- setProcessing(true);
- fetch(`/api/projects/${slug}/tags/${tag.id}`, {
- method: "DELETE",
- }).then((res) => {
- setProcessing(false);
- if (res.ok) {
- toast.success("Tag deleted");
- mutate(`/api/projects/${slug}/tags`);
- } else {
- toast.error("Something went wrong");
- }
- });
- };
-
- return processing ? (
- <LoadingCircle />
- ) : (
- <Popover
- content={
- <div
- data-exclude-click
- className="flex w-48 flex-col divide-y divide-gray-200"
- >
- <div className="p-2">
- <form
- onClick={(e) => e.stopPropagation()} // prevent triggering <Command.Item> onClick
- onKeyDown={(e) => e.stopPropagation()} // prevent triggering <Command.Item> onKeyDown
- onSubmit={handleEdit}
- className="relative py-1"
- >
- <div className="my-2 flex items-center justify-between px-3">
- <p className="text-xs text-gray-500">Edit Tag</p>
- {data !== tag && (
- <button className="text-xs text-gray-500">Save</button>
- )}
- </div>
- <input
- type="text"
- autoFocus
- required
- value={data.name}
- onChange={(e) => setData({ ...data, name: e.target.value })}
- className="block w-full rounded-md border-gray-300 py-1 pr-7 text-sm text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
- />
- <div className="grid grid-cols-3 gap-3 p-3 pb-0">
- {COLORS_LIST.map(({ color, css }) => (
- <button
- key={color}
- type="button"
- className={`mx-auto flex h-6 w-6 items-center justify-center rounded-full transition-all duration-75 hover:scale-110 active:scale-90 ${css}`}
- onClick={(e) => {
- e.stopPropagation();
- setData({ ...data, color });
- }}
- >
- {data.color === color && <Check className="h-4 w-4" />}
- </button>
- ))}
- </div>
- </form>
- </div>
- <div className="p-2">
- <button
- onClick={(e) => {
- e.stopPropagation();
- confirm(
- "Are you sure you want to delete this tag? All tagged links will be untagged, but they won't be deleted.",
- ) && handleDelete();
- }}
- className="flex w-full items-center space-x-2 rounded-md p-2 text-red-600 transition-colors hover:bg-red-100 active:bg-red-200"
- >
- <IconMenu
- text="Delete Tag"
- icon={<Trash className="h-4 w-4 text-red-600" />}
- />
- </button>
- </div>
- </div>
- }
- align="end"
- desktopOnly
- openPopover={openPopover}
- setOpenPopover={setOpenPopover}
- >
- <button
- type="button"
- onClick={(e) => {
- e.stopPropagation();
- setOpenPopover(!openPopover);
- }}
- className={`${
- openPopover
- ? "bg-gray-200"
- : "hidden hover:bg-gray-200 group-hover:block"
- } rounded-md p-1 transition-colors`}
- >
- <ThreeDots className="h-4 w-4 text-gray-500" />
- </button>
- </Popover>
- );
-};
-
-export function useTagLinkModal({ props }: { props?: LinkProps }) {
- const [showTagLinkModal, setShowTagLinkModal] = useState(false);
-
- const TagLinkModalCallback = useCallback(() => {
- return (
- <TagLinkModal
- showTagLinkModal={showTagLinkModal}
- setShowTagLinkModal={setShowTagLinkModal}
- props={props}
- />
- );
- }, [showTagLinkModal, setShowTagLinkModal]);
-
- return useMemo(
- () => ({
- setShowTagLinkModal,
- TagLinkModal: TagLinkModalCallback,
- }),
- [setShowTagLinkModal, TagLinkModalCallback],
- );
-}