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

194 lines
5.5 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";
import useLinkStore from "@/store/links";
import { toast } from "react-hot-toast";
import useAccountStore from "@/store/account";
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";
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 { account } = useAccountStore();
const { removeLink, updateLink } = useLinkStore();
const pinLink = async () => {
const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0];
2024-06-09 08:27:16 -05:00
const load = toast.loading(t("applying"));
2023-12-15 21:25:39 -06:00
const response = await updateLink({
...link,
pinnedBy: isAlreadyPinned ? undefined : [{ id: account.id }],
});
toast.dismiss(load);
if (response.ok) {
2024-06-09 08:27:16 -05:00
toast.success(isAlreadyPinned ? t("link_unpinned") : t("link_unpinned"));
} else {
toast.error(response.data as string);
}
2023-12-15 21:25:39 -06:00
};
const deleteLink = async () => {
2024-06-09 08:27:16 -05:00
const load = toast.loading(t("deleting"));
2023-12-15 21:25:39 -06:00
const response = await removeLink(link.id as number);
toast.dismiss(load);
if (response.ok) {
toast.success(t("deleted"));
} else {
toast.error(response.data as string);
}
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
className={`dropdown dropdown-left absolute ${position || "top-3 right-3"
} ${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-04-23 19:56:00 -05:00
>
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>
{linkInfo !== undefined && toggleShowInfo && (
2023-12-27 14:00:48 -06:00
<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>
)}
{permissions === true || permissions?.canUpdate && (
2023-12-19 16:20:09 -06:00
<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>
)}
{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>
)}
{permissions === true || permissions?.canDelete && (
2023-12-19 16:20:09 -06:00
<li>
<div
role="button"
tabIndex={0}
onClick={(e) => {
(document?.activeElement as HTMLElement)?.blur();
e.shiftKey ? deleteLink() : setDeleteLinkModal(true);
}}
>
2024-06-09 08:27:16 -05:00
{t("delete")}
2023-12-19 16:20:09 -06:00
</div>
</li>
)}
2023-12-19 16:20:09 -06:00
</ul>
</div>
2023-12-15 21:25:39 -06:00
{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)}
activeLink={link}
/>
)}
2023-12-15 21:25:39 -06:00
{/* {expandedLink ? (
<ExpandedLink onClose={() => setExpandedLink(false)} link={link} />
) : undefined} */}
2023-12-21 09:55:07 -06:00
</>
2023-12-15 21:25:39 -06:00
);
}