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