el.xwx.moe/components/LinkViews/LinkComponents/LinkActions.tsx

231 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-12-15 21:25:39 -06:00
import { useState } from "react";
import {
CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
import usePermissions from "@/hooks/usePermissions";
import EditLinkModal from "@/components/ModalContent/EditLinkModal";
import DeleteLinkModal from "@/components/ModalContent/DeleteLinkModal";
import PreservedFormatsModal from "@/components/ModalContent/PreservedFormatsModal";
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-07-31 13:15:50 -05:00
import { useUser } from "@/hooks/store/user";
import { useDeleteLink, useUpdateLink } from "@/hooks/store/links";
2024-08-14 14:22:28 -05:00
import toast from "react-hot-toast";
2024-08-18 15:39:43 -05:00
import LinkDetailModal from "@/components/ModalContent/LinkDetailModal";
import { useRouter } from "next/router";
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;
position?: string;
2024-04-23 19:56:00 -05:00
alignToTop?: boolean;
2024-01-19 23:34:49 -06:00
flipDropdown?: boolean;
2023-12-19 16:20:09 -06:00
};
2023-12-17 02:22:08 -06:00
2023-12-23 18:00:53 -06:00
export default function LinkActions({
link,
position,
2024-04-23 19:56:00 -05:00
alignToTop,
2024-01-19 23:34:49 -06:00
flipDropdown,
2023-12-23 18:00:53 -06:00
}: 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);
const [editLinkModal, setEditLinkModal] = useState(false);
2024-08-18 15:39:43 -05:00
const [linkDetailModal, setLinkDetailModal] = useState(false);
2023-12-15 21:25:39 -06:00
const [deleteLinkModal, setDeleteLinkModal] = useState(false);
const [preservedFormatsModal, setPreservedFormatsModal] = useState(false);
const { data: user = {} } = useUser();
2023-12-15 21:25:39 -06:00
const updateLink = useUpdateLink();
const deleteLink = useDeleteLink();
2023-12-15 21:25:39 -06:00
const pinLink = async () => {
2024-08-14 14:22:28 -05:00
const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0] ? true : false;
2023-12-15 21:25:39 -06:00
2024-08-14 14:22:28 -05:00
const load = toast.loading(t("updating"));
await updateLink.mutateAsync(
{
...link,
pinnedBy: isAlreadyPinned ? undefined : [{ id: user.id }],
},
{
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(
isAlreadyPinned ? t("link_unpinned") : t("link_pinned")
);
}
},
}
);
2023-12-15 21:25:39 -06:00
};
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 ? (
<div
2024-08-18 15:39:43 -05:00
className={`absolute ${position || "top-3 right-3"} ${
alignToTop ? "" : "dropdown-end"
} z-20`}
onClick={() => setLinkDetailModal(true)}
>
2024-08-18 15:39:43 -05:00
<div className="btn btn-ghost btn-sm btn-square text-neutral">
<i title="More" className="bi-three-dots text-xl" />
</div>
2023-12-15 21:25:39 -06:00
</div>
2024-08-18 15:39:43 -05:00
) : (
<div
className={`dropdown dropdown-left absolute ${
position || "top-3 right-3"
} ${alignToTop ? "" : "dropdown-end"} 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}
className="btn btn-ghost btn-sm btn-square text-neutral"
>
<i title="More" className="bi-three-dots text-xl" />
</div>
<ul
className={`dropdown-content z-[20] menu shadow bg-base-200 border border-neutral-content rounded-box mr-1 ${
alignToTop ? "" : "translate-y-10"
}`}
>
{permissions === true ||
(permissions?.canUpdate && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
pinLink();
}}
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-18 15:39:43 -05:00
setLinkDetailModal(true);
2023-12-27 14:00:48 -06: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>
)}
{link.type === "url" && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setPreservedFormatsModal(true);
}}
className="whitespace-nowrap"
>
{t("preserved_formats")}
</div>
</li>
)}
{(permissions === true || permissions?.canDelete) && (
<li>
<div
role="button"
tabIndex={0}
onClick={async (e) => {
(document?.activeElement as HTMLElement)?.blur();
e.shiftKey
? async () => {
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"));
}
},
});
}
: setDeleteLinkModal(true);
}}
className="whitespace-nowrap"
>
{t("delete")}
</div>
</li>
)}
</ul>
</div>
)}
{editLinkModal && (
2023-12-15 21:25:39 -06:00
<EditLinkModal
onClose={() => setEditLinkModal(false)}
activeLink={link}
/>
)}
{deleteLinkModal && (
2023-12-15 21:25:39 -06:00
<DeleteLinkModal
onClose={() => setDeleteLinkModal(false)}
activeLink={link}
/>
)}
{preservedFormatsModal && (
2023-12-15 21:25:39 -06:00
<PreservedFormatsModal
onClose={() => setPreservedFormatsModal(false)}
2024-08-13 02:01:02 -05:00
link={link}
2023-12-15 21:25:39 -06:00
/>
)}
2024-08-18 15:39:43 -05:00
{linkDetailModal && (
<LinkDetailModal
onClose={() => setLinkDetailModal(false)}
onEdit={() => setEditLinkModal(true)}
link={link}
/>
)}
2023-12-21 09:55:07 -06:00
</>
2023-12-15 21:25:39 -06:00
);
}