el.xwx.moe/components/LinkCard.tsx

139 lines
5.0 KiB
TypeScript
Raw Normal View History

2023-06-05 05:16:04 -05:00
import {
2023-12-15 21:25:39 -06:00
CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
2023-06-05 05:16:04 -05:00
} from "@/types/global";
2023-12-15 21:25:39 -06:00
import {faLink} from "@fortawesome/free-solid-svg-icons";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {useEffect, useState} from "react";
2023-03-10 23:10:10 -06:00
import Image from "next/image";
2023-03-23 10:25:17 -05:00
import useLinkStore from "@/store/links";
2023-06-05 05:16:04 -05:00
import useCollectionStore from "@/store/collections";
2023-12-05 14:17:36 -06:00
import {
2023-12-15 21:25:39 -06:00
faFileImage,
faFilePdf,
2023-12-05 14:17:36 -06:00
} from "@fortawesome/free-regular-svg-icons";
2023-11-25 02:27:34 -06:00
import isValidUrl from "@/lib/shared/isValidUrl";
import unescapeString from "@/lib/client/unescapeString";
2023-12-15 21:25:39 -06:00
import LinkActions from "@/components/LinkViews/LinkComponents/LinkActions";
import LinkDate from "@/components/LinkViews/LinkComponents/LinkDate";
import LinkCollection from "@/components/LinkViews/LinkComponents/LinkCollection";
2023-03-05 15:03:20 -06:00
2023-05-27 11:53:02 -05:00
type Props = {
2023-12-15 21:25:39 -06:00
link: LinkIncludingShortenedCollectionAndTags;
count: number;
className?: string;
2023-05-27 11:53:02 -05:00
};
2023-12-15 21:25:39 -06:00
export default function LinkCard({link, count, className}: Props) {
const {links} = useLinkStore();
2023-12-15 21:25:39 -06:00
const {collections} = useCollectionStore();
2023-12-15 21:25:39 -06:00
const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>(
collections.find(
(e) => e.id === link.collection.id
) as CollectionIncludingMembersAndLinkCount
);
2023-12-15 21:25:39 -06:00
useEffect(() => {
setCollection(
collections.find(
(e) => e.id === link.collection.id
) as CollectionIncludingMembersAndLinkCount
);
}, [collections, links]);
2023-12-15 21:25:39 -06:00
let shortendURL;
2023-12-15 21:25:39 -06:00
try {
shortendURL = new URL(link.url || "").host.toLowerCase();
} catch (error) {
console.log(error);
2023-05-27 11:29:39 -05:00
}
2023-12-05 14:17:36 -06:00
2023-12-15 21:25:39 -06:00
const url =
isValidUrl(link.url || "") && link.url ? new URL(link.url) : undefined;
2023-12-15 21:25:39 -06:00
return (
2023-12-06 23:33:05 -06:00
<div
2023-12-15 21:25:39 -06:00
className={`h-fit hover:bg-base-300 hover:border-base-300 border border-solid border-neutral-content bg-base-200 shadow-md hover:shadow-none duration-200 rounded-2xl relative ${
className || ""
}`}
2023-12-05 14:17:36 -06:00
>
2023-12-15 21:25:39 -06:00
<div
onClick={() => link.url && window.open(link.url || "", "_blank")}
className="flex flex-col justify-between cursor-pointer h-full w-full gap-1 p-3"
>
{link.url && url ? (
<Image
src={`https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${link.url}&size=32`}
width={64}
height={64}
alt=""
className={`absolute w-12 bg-white shadow rounded-md p-1 bottom-3 right-3 select-none z-10`}
draggable="false"
onError={(e) => {
const target = e.target as HTMLElement;
target.style.display = "none";
}}
/>
) : link.type === "pdf" ? (
<FontAwesomeIcon
icon={faFilePdf}
className="absolute h-12 w-12 bg-primary/20 text-primary shadow rounded-md p-2 bottom-3 right-3 select-none z-10"
/>
) : link.type === "image" ? (
<FontAwesomeIcon
icon={faFileImage}
className="absolute h-12 w-12 bg-primary/20 text-primary shadow rounded-md p-2 bottom-3 right-3 select-none z-10"
/>
) : undefined}
<div className="flex items-baseline gap-1">
<p className="text-sm text-neutral">{count + 1}</p>
<p className="text-lg truncate w-full pr-8">
{unescapeString(link.name || link.description) || link.url}
</p>
</div>
2023-12-05 14:17:36 -06:00
2023-12-15 21:25:39 -06:00
{link.url ? (
<div className="flex items-center gap-1 max-w-full w-fit text-neutral">
<FontAwesomeIcon icon={faLink} className="mt-1 w-4 h-4"/>
<p className="truncate w-full">{shortendURL}</p>
</div>
) : (
<div className="badge badge-primary badge-sm my-1">{link.type}</div>
)}
<LinkCollection link={link} collection={collection}/>
<LinkDate link={link}/>
{/* {link.tags[0] ? (
<div className="flex gap-3 items-center flex-wrap mt-2 truncate relative">
<div className="flex gap-1 items-center flex-nowrap">
{link.tags.map((e, i) => (
<Link
href={"/tags/" + e.id}
key={i}
onClick={(e) => {
e.stopPropagation();
}}
className="btn btn-xs btn-ghost truncate max-w-[19rem]"
>
#{e.name}
</Link>
))}
</div>
<div className="absolute w-1/2 top-0 bottom-0 right-0 bg-gradient-to-r from-transparent to-base-200 to-35%"></div>
</div>
) : (
<p className="text-xs mt-2 p-1 font-semibold italic">No Tags</p>
)} */}
</div>
<LinkActions link={link} collection={collection}/>
2023-12-05 14:17:36 -06:00
</div>
2023-12-15 21:25:39 -06:00
);
2023-03-05 15:03:20 -06:00
}