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

195 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-07-19 16:49:54 -05:00
import NoLinksFound from "@/components/NoLinksFound";
2023-06-13 23:40:23 -05:00
import SortDropdown from "@/components/SortDropdown";
import useLinks from "@/hooks/useLinks";
import MainLayout from "@/layouts/MainLayout";
2023-03-28 02:31:50 -05:00
import useLinkStore from "@/store/links";
2024-02-11 02:02:14 -06:00
import React, { useEffect, useState } from "react";
2023-12-16 09:33:33 -06:00
import PageHeader from "@/components/PageHeader";
2024-02-11 02:02:14 -06:00
import { Member, Sort, ViewMode } from "@/types/global";
2023-12-15 21:25:39 -06:00
import ViewDropdown from "@/components/ViewDropdown";
import CardView from "@/components/LinkViews/Layouts/CardView";
import ListView from "@/components/LinkViews/Layouts/ListView";
import useCollectivePermissions from "@/hooks/useCollectivePermissions";
import toast from "react-hot-toast";
import BulkDeleteLinksModal from "@/components/ModalContent/BulkDeleteLinksModal";
import BulkEditLinksModal from "@/components/ModalContent/BulkEditLinksModal";
2023-12-21 04:11:26 -06:00
// import GridView from "@/components/LinkViews/Layouts/GridView";
2024-02-13 09:55:51 -06:00
import { useRouter } from "next/router";
2023-03-28 02:31:50 -05:00
export default function Links() {
2024-02-11 01:29:11 -06:00
const { links, selectedLinks, deleteLinksById, setSelectedLinks } =
useLinkStore();
2023-03-28 02:31:50 -05:00
2023-12-16 14:06:26 -06:00
const [viewMode, setViewMode] = useState<string>(
2023-12-21 09:55:07 -06:00
localStorage.getItem("viewMode") || ViewMode.Card
2023-12-16 14:06:26 -06:00
);
const [sortBy, setSortBy] = useState<Sort>(Sort.DateNewestFirst);
2024-02-13 09:55:51 -06:00
const router = useRouter();
const [bulkDeleteLinksModal, setBulkDeleteLinksModal] = useState(false);
const [bulkEditLinksModal, setBulkEditLinksModal] = useState(false);
const [editMode, setEditMode] = useState(false);
2024-03-06 08:06:38 -06:00
2024-02-13 09:55:51 -06:00
useEffect(() => {
2024-03-06 08:06:38 -06:00
if (editMode) return setEditMode(false);
2024-02-13 09:55:51 -06:00
}, [router]);
2024-02-11 01:29:11 -06:00
const collectivePermissions = useCollectivePermissions(
2024-02-13 04:54:18 -06:00
selectedLinks.map((link) => link.collectionId as number)
2024-02-11 01:29:11 -06:00
);
useLinks({ sort: sortBy });
const handleSelectAll = () => {
if (selectedLinks.length === links.length) {
setSelectedLinks([]);
} else {
setSelectedLinks(links.map((link) => link));
}
};
const bulkDeleteLinks = async () => {
const load = toast.loading(
2024-02-22 03:15:14 -06:00
`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(
2024-02-22 03:15:14 -06:00
`Deleted ${selectedLinks.length} Link${
selectedLinks.length > 1 ? "s" : ""
}!`
);
};
const linkView = {
2023-12-21 09:55:07 -06:00
[ViewMode.Card]: CardView,
2023-12-16 14:06:26 -06:00
// [ViewMode.Grid]: GridView,
2023-12-15 21:25:39 -06:00
[ViewMode.List]: ListView,
};
2023-12-15 22:57:50 -06:00
// @ts-ignore
const LinkComponent = linkView[viewMode];
2023-12-15 21:25:39 -06:00
2023-03-28 02:31:50 -05:00
return (
<MainLayout>
2023-10-23 09:45:48 -05:00
<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-link-45deg"}
title={"All Links"}
description={"Links from every Collections"}
/>
<div className="mt-2 flex items-center justify-end gap-2">
2024-02-13 04:54:18 -06:00
{links.length > 0 && (
2024-02-11 01:08:28 -06:00
<div
role="button"
onClick={() => {
2024-02-11 01:29:11 -06:00
setEditMode(!editMode);
setSelectedLinks([]);
2024-02-11 01:08:28 -06:00
}}
2024-02-22 03:15:14 -06:00
className={`btn btn-square btn-sm btn-ghost ${
editMode
? "bg-primary/20 hover:bg-primary/20"
: "hover:bg-neutral/20"
}`}
2024-02-11 01:08:28 -06:00
>
<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>
2023-04-23 08:26:39 -05:00
</div>
2023-12-16 09:33:33 -06:00
2024-02-22 03:15:14 -06:00
{editMode && links.length > 0 && (
2024-02-11 02:48:34 -06:00
<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">
2024-02-13 09:55:51 -06:00
<button
onClick={() => setBulkEditLinksModal(true)}
className="btn btn-sm btn-accent text-white w-fit ml-auto"
disabled={
selectedLinks.length === 0 ||
!(
collectivePermissions === true ||
collectivePermissions?.canUpdate
)
}
>
Edit
</button>
<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"
disabled={
selectedLinks.length === 0 ||
!(
collectivePermissions === true ||
collectivePermissions?.canDelete
)
}
>
Delete
</button>
</div>
</div>
2024-02-11 02:48:34 -06:00
)}
2023-07-19 16:49:54 -05:00
{links[0] ? (
<LinkComponent editMode={editMode} links={links} />
2023-07-19 16:49:54 -05:00
) : (
2023-12-17 22:32:33 -06:00
<NoLinksFound text="You Haven't Created Any Links Yet" />
2023-07-19 16:49:54 -05:00
)}
2023-04-23 08:26:39 -05:00
</div>
{bulkDeleteLinksModal && (
<BulkDeleteLinksModal
onClose={() => {
setBulkDeleteLinksModal(false);
}}
/>
)}
{bulkEditLinksModal && (
2024-02-11 01:29:11 -06:00
<BulkEditLinksModal
onClose={() => {
setBulkEditLinksModal(false);
}}
/>
)}
</MainLayout>
2023-03-28 02:31:50 -05:00
);
}