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