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

ComposerPreviewAnnotationCards.tsx

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