el.xwx.moe/pages/links/pinned.tsx

185 lines
6.5 KiB
TypeScript
Raw Normal View History

import SortDropdown from "@/components/SortDropdown";
import useLinks from "@/hooks/useLinks";
import MainLayout from "@/layouts/MainLayout";
import useLinkStore from "@/store/links";
2023-12-16 09:33:33 -06:00
import React, { useState } from "react";
import PageHeader from "@/components/PageHeader";
import { Sort, ViewMode } from "@/types/global";
import ViewDropdown from "@/components/ViewDropdown";
import CardView from "@/components/LinkViews/Layouts/CardView";
import ListView from "@/components/LinkViews/Layouts/ListView";
import BulkDeleteLinksModal from "@/components/ModalContent/BulkDeleteLinksModal";
import BulkEditLinksModal from "@/components/ModalContent/BulkEditLinksModal";
import useCollectivePermissions from "@/hooks/useCollectivePermissions";
import toast from "react-hot-toast";
2023-12-21 09:55:07 -06:00
// import GridView from "@/components/LinkViews/Layouts/GridView";
export default function PinnedLinks() {
const { links, selectedLinks, deleteLinksById, setSelectedLinks } = useLinkStore();
const [viewMode, setViewMode] = useState<string>(
2023-12-21 09:55:07 -06:00
localStorage.getItem("viewMode") || ViewMode.Card
);
const [sortBy, setSortBy] = useState<Sort>(Sort.DateNewestFirst);
useLinks({ sort: sortBy, pinnedOnly: true });
const [bulkDeleteLinksModal, setBulkDeleteLinksModal] = useState(false);
const [bulkEditLinksModal, setBulkEditLinksModal] = useState(false);
const [editMode, setEditMode] = useState(false);
const collectivePermissions = useCollectivePermissions(selectedLinks.map((link) => link.collectionId as number));
useLinks({ sort: sortBy });
const handleSelectAll = () => {
if (selectedLinks.length === links.length) {
setSelectedLinks([]);
} else {
setSelectedLinks(links.map((link) => link));
}
};
const bulkDeleteLinks = async () => {
const load = toast.loading(
`Deleting ${selectedLinks.length} Link${selectedLinks.length > 1 ? "s" : ""
}...`
);
const response = await deleteLinksById(
selectedLinks.map((link) => link.id as number)
);
toast.dismiss(load);
response.ok &&
toast.success(
`Deleted ${selectedLinks.length} Link${selectedLinks.length > 1 ? "s" : ""
}!`
);
};
const linkView = {
2023-12-21 09:55:07 -06:00
[ViewMode.Card]: CardView,
// [ViewMode.Grid]: GridView,
[ViewMode.List]: ListView,
};
// @ts-ignore
const LinkComponent = linkView[viewMode];
return (
<MainLayout>
<div className="p-5 flex flex-col gap-5 w-full h-full">
2023-12-17 22:32:33 -06:00
<div className="flex justify-between">
<PageHeader
icon={"bi-pin-angle"}
title={"Pinned Links"}
description={"Pinned Links from your Collections"}
/>
<div className="mt-2 flex items-center justify-end gap-2">
2024-02-11 01:08:28 -06:00
{links.length > 0 && (
<div
role="button"
onClick={() => {
setEditMode(!editMode)
setSelectedLinks([])
}}
className={`btn btn-square btn-sm btn-ghost ${editMode
? "bg-primary/20 hover:bg-primary/20"
: "hover:bg-neutral/20"
}`}
>
<i className="bi-pencil-fill text-neutral text-xl"></i>
</div>
)}
2023-12-17 22:32:33 -06:00
<SortDropdown sortBy={sortBy} setSort={setSortBy} />
<ViewDropdown viewMode={viewMode} setViewMode={setViewMode} />
</div>
</div>
2023-12-17 02:47:32 -06:00
{editMode && (
<div className="w-full flex justify-between items-center min-h-[32px]">
{links.length > 0 && (
<div className="flex gap-3 ml-3">
<input
type="checkbox"
className="checkbox checkbox-primary"
onChange={() => handleSelectAll()}
checked={
selectedLinks.length === links.length && links.length > 0
}
/>
{selectedLinks.length > 0 ? (
<span>
{selectedLinks.length}{" "}
{selectedLinks.length === 1 ? "link" : "links"} selected
</span>
) : (
<span>Nothing selected</span>
)}
</div>
)}
<div className="flex gap-3">
{selectedLinks.length > 0 &&
(collectivePermissions === true || collectivePermissions?.canUpdate) && (
<button
onClick={() => setBulkEditLinksModal(true)}
className="btn btn-sm btn-accent dark:border-violet-400 text-white w-fit ml-auto"
>
Edit
</button>
)}
{selectedLinks.length > 0 &&
(collectivePermissions === true || collectivePermissions?.canDelete) && (
<button
onClick={(e) => {
(document?.activeElement as HTMLElement)?.blur();
e.shiftKey
? bulkDeleteLinks()
: setBulkDeleteLinksModal(true);
}}
className="btn btn-sm bg-red-400 border-red-400 hover:border-red-500 hover:bg-red-500 text-white w-fit ml-auto"
>
Delete
</button>
)}
</div>
</div>
)}
2023-11-11 15:06:50 -06:00
{links.some((e) => e.pinnedBy && e.pinnedBy[0]) ? (
<LinkComponent editMode={editMode} links={links} />
) : (
2023-11-11 15:06:50 -06:00
<div
style={{ flex: "1 1 auto" }}
2023-11-25 04:54:43 -06:00
className="sky-shadow flex flex-col justify-center h-full border border-solid border-neutral-content w-full mx-auto p-10 rounded-2xl bg-base-200"
2023-11-11 15:06:50 -06:00
>
2023-11-24 07:39:55 -06:00
<p className="text-center text-2xl">
2023-11-11 15:06:50 -06:00
Pin Your Favorite Links Here!
</p>
2023-11-25 04:39:56 -06:00
<p className="text-center mx-auto max-w-96 w-fit text-neutral text-sm mt-2">
2023-11-11 15:06:50 -06:00
You can Pin your favorite Links by clicking on the three dots on
each Link and clicking{" "}
<span className="font-semibold">Pin to Dashboard</span>.
</p>
</div>
)}
</div>
{bulkDeleteLinksModal && (
<BulkDeleteLinksModal
onClose={() => {
setBulkDeleteLinksModal(false);
setEditMode(false);
}}
/>
)}
{bulkEditLinksModal && (
<BulkEditLinksModal onClose={() => {
setBulkEditLinksModal(false);
setEditMode(false);
}} />
)}
</MainLayout>
);
}