import type { Document } from "dbtype/api"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card.tsx"; import TagBadge from "@/components/gallery/TagBadge.tsx"; import { Fragment, useLayoutEffect, useRef, useState } from "react"; import { LazyImage } from "./LazyImage.tsx"; import StyledLink from "./StyledLink.tsx"; import React from "react"; function clipTagsWhenOverflow(tags: string[], limit: number) { let l = 0; for (let i = 0; i < tags.length; i++) { l += tags[i].length; if (l > limit) { return tags.slice(0, i); } l += 1; // for space } return tags; } function GalleryCardImpl({ doc: x }: { doc: Document; }) { const ref = useRef(null); const [clipCharCount, setClipCharCount] = useState(200); const isDeleted = x.deleted_at !== null; const artists = x.tags.filter(x => x.startsWith("artist:")).map(x => x.replace("artist:", "")); const groups = x.tags.filter(x => x.startsWith("group:")).map(x => x.replace("group:", "")); const originalTags = x.tags.filter(x => !x.startsWith("artist:") && !x.startsWith("group:")); const clippedTags = clipTagsWhenOverflow(originalTags, clipCharCount); useLayoutEffect(() => { const listener = () => { if (ref.current) { const { width } = ref.current.getBoundingClientRect(); const charWidth = 7; // rough estimate const newClipCharCount = Math.floor(width / charWidth) * 3; setClipCharCount(newClipCharCount); } }; listener(); window.addEventListener("resize", listener); return () => { window.removeEventListener("resize", listener); }; }, []); return {isDeleted ?
Deleted
:
}
{x.title} {artists.map((x, i) => {x} {i + 1 < artists.length && , } )} {groups.length > 0 && {" | "}} {groups.map((x, i) => {x} {i + 1 < groups.length && , } )}
    {clippedTags.map(tag => )} {clippedTags.length < originalTags.length && }
; } export const GalleryCard = React.memo(GalleryCardImpl);