2023-12-15 21:25:39 -06:00
|
|
|
import { useState } from "react";
|
|
|
|
import {
|
|
|
|
CollectionIncludingMembersAndLinkCount,
|
|
|
|
LinkIncludingShortenedCollectionAndTags,
|
|
|
|
} from "@/types/global";
|
|
|
|
import usePermissions from "@/hooks/usePermissions";
|
|
|
|
import DeleteLinkModal from "@/components/ModalContent/DeleteLinkModal";
|
2024-01-14 09:09:09 -06:00
|
|
|
import { dropdownTriggerer } from "@/lib/client/utils";
|
2024-06-09 08:27:16 -05:00
|
|
|
import { useTranslation } from "next-i18next";
|
2024-09-11 00:38:38 -05:00
|
|
|
import { useDeleteLink, useGetLink } from "@/hooks/store/links";
|
2024-08-14 14:22:28 -05:00
|
|
|
import toast from "react-hot-toast";
|
2024-08-30 16:29:15 -05:00
|
|
|
import LinkModal from "@/components/ModalContent/LinkModal";
|
2024-08-18 15:39:43 -05:00
|
|
|
import { useRouter } from "next/router";
|
2024-09-09 23:09:33 -05:00
|
|
|
import clsx from "clsx";
|
2024-09-11 00:38:38 -05:00
|
|
|
import usePinLink from "@/lib/client/pinLink";
|
2023-12-15 21:25:39 -06:00
|
|
|
|
2023-12-17 02:22:08 -06:00
|
|
|
type Props = {
|
2023-12-15 21:25:39 -06:00
|
|
|
link: LinkIncludingShortenedCollectionAndTags;
|
|
|
|
collection: CollectionIncludingMembersAndLinkCount;
|
2024-09-09 23:09:33 -05:00
|
|
|
btnStyle?: string;
|
2023-12-19 16:20:09 -06:00
|
|
|
};
|
2023-12-17 02:22:08 -06:00
|
|
|
|
2024-09-12 12:47:18 -05:00
|
|
|
export default function LinkActions({ link, btnStyle }: Props) {
|
2024-06-09 08:27:16 -05:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-12-15 21:25:39 -06:00
|
|
|
const permissions = usePermissions(link.collection.id as number);
|
2024-08-28 19:48:35 -05:00
|
|
|
const getLink = useGetLink();
|
2023-12-15 21:25:39 -06:00
|
|
|
|
2024-09-11 00:38:38 -05:00
|
|
|
const pinLink = usePinLink();
|
|
|
|
|
2023-12-15 21:25:39 -06:00
|
|
|
const [editLinkModal, setEditLinkModal] = useState(false);
|
2024-08-30 16:29:15 -05:00
|
|
|
const [linkModal, setLinkModal] = useState(false);
|
2023-12-15 21:25:39 -06:00
|
|
|
const [deleteLinkModal, setDeleteLinkModal] = useState(false);
|
|
|
|
|
2024-08-12 23:08:57 -05:00
|
|
|
const deleteLink = useDeleteLink();
|
2023-12-15 21:25:39 -06:00
|
|
|
|
2024-08-28 19:48:35 -05:00
|
|
|
const updateArchive = async () => {
|
|
|
|
const load = toast.loading(t("sending_request"));
|
|
|
|
|
|
|
|
const response = await fetch(`/api/v1/links/${link?.id}/archive`, {
|
|
|
|
method: "PUT",
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
await getLink.mutateAsync({ id: link.id as number });
|
|
|
|
|
|
|
|
toast.success(t("link_being_archived"));
|
|
|
|
} else toast.error(data.response);
|
|
|
|
};
|
|
|
|
|
2024-08-18 15:39:43 -05:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const isPublicRoute = router.pathname.startsWith("/public") ? true : false;
|
|
|
|
|
2023-12-15 21:25:39 -06:00
|
|
|
return (
|
2023-12-21 09:55:07 -06:00
|
|
|
<>
|
2024-08-18 15:39:43 -05:00
|
|
|
{isPublicRoute ? (
|
2023-12-17 00:25:46 -06:00
|
|
|
<div
|
2024-09-12 12:47:18 -05:00
|
|
|
className="absolute top-3 right-3 group-hover:opacity-100 group-focus-within:opacity-100 opacity-0 duration-100"
|
2024-09-04 21:19:40 -05:00
|
|
|
onMouseDown={dropdownTriggerer}
|
2024-08-30 16:29:15 -05:00
|
|
|
onClick={() => setLinkModal(true)}
|
2023-12-17 00:25:46 -06:00
|
|
|
>
|
2024-09-11 01:29:50 -05:00
|
|
|
<div className={clsx("btn btn-sm btn-square text-neutral", btnStyle)}>
|
2024-11-07 00:12:05 -06:00
|
|
|
<i title="More" className="bi-info-circle text-xl" />
|
2024-08-18 15:39:43 -05:00
|
|
|
</div>
|
2023-12-15 21:25:39 -06:00
|
|
|
</div>
|
2024-08-18 15:39:43 -05:00
|
|
|
) : (
|
|
|
|
<div
|
2024-09-12 12:47:18 -05:00
|
|
|
className={`dropdown dropdown-end absolute top-3 right-3 group-hover:opacity-100 group-focus-within:opacity-100 opacity-0 duration-100 z-20`}
|
2024-04-23 19:56:00 -05:00
|
|
|
>
|
2024-08-18 15:39:43 -05:00
|
|
|
<div
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
onMouseDown={dropdownTriggerer}
|
2024-09-09 23:09:33 -05:00
|
|
|
className={clsx("btn btn-sm btn-square text-neutral", btnStyle)}
|
2024-08-18 15:39:43 -05:00
|
|
|
>
|
|
|
|
<i title="More" className="bi-three-dots text-xl" />
|
|
|
|
</div>
|
|
|
|
<ul
|
2024-09-09 22:05:57 -05:00
|
|
|
className={
|
2024-09-11 00:38:38 -05:00
|
|
|
"dropdown-content z-[20] menu shadow bg-base-200 border border-neutral-content rounded-box mt-1"
|
2024-09-09 22:05:57 -05:00
|
|
|
}
|
2024-08-18 15:39:43 -05:00
|
|
|
>
|
2024-09-04 21:19:40 -05:00
|
|
|
<li>
|
|
|
|
<div
|
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
|
|
|
onClick={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-09-11 00:38:38 -05:00
|
|
|
pinLink(link);
|
2024-09-04 21:19:40 -05:00
|
|
|
}}
|
|
|
|
className="whitespace-nowrap"
|
|
|
|
>
|
|
|
|
{link?.pinnedBy && link.pinnedBy[0]
|
|
|
|
? t("unpin")
|
|
|
|
: t("pin_to_dashboard")}
|
|
|
|
</div>
|
|
|
|
</li>
|
2023-12-27 14:00:48 -06:00
|
|
|
<li>
|
|
|
|
<div
|
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
|
|
|
onClick={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-08-30 16:29:15 -05:00
|
|
|
setLinkModal(true);
|
2023-12-27 14:00:48 -06:00
|
|
|
}}
|
2024-08-14 18:13:19 -05:00
|
|
|
className="whitespace-nowrap"
|
2023-12-27 14:00:48 -06:00
|
|
|
>
|
2024-08-18 15:39:43 -05:00
|
|
|
{t("show_link_details")}
|
2023-12-27 14:00:48 -06:00
|
|
|
</div>
|
|
|
|
</li>
|
2024-08-18 15:39:43 -05:00
|
|
|
{(permissions === true || permissions?.canUpdate) && (
|
|
|
|
<li>
|
|
|
|
<div
|
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
|
|
|
onClick={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
|
|
|
setEditLinkModal(true);
|
|
|
|
}}
|
|
|
|
className="whitespace-nowrap"
|
|
|
|
>
|
|
|
|
{t("edit_link")}
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
)}
|
|
|
|
{(permissions === true || permissions?.canDelete) && (
|
|
|
|
<li>
|
|
|
|
<div
|
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
|
|
|
onClick={async (e) => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-08-19 17:14:09 -05:00
|
|
|
console.log(e.shiftKey);
|
2024-08-18 15:39:43 -05:00
|
|
|
e.shiftKey
|
2024-08-19 17:14:09 -05:00
|
|
|
? (async () => {
|
2024-08-18 15:39:43 -05:00
|
|
|
const load = toast.loading(t("deleting"));
|
2024-08-14 14:22:28 -05:00
|
|
|
|
2024-08-18 15:39:43 -05:00
|
|
|
await deleteLink.mutateAsync(link.id as number, {
|
|
|
|
onSettled: (data, error) => {
|
|
|
|
toast.dismiss(load);
|
2023-12-15 21:25:39 -06:00
|
|
|
|
2024-08-18 15:39:43 -05:00
|
|
|
if (error) {
|
|
|
|
toast.error(error.message);
|
|
|
|
} else {
|
|
|
|
toast.success(t("deleted"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2024-08-19 17:14:09 -05:00
|
|
|
})()
|
2024-08-18 15:39:43 -05:00
|
|
|
: setDeleteLinkModal(true);
|
|
|
|
}}
|
|
|
|
className="whitespace-nowrap"
|
|
|
|
>
|
|
|
|
{t("delete")}
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-07-22 22:34:36 -05:00
|
|
|
{editLinkModal && (
|
2024-08-30 16:29:15 -05:00
|
|
|
<LinkModal
|
2023-12-15 21:25:39 -06:00
|
|
|
onClose={() => setEditLinkModal(false)}
|
2024-09-11 00:38:38 -05:00
|
|
|
onPin={() => pinLink(link)}
|
2024-08-30 16:29:15 -05:00
|
|
|
onUpdateArchive={updateArchive}
|
|
|
|
onDelete={() => setDeleteLinkModal(true)}
|
|
|
|
link={link}
|
|
|
|
activeMode="edit"
|
2023-12-15 21:25:39 -06:00
|
|
|
/>
|
2024-07-22 22:34:36 -05:00
|
|
|
)}
|
|
|
|
{deleteLinkModal && (
|
2023-12-15 21:25:39 -06:00
|
|
|
<DeleteLinkModal
|
|
|
|
onClose={() => setDeleteLinkModal(false)}
|
|
|
|
activeLink={link}
|
|
|
|
/>
|
2024-07-22 22:34:36 -05:00
|
|
|
)}
|
2024-08-30 16:29:15 -05:00
|
|
|
{linkModal && (
|
|
|
|
<LinkModal
|
|
|
|
onClose={() => setLinkModal(false)}
|
2024-09-11 00:38:38 -05:00
|
|
|
onPin={() => pinLink(link)}
|
2024-08-28 19:48:35 -05:00
|
|
|
onUpdateArchive={updateArchive}
|
2024-08-28 19:22:11 -05:00
|
|
|
onDelete={() => setDeleteLinkModal(true)}
|
2024-08-18 15:39:43 -05:00
|
|
|
link={link}
|
|
|
|
/>
|
|
|
|
)}
|
2023-12-21 09:55:07 -06:00
|
|
|
</>
|
2023-12-15 21:25:39 -06:00
|
|
|
);
|
|
|
|
}
|