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

147 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-12-15 21:25:39 -06:00
import {
CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
import { useEffect, useState } from "react";
import useLinkStore from "@/store/links";
import unescapeString from "@/lib/client/unescapeString";
import LinkActions from "@/components/LinkViews/LinkComponents/LinkActions";
import LinkDate from "@/components/LinkViews/LinkComponents/LinkDate";
import LinkCollection from "@/components/LinkViews/LinkComponents/LinkCollection";
import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
2024-01-15 02:39:53 -06:00
import { isPWA } from "@/lib/client/utils";
2024-02-08 00:44:41 -06:00
import { generateLinkHref } from "@/lib/client/generateLinkHref";
import usePermissions from "@/hooks/usePermissions";
2024-02-13 13:35:31 -06:00
import toast from "react-hot-toast";
2024-08-13 02:01:02 -05:00
import LinkTypeBadge from "./LinkTypeBadge";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
2024-07-30 13:57:09 -05:00
import { useCollections } from "@/hooks/store/collections";
2024-07-31 13:15:50 -05:00
import { useUser } from "@/hooks/store/user";
import { useLinks } from "@/hooks/store/links";
import useLocalSettingsStore from "@/store/localSettings";
2023-12-15 21:25:39 -06:00
type Props = {
link: LinkIncludingShortenedCollectionAndTags;
count: number;
className?: string;
2024-02-10 23:55:00 -06:00
editMode?: boolean;
2023-12-15 21:25:39 -06:00
};
export default function LinkCardCompact({ link, editMode }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
const { data: collections = [] } = useCollections();
2024-07-30 13:57:09 -05:00
const { data: user = {} } = useUser();
const { setSelectedLinks, selectedLinks } = useLinkStore();
const {
settings: { show },
} = useLocalSettingsStore();
const { links } = useLinks();
2024-03-06 08:06:38 -06:00
useEffect(() => {
if (!editMode) {
setSelectedLinks([]);
}
}, [editMode]);
2024-02-11 01:29:11 -06:00
const handleCheckboxClick = (
link: LinkIncludingShortenedCollectionAndTags
) => {
const linkIndex = selectedLinks.findIndex(
(selectedLink) => selectedLink.id === link.id
);
if (linkIndex !== -1) {
const updatedLinks = [...selectedLinks];
updatedLinks.splice(linkIndex, 1);
setSelectedLinks(updatedLinks);
} else {
setSelectedLinks([...selectedLinks, link]);
}
};
2023-12-15 21:25:39 -06:00
const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>(
collections.find(
2023-12-16 14:06:26 -06:00
(e) => e.id === link.collection.id
) as CollectionIncludingMembersAndLinkCount
2023-12-15 21:25:39 -06:00
);
useEffect(() => {
setCollection(
collections.find(
2023-12-16 14:06:26 -06:00
(e) => e.id === link.collection.id
) as CollectionIncludingMembersAndLinkCount
2023-12-15 21:25:39 -06:00
);
}, [collections, links]);
const permissions = usePermissions(collection?.id as number);
2024-02-13 04:54:18 -06:00
const selectedStyle = selectedLinks.some(
(selectedLink) => selectedLink.id === link.id
)
? "border border-primary bg-base-300"
: "border-transparent";
2024-02-13 09:55:51 -06:00
2024-02-13 04:54:18 -06:00
const selectable =
editMode &&
(permissions === true || permissions?.canCreate || permissions?.canDelete);
2023-12-15 21:25:39 -06:00
return (
2023-12-16 14:06:26 -06:00
<>
2023-12-24 06:30:45 -06:00
<div
2024-08-26 15:11:02 -05:00
className={`${selectedStyle} rounded-md border relative items-center flex ${
!isPWA() ? "hover:bg-base-300 px-2 py-1" : "py-1"
} duration-200 w-full`}
2024-02-13 13:35:31 -06:00
onClick={() =>
selectable
? handleCheckboxClick(link)
: editMode
2024-06-09 08:27:16 -05:00
? toast.error(t("link_selection_error"))
2024-02-13 13:35:31 -06:00
: undefined
}
2023-12-24 06:30:45 -06:00
>
2024-03-06 08:06:38 -06:00
<div
className="flex items-center cursor-pointer w-full min-h-12"
2024-03-06 08:06:38 -06:00
onClick={() =>
!editMode && window.open(generateLinkHref(link, user), "_blank")
2024-03-06 08:06:38 -06:00
}
>
{show.icon && (
<div className="shrink-0">
<LinkIcon link={link} hideBackground />
</div>
)}
2024-03-06 08:06:38 -06:00
<div className="w-[calc(100%-56px)] ml-2">
{show.name && (
<p className="line-clamp-1 mr-8 text-primary select-none">
{unescapeString(link.name)}
</p>
)}
2024-03-06 08:06:38 -06:00
<div className="mt-1 flex flex-col sm:flex-row sm:items-center gap-2 text-xs text-neutral">
2024-05-24 18:13:04 -05:00
<div className="flex items-center gap-x-3 text-neutral flex-wrap">
{show.link && <LinkTypeBadge link={link} />}
{show.collection && (
2024-03-06 08:06:38 -06:00
<LinkCollection link={link} collection={collection} />
)}
{show.date && <LinkDate link={link} />}
</div>
2024-02-13 04:54:18 -06:00
</div>
2024-03-06 08:06:38 -06:00
</div>
</div>
<LinkActions
link={link}
collection={collection}
className="top-3 right-3"
2024-03-06 08:06:38 -06:00
/>
2023-12-15 21:25:39 -06:00
</div>
2024-08-26 15:11:02 -05:00
<div className="last:hidden rounded-none my-0 mx-1 border-t border-base-300 h-[1px]"></div>
2023-12-16 14:06:26 -06:00
</>
2023-12-15 21:25:39 -06:00
);
}