Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerPreviewAnnotationCards.tsx

144 lines5.6 KB

1import type { PreviewAnnotationPayload } from "@t3tools/contracts";
2import { Frame, MousePointerClick, Paintbrush, PenLine, X } from "lucide-react";
3import type { ReactNode } from "react";
4​
5import type { ComposerImageAttachment } from "~/composerDraftStore";
6import { formatElementContextLabel, normalizeElementContextSelection } from "~/lib/elementContext";
7import { cn } from "~/lib/utils";
8​
9interface ComposerPreviewAnnotationCardsProps {
10 annotations: ReadonlyArray<PreviewAnnotationPayload>;
11 images: ReadonlyArray<ComposerImageAttachment>;
12 onRemove: (annotationId: string) => void;
13 onExpandImage: (imageId: string) => void;
14 className?: string;
16​
17function TargetStat(props: { icon: ReactNode; count: number; label: string }) {
18 return (
19 <span
20 className="inline-flex items-center gap-1 text-[10px] font-medium text-muted-foreground"
21 title={`${props.count} ${props.label}${props.count === 1 ? "" : "s"}`}
22 >
23 {props.icon}
24 {props.count}
25 </span>
26 );
28​
29export function ComposerPreviewAnnotationCards({
30 annotations,
31 images,
32 onRemove,
33 onExpandImage,
34 className,
35}: ComposerPreviewAnnotationCardsProps) {
36 if (annotations.length === 0) return null;
37 const imagesById = new Map(images.map((image) => [image.id, image]));
38​
39 return (
40 <div className={cn("flex flex-wrap gap-1.5", className)}>
41 {annotations.map((annotation) => {
42 const image = imagesById.get(annotation.id);
43 const elementLabels = annotation.elements.flatMap((target) => {
44 const context = normalizeElementContextSelection(target.element);
45 return context ? [{ id: target.id, label: formatElementContextLabel(context) }] : [];
46 });
47 return (
48 <section
49 key={annotation.id}
50 className="group/preview-annotation relative flex min-w-0 max-w-full items-center overflow-hidden rounded-lg border border-border/80 bg-background/72"
51 >
52 {image?.previewUrl ? (
53 <button
54 type="button"
55 aria-label={`Preview ${image.name}`}
56 className="size-14 shrink-0 cursor-zoom-in overflow-hidden border-r border-border/70 bg-muted"
57 onClick={() => onExpandImage(image.id)}
58 >
59 <img
60 src={image.previewUrl}
61 alt="Annotated preview crop"
62 className="size-full object-cover transition duration-200 group-hover/preview-annotation:scale-[1.03]"
63 />
64 </button>
65 ) : (
66 <span className="grid size-10 shrink-0 place-items-center border-r border-border/70 text-message-action">
67 <MousePointerClick className="size-3.5" />
68 </span>
69 )}
70 <div className="min-w-0 px-2.5 py-2 pr-8">
71 {annotation.comment.trim() ? (
72 <p className="max-w-80 truncate text-foreground text-xs font-medium">
73 {annotation.comment.trim()}
74 </p>
75 ) : null}
76 <div
77 className={cn(
78 "flex min-w-0 items-center gap-2",
79 annotation.comment.trim() && "mt-1",
80 )}
81 >
82 {elementLabels.length > 0 ? (
83 <div className="flex min-w-0 items-center gap-1">
84 {elementLabels.slice(0, 2).map(({ id, label }) => (
85 <span
86 key={id}
87 className="max-w-40 truncate font-mono text-secondary-label text-[10px]"
88 >
89 {label}
90 </span>
91 ))}
92 {elementLabels.length > 2 ? (
93 <span className="text-secondary-label text-[10px]">
94 +{elementLabels.length - 2}
95 </span>
96 ) : null}
97 </div>
98 ) : null}
99 <div className="flex shrink-0 items-center gap-2">
100 {annotation.elements.length > 0 ? (
101 <TargetStat
102 icon={<MousePointerClick className="size-3" />}
103 count={annotation.elements.length}
104 label="element"
105 />
106 ) : null}
107 {annotation.regions.length > 0 ? (
108 <TargetStat
109 icon={<Frame className="size-3" />}
110 count={annotation.regions.length}
111 label="region"
112 />
113 ) : null}
114 {annotation.strokes.length > 0 ? (
115 <TargetStat
116 icon={<PenLine className="size-3" />}
117 count={annotation.strokes.length}
118 label="drawing"
119 />
120 ) : null}
121 {annotation.styleChanges.length > 0 ? (
122 <TargetStat
123 icon={<Paintbrush className="size-3" />}
124 count={annotation.styleChanges.length}
125 label="style change"
126 />
127 ) : null}
128 </div>
129 </div>
130 </div>
131 <button
132 type="button"
133 aria-label="Remove preview annotation"
134 className="absolute right-1.5 top-1.5 grid size-5 place-items-center rounded text-icon-muted transition hover:bg-muted hover:text-foreground"
135 onClick={() => onRemove(annotation.id)}
136 >
137 <X className="size-3" />
138 </button>
139 </section>
140 );
141 })}
142 </div>
143 );