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

176 lines
5.0 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";
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;
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,
}: Props) {
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];
const load = toast.loading("Applying...");
const response = await updateLink({
...link,
pinnedBy: isAlreadyPinned ? undefined : [{ id: account.id }],
});
toast.dismiss(load);
response.ok &&
2023-12-19 16:20:09 -06:00
toast.success(`Link ${isAlreadyPinned ? "Unpinned!" : "Pinned!"}`);
2023-12-15 21:25:39 -06:00
};
const deleteLink = async () => {
const load = toast.loading("Deleting...");
const response = await removeLink(link.id as number);
toast.dismiss(load);
response.ok && toast.success(`Link Deleted.`);
};
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"
} z-20`}
>
<div
2023-12-19 16:20:09 -06:00
tabIndex={0}
role="button"
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>
2023-12-19 16:20:09 -06:00
<ul className="dropdown-content z-[20] menu shadow bg-base-200 border border-neutral-content rounded-box w-44 mr-1">
{permissions === true ? (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
pinLink();
}}
>
{link?.pinnedBy && link.pinnedBy[0]
? "Unpin"
: "Pin to Dashboard"}
</div>
</li>
) : undefined}
2023-12-27 14:00:48 -06:00
{linkInfo !== undefined && toggleShowInfo ? (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
toggleShowInfo();
}}
>
{!linkInfo ? "Show" : "Hide"} Link Details
</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);
}}
>
2023-12-25 19:43:31 -06:00
Edit Link
2023-12-19 16:20:09 -06:00
</div>
</li>
) : undefined}
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
setPreservedFormatsModal(true);
}}
>
Preserved Formats
</div>
</li>
{permissions === true || permissions?.canDelete ? (
<li>
<div
role="button"
tabIndex={0}
onClick={(e) => {
(document?.activeElement as HTMLElement)?.blur();
e.shiftKey ? deleteLink() : setDeleteLinkModal(true);
}}
>
Delete
</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)}
activeLink={link}
/>
) : 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
);
}