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

206 lines
6.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";
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;
2023-12-27 14:00:48 -06:00
toggleShowInfo?: () => void;
linkInfo?: boolean;
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,
toggleShowInfo,
position,
linkInfo,
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);
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
};
return (
2023-12-21 09:55:07 -06:00
<>
2023-12-19 16:20:09 -06:00
<div
2024-04-23 19:56:00 -05:00
className={`dropdown dropdown-left absolute ${
2024-02-07 08:48:40 -06:00
position || "top-3 right-3"
2024-04-23 19:56:00 -05:00
} ${alignToTop ? "" : "dropdown-end"} z-20`}
2023-12-19 16:20:09 -06:00
>
<div
2023-12-19 16:20:09 -06:00
tabIndex={0}
role="button"
2024-01-14 09:09:09 -06:00
onMouseDown={dropdownTriggerer}
2023-12-19 16:20:09 -06:00
className="btn btn-ghost btn-sm btn-square text-neutral"
>
2023-12-21 09:55:07 -06:00
<i title="More" className="bi-three-dots text-xl" />
2023-12-15 21:25:39 -06:00
</div>
2024-04-23 19:56:00 -05:00
<ul
className={`dropdown-content z-[20] menu shadow bg-base-200 border border-neutral-content rounded-box w-44 mr-1 ${
alignToTop ? "" : "translate-y-10"
}`}
>
2024-02-07 08:48:40 -06:00
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
pinLink();
}}
>
{link?.pinnedBy && link.pinnedBy[0]
2024-06-09 08:27:16 -05:00
? t("unpin")
: t("pin_to_dashboard")}
2024-02-07 08:48:40 -06:00
</div>
</li>
2023-12-27 14:00:48 -06:00
{linkInfo !== undefined && toggleShowInfo ? (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
toggleShowInfo();
}}
>
2024-06-09 08:27:16 -05:00
{!linkInfo ? t("show_link_details") : t("hide_link_details")}
2023-12-27 14:00:48 -06:00
</div>
</li>
) : undefined}
2023-12-19 16:20:09 -06:00
{permissions === true || permissions?.canUpdate ? (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setEditLinkModal(true);
}}
>
2024-06-09 08:27:16 -05:00
{t("edit_link")}
2023-12-19 16:20:09 -06:00
</div>
</li>
) : undefined}
{link.type === "url" && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setPreservedFormatsModal(true);
}}
>
2024-06-09 08:27:16 -05:00
{t("preserved_formats")}
</div>
</li>
)}
2023-12-19 16:20:09 -06:00
{permissions === true || permissions?.canDelete ? (
<li>
<div
role="button"
tabIndex={0}
onClick={async (e) => {
2023-12-19 16:20:09 -06:00
(document?.activeElement as HTMLElement)?.blur();
e.shiftKey
2024-08-14 14:22:28 -05:00
? async () => {
const load = toast.loading(t("deleting"));
await deleteLink.mutateAsync(link.id as number, {
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(t("deleted"));
}
},
});
}
: setDeleteLinkModal(true);
2023-12-19 16:20:09 -06:00
}}
>
2024-06-09 08:27:16 -05:00
{t("delete")}
2023-12-19 16:20:09 -06:00
</div>
</li>
) : undefined}
</ul>
</div>
2023-12-15 21:25:39 -06:00
{editLinkModal ? (
<EditLinkModal
onClose={() => setEditLinkModal(false)}
activeLink={link}
/>
) : undefined}
{deleteLinkModal ? (
<DeleteLinkModal
onClose={() => setDeleteLinkModal(false)}
activeLink={link}
/>
) : undefined}
{preservedFormatsModal ? (
<PreservedFormatsModal
onClose={() => setPreservedFormatsModal(false)}
2024-08-13 02:01:02 -05:00
link={link}
2023-12-15 21:25:39 -06:00
/>
) : undefined}
{/* {expandedLink ? (
<ExpandedLink onClose={() => setExpandedLink(false)} link={link} />
) : undefined} */}
2023-12-21 09:55:07 -06:00
</>
2023-12-15 21:25:39 -06:00
);
}