el.xwx.moe/pages/public/collections/[id].tsx

318 lines
11 KiB
TypeScript
Raw Normal View History

2023-10-05 11:19:31 -05:00
"use client";
2023-05-29 14:40:23 -05:00
import getPublicCollectionData from "@/lib/client/getPublicCollectionData";
2023-12-24 06:30:45 -06:00
import {
2024-07-27 17:40:07 -05:00
AccountSettings,
2023-12-24 06:30:45 -06:00
CollectionIncludingMembersAndLinkCount,
Sort,
ViewMode,
} from "@/types/global";
2023-05-29 14:40:23 -05:00
import { useRouter } from "next/router";
import React, { useEffect, useState } from "react";
2023-10-11 11:18:45 -05:00
import Head from "next/head";
2023-11-15 22:31:13 -06:00
import ProfilePhoto from "@/components/ProfilePhoto";
2023-11-16 02:22:16 -06:00
import ToggleDarkMode from "@/components/ToggleDarkMode";
import getPublicUserData from "@/lib/client/getPublicUserData";
import Image from "next/image";
import Link from "next/link";
2023-11-24 06:50:16 -06:00
import useLocalSettingsStore from "@/store/localSettings";
2023-11-30 05:13:42 -06:00
import SearchBar from "@/components/SearchBar";
2023-12-01 16:44:34 -06:00
import EditCollectionSharingModal from "@/components/ModalContent/EditCollectionSharingModal";
import { useTranslation } from "next-i18next";
import getServerSideProps from "@/lib/client/getServerSideProps";
import LinkListOptions from "@/components/LinkListOptions";
import { usePublicLinks } from "@/hooks/store/publicLinks";
import Links from "@/components/LinkViews/Links";
2024-11-02 19:43:53 -05:00
import { usePublicTags } from "@/hooks/store/publicTags";
2023-05-28 00:55:49 -05:00
export default function PublicCollections() {
const { t } = useTranslation();
2023-11-16 02:22:16 -06:00
const { settings } = useLocalSettingsStore();
2023-11-24 06:50:16 -06:00
2023-05-29 14:40:23 -05:00
const router = useRouter();
2024-07-27 17:41:13 -05:00
const [collectionOwner, setCollectionOwner] = useState<
Partial<AccountSettings>
>({});
2023-11-16 02:22:16 -06:00
const handleTagSelection = (tag: string | undefined) => {
2024-07-05 03:40:40 -05:00
if (tag) {
Object.keys(searchFilter).forEach(
(v) =>
(searchFilter[
v as keyof {
name: boolean;
url: boolean;
description: boolean;
tags: boolean;
textContent: boolean;
}
] = false)
);
2024-07-05 03:40:40 -05:00
searchFilter.tags = true;
return router.push(
"/public/collections/" +
router.query.id +
"?q=" +
encodeURIComponent(tag || "")
2024-07-05 03:40:40 -05:00
);
} else {
return router.push("/public/collections/" + router.query.id);
2024-07-05 03:40:40 -05:00
}
};
2024-07-05 03:40:40 -05:00
2023-11-15 12:12:06 -06:00
const [searchFilter, setSearchFilter] = useState({
name: true,
url: true,
description: true,
tags: true,
2024-03-10 05:08:28 -05:00
textContent: false,
2023-11-15 12:12:06 -06:00
});
const [sortBy, setSortBy] = useState<Sort>(
Number(localStorage.getItem("sortBy")) ?? Sort.DateNewestFirst
);
2023-11-15 12:12:06 -06:00
2024-11-02 19:43:53 -05:00
const { data: tags } = usePublicTags();
const { links, data } = usePublicLinks({
2023-11-15 12:12:06 -06:00
sort: sortBy,
searchQueryString: router.query.q
? decodeURIComponent(router.query.q as string)
: undefined,
searchByName: searchFilter.name,
searchByUrl: searchFilter.url,
searchByDescription: searchFilter.description,
searchByTextContent: searchFilter.textContent,
searchByTags: searchFilter.tags,
});
const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>();
2023-05-29 14:40:23 -05:00
useEffect(() => {
2023-06-02 22:50:16 -05:00
if (router.query.id) {
2024-08-16 11:35:04 -05:00
getPublicCollectionData(Number(router.query.id)).then((res) => {
if (res.status === 400) {
router.push("/dashboard");
} else {
setCollection(res.response);
}
2024-08-16 11:35:04 -05:00
});
2023-06-02 22:50:16 -05:00
}
2023-05-29 14:40:23 -05:00
}, []);
2023-11-16 02:22:16 -06:00
useEffect(() => {
2024-08-16 11:35:04 -05:00
if (collection) {
getPublicUserData(collection.ownerId as number).then((owner) =>
setCollectionOwner(owner)
);
}
2023-11-16 02:22:16 -06:00
}, [collection]);
2023-12-01 11:17:00 -06:00
const [editCollectionSharingModal, setEditCollectionSharingModal] =
useState(false);
const [viewMode, setViewMode] = useState<ViewMode>(
(localStorage.getItem("viewMode") as ViewMode) || ViewMode.Card
2023-12-24 06:30:45 -06:00
);
2024-08-18 15:39:43 -05:00
if (!collection) return <></>;
else
return (
<div
className="h-96"
style={{
backgroundImage: `linear-gradient(${collection?.color}40 10%,
${settings.theme === "dark" ? "#171717" : "#f5f5dc"} 100%)`,
2024-08-18 15:39:43 -05:00
}}
>
{collection && (
<Head>
<title>{collection.name} | Linkwarden</title>
<meta
property="og:title"
content={`${collection.name} | Linkwarden`}
key="title"
/>
</Head>
)}
<div className="lg:w-3/4 w-full mx-auto p-5 bg">
<div className="flex items-center justify-between">
<p className="text-4xl font-thin mb-2 capitalize mt-10">
{collection.name}
</p>
<div className="flex gap-2 items-center mt-8 min-w-fit">
<ToggleDarkMode />
2023-11-29 14:17:51 -06:00
2024-08-18 15:39:43 -05:00
<Link href="https://linkwarden.app/" target="_blank">
<Image
src={`/icon.png`}
width={551}
height={551}
alt="Linkwarden"
title={t("list_created_with_linkwarden")}
className="h-8 w-fit mx-auto rounded"
/>
</Link>
</div>
2023-11-16 02:22:16 -06:00
</div>
2023-11-15 22:31:13 -06:00
2024-08-18 15:39:43 -05:00
<div className="mt-3">
<div className={`min-w-[15rem]`}>
<div className="flex gap-1 justify-center sm:justify-end items-center w-fit">
<div
className="flex items-center btn px-2 btn-ghost rounded-full"
onClick={() => setEditCollectionSharingModal(true)}
>
{collectionOwner.id && (
<ProfilePhoto
src={collectionOwner.image || undefined}
name={collectionOwner.name}
/>
)}
{collection.members
.sort((a, b) => (a.userId as number) - (b.userId as number))
.map((e, i) => {
return (
<ProfilePhoto
key={i}
src={e.user.image ? e.user.image : undefined}
className="-ml-3"
name={e.user.name}
/>
);
})
.slice(0, 3)}
{collection.members.length - 3 > 0 && (
<div className={`avatar drop-shadow-md placeholder -ml-3`}>
<div className="bg-base-100 text-neutral rounded-full w-8 h-8 ring-2 ring-neutral-content">
<span>+{collection.members.length - 3}</span>
</div>
</div>
2024-08-18 15:39:43 -05:00
)}
</div>
2024-08-18 15:39:43 -05:00
<p className="text-neutral text-sm">
{collection.members.length > 0 &&
collection.members.length === 1
? t("by_author_and_other", {
2024-08-18 13:06:36 -05:00
author: collectionOwner.name,
count: collection.members.length,
})
2024-08-18 15:39:43 -05:00
: collection.members.length > 0 &&
collection.members.length !== 1
? t("by_author_and_others", {
author: collectionOwner.name,
count: collection.members.length,
})
: t("by_author", {
author: collectionOwner.name,
})}
</p>
</div>
2023-11-15 22:31:13 -06:00
</div>
</div>
2023-05-31 09:30:45 -05:00
2024-08-18 15:39:43 -05:00
<p className="mt-5">{collection.description}</p>
2023-11-15 22:31:13 -06:00
2024-08-18 15:39:43 -05:00
<div className="divider mt-5 mb-0"></div>
2023-11-16 02:22:16 -06:00
2024-08-18 15:39:43 -05:00
<div className="flex mb-5 mt-10 flex-col gap-5">
<LinkListOptions
t={t}
viewMode={viewMode}
setViewMode={setViewMode}
sortBy={sortBy}
setSortBy={setSortBy}
searchFilter={searchFilter}
setSearchFilter={setSearchFilter}
>
<SearchBar
placeholder={
collection._count?.links === 1
? t("search_count_link", {
count: collection._count?.links,
})
: t("search_count_links", {
count: collection._count?.links,
})
}
2023-12-24 06:30:45 -06:00
/>
2024-08-18 15:39:43 -05:00
</LinkListOptions>
2024-11-02 19:43:53 -05:00
{tags && tags[0] && (
<div className="flex gap-2 mt-2 mb-6 flex-wrap">
<button
className="max-w-full"
onClick={() => handleTagSelection(undefined)}
>
2024-11-02 19:43:53 -05:00
<div
className={`${
!router.query.q
? "bg-primary/20"
: "bg-neutral-content/20 hover:bg-neutral/20"
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 rounded-md h-8`}
>
2024-11-08 17:03:00 -06:00
<p className="truncate px-3">{t("all_links")}</p>
2024-11-02 19:43:53 -05:00
</div>
</button>
{tags
.map((t) => t.name)
.filter((item, pos, self) => self.indexOf(item) === pos)
.sort((a, b) => a.localeCompare(b))
.map((e, i) => {
const active = router.query.q === e;
return (
<button
className="max-w-full"
2024-11-02 19:43:53 -05:00
key={i}
onClick={() => handleTagSelection(e)}
>
<div
2024-11-02 19:43:53 -05:00
className={`${
active
? "bg-primary/20"
: "bg-neutral-content/20 hover:bg-neutral/20"
} duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 rounded-md h-8`}
>
2024-11-02 19:43:53 -05:00
<i className="bi-hash text-2xl text-primary drop-shadow"></i>
2024-11-08 17:03:00 -06:00
<p className="truncate pr-3">{e}</p>
</div>
</button>
2024-11-02 19:43:53 -05:00
);
})}
</div>
)}
2024-08-18 15:39:43 -05:00
<Links
links={
links?.map((e, i) => {
2023-12-24 06:30:45 -06:00
const linkWithCollectionData = {
...e,
collection: collection, // Append collection data
};
return linkWithCollectionData;
2024-08-18 15:39:43 -05:00
}) as any
}
2024-08-18 15:39:43 -05:00
layout={viewMode}
placeholderCount={1}
useData={data}
2023-12-24 06:30:45 -06:00
/>
2024-08-18 15:39:43 -05:00
{!data.isLoading && links && !links[0] && (
<p>{t("nothing_found")}</p>
)}
2023-05-31 09:30:45 -05:00
2024-08-18 15:39:43 -05:00
{/* <p className="text-center text-neutral">
2023-11-16 02:22:16 -06:00
List created with <span className="text-black">Linkwarden.</span>
</p> */}
2024-08-18 15:39:43 -05:00
</div>
2023-11-16 02:22:16 -06:00
</div>
2024-08-18 15:39:43 -05:00
{editCollectionSharingModal && (
<EditCollectionSharingModal
onClose={() => setEditCollectionSharingModal(false)}
activeCollection={collection}
/>
)}
2023-11-15 22:31:13 -06:00
</div>
2024-08-18 15:39:43 -05:00
);
2023-05-28 00:55:49 -05:00
}
export { getServerSideProps };