el.xwx.moe/components/LinkCard.tsx

114 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-06-05 05:16:04 -05:00
import {
2023-12-15 22:08:28 -06:00
CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags,
2023-06-05 05:16:04 -05:00
} from "@/types/global";
2023-12-15 22:08:28 -06:00
import { faLink } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useEffect, useState } from "react";
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-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-12-15 22:05:19 -06:00
import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
2023-03-05 15:03:20 -06:00
2023-05-27 11:53:02 -05:00
type Props = {
2023-12-15 22:08:28 -06:00
link: LinkIncludingShortenedCollectionAndTags;
count: number;
className?: string;
2023-05-27 11:53:02 -05:00
};
2023-12-15 22:08:28 -06:00
export default function LinkCard({ link, count, className }: Props) {
const { links } = useLinkStore();
2023-12-15 22:08:28 -06:00
const { collections } = useCollectionStore();
2023-12-15 22:08:28 -06:00
const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>(
collections.find(
(e) => e.id === link.collection.id,
) as CollectionIncludingMembersAndLinkCount,
);
2023-12-15 22:08:28 -06:00
useEffect(() => {
setCollection(
collections.find(
(e) => e.id === link.collection.id,
) as CollectionIncludingMembersAndLinkCount,
);
}, [collections, links]);
2023-12-15 22:08:28 -06:00
let shortendURL;
2023-12-15 22:08:28 -06:00
try {
shortendURL = new URL(link.url || "").host.toLowerCase();
} catch (error) {
console.log(error);
}
2023-12-05 14:17:36 -06:00
2023-12-15 22:08:28 -06:00
const url =
isValidUrl(link.url || "") && link.url ? new URL(link.url) : undefined;
2023-12-15 22:08:28 -06:00
return (
<div
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 || ""
}`}
>
<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"
>
<div className="absolute bottom-3 right-3">
<LinkIcon link={link} />
</div>
2023-12-15 21:25:39 -06:00
2023-12-15 22:08:28 -06:00
<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 22:08:28 -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>
)}
2023-12-15 21:25:39 -06:00
2023-12-15 22:08:28 -06:00
<LinkCollection link={link} collection={collection} />
2023-12-15 21:25:39 -06:00
2023-12-15 22:08:28 -06:00
<LinkDate link={link} />
{/* {link.tags[0] ? (
2023-12-15 21:25:39 -06:00
<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>
)} */}
2023-12-15 22:08:28 -06:00
</div>
2023-12-15 21:25:39 -06:00
2023-12-15 22:08:28 -06:00
<LinkActions link={link} collection={collection} />
</div>
);
2023-03-05 15:03:20 -06:00
}