Skip to content

Commit 7ccabf4

Browse files
refactor(web): resting image previews and queued attachments are their own components
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1122a85 commit 7ccabf4

2 files changed

Lines changed: 145 additions & 117 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 24 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,7 @@ import {
158158
type ComposerBannerStackContent,
159159
type ComposerBannerStackItem,
160160
} from "./ComposerBannerStack";
161-
import {
162-
compressImageForStash,
163-
isHeicImageFile,
164-
prepareImageForAttachment,
165-
} from "../../lib/imageCompression";
166-
import { AttachmentImage } from "../media/AttachmentImage";
161+
import { compressImageForStash, prepareImageForAttachment } from "../../lib/imageCompression";
167162
import {
168163
fileAttachmentTooLargeMessage,
169164
formatAttachmentSize,
@@ -201,7 +196,6 @@ import {
201196
toKindScopedComposerContextId,
202197
} from "@t3tools/shared/composerContextReferences";
203198
import {
204-
getRestingComposerImagePreviewCounts,
205199
resolveRestingComposerControlsLayout,
206200
shouldAnimateComposerRestingTransition,
207201
shouldUseCompactComposerPrimaryActions,
@@ -286,11 +280,12 @@ import {
286280
} from "./ComposerCommandMenu";
287281
import { ComposerPendingApprovalActions } from "./ComposerPendingApprovalActions";
288282
import { CompactComposerControlsMenu } from "./CompactComposerControlsMenu";
289-
import { ComposerImageThumbnail } from "./ComposerImageThumbnail";
290283
import {
291284
ComposerAttachmentFileList,
292285
ComposerAttachmentMediaRow,
293286
ComposerFilePreviewDialog,
287+
ComposerQueuedAttachmentRow,
288+
ComposerRestingImagePreviews,
294289
} from "./ComposerAttachmentStrip";
295290
import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
296291
import { ComposerPendingApprovalPanel } from "./ComposerPendingApprovalPanel";
@@ -344,7 +339,6 @@ import {
344339
submitComposerDraft,
345340
} from "./composerSubmission";
346341
import { ComposerPromptLengthValidation } from "./ComposerPromptLengthValidation";
347-
import { PierreEntryIcon } from "./PierreEntryIcon";
348342
import { pendingDraftWork } from "./pendingDraftWork";
349343
import {
350344
importComposerThreadAttachment,
@@ -989,7 +983,6 @@ import {
989983
PencilRulerIcon,
990984
PlayIcon,
991985
ShieldIcon,
992-
XIcon,
993986
} from "lucide-react";
994987
import { proposedPlanTitle } from "@t3tools/shared/proposedPlanText";
995988
import { hasProviderSetup } from "./ProviderStatusBanner";
@@ -4830,85 +4823,24 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
48304823
measure();
48314824
return observeResize(restingActionsElement, measure);
48324825
}, [isComposerResting, restingActionsElement]);
4833-
const restingImagePreviewCounts = getRestingComposerImagePreviewCounts(
4834-
standaloneComposerImages.length,
4835-
);
4836-
const restingComposerImages = standaloneComposerImages.slice(
4837-
0,
4838-
restingImagePreviewCounts.visibleCount,
4839-
);
4826+
const showMoreRestingImages = () => {
4827+
if (isComposerCollapsedMobile) {
4828+
expandMobileComposer();
4829+
return;
4830+
}
4831+
setIsComposerScrollCollapsed(false);
4832+
setIsComposerFocused(true);
4833+
scheduleComposerFocus();
4834+
};
48404835
const collapsedComposerImagePreviews =
4841-
restingComposerImages.length > 0 ? (
4842-
<div
4843-
data-chat-composer-resting-images="true"
4844-
className="flex shrink-0 items-center gap-1 ps-1"
4845-
>
4846-
{restingComposerImages.map((image) => (
4847-
<button
4848-
key={image.id}
4849-
type="button"
4850-
className="relative size-7 shrink-0 cursor-zoom-in overflow-hidden rounded-md border border-border/70 bg-muted/60"
4851-
aria-label={`Preview ${image.name}`}
4852-
onPointerDown={(event) => event.preventDefault()}
4853-
onClick={() => {
4854-
const preview = buildExpandedImagePreview(composerImages, image.id);
4855-
if (preview) onExpandImage(preview);
4856-
}}
4857-
>
4858-
{image.previewUrl ? (
4859-
<ComposerImageThumbnail
4860-
file={image.file}
4861-
alt=""
4862-
className="size-full object-cover"
4863-
fallback={
4864-
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
4865-
<AttachmentImage
4866-
name={image.name}
4867-
mimeType={image.mimeType}
4868-
src={image.previewUrl}
4869-
alt=""
4870-
className="size-full object-cover"
4871-
/>
4872-
) : (
4873-
<PierreEntryIcon
4874-
pathValue={image.name}
4875-
kind="file"
4876-
theme={resolvedTheme}
4877-
className="m-auto size-3.5"
4878-
/>
4879-
)
4880-
}
4881-
/>
4882-
) : (
4883-
<PierreEntryIcon
4884-
pathValue={image.name}
4885-
kind="file"
4886-
theme={resolvedTheme}
4887-
className="m-auto size-3.5"
4888-
/>
4889-
)}
4890-
</button>
4891-
))}
4892-
{restingImagePreviewCounts.overflowCount > 0 ? (
4893-
<button
4894-
type="button"
4895-
className="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-md border border-border/70 bg-muted/60 font-medium text-secondary-label text-xs tabular-nums outline-none hover:bg-muted focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
4896-
aria-label={`Show ${String(restingImagePreviewCounts.overflowCount)} more image attachments`}
4897-
onPointerDown={(event) => event.preventDefault()}
4898-
onClick={() => {
4899-
if (isComposerCollapsedMobile) {
4900-
expandMobileComposer();
4901-
return;
4902-
}
4903-
setIsComposerScrollCollapsed(false);
4904-
setIsComposerFocused(true);
4905-
scheduleComposerFocus();
4906-
}}
4907-
>
4908-
+{restingImagePreviewCounts.overflowCount}
4909-
</button>
4910-
) : null}
4911-
</div>
4836+
standaloneComposerImages.length > 0 ? (
4837+
<ComposerRestingImagePreviews
4838+
images={standaloneComposerImages}
4839+
allImages={composerImages}
4840+
resolvedTheme={resolvedTheme}
4841+
onExpandImage={onExpandImage}
4842+
onShowMore={showMoreRestingImages}
4843+
/>
49124844
) : null;
49134845
const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
49144846
usePanelAnimationSettings();
@@ -6695,35 +6627,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
66956627
pendingUserInputs.length === 0 &&
66966628
editingQueuedAttachments !== null &&
66976629
editingQueuedAttachments.length > 0 && (
6698-
<div className="mb-3 flex flex-wrap gap-2">
6699-
{editingQueuedAttachments.map(({ attachment, url }) => (
6700-
<div
6701-
key={attachment.id}
6702-
className="relative h-16 w-16 overflow-hidden rounded-lg border border-border/80 bg-background"
6703-
>
6704-
{attachment.type === "image" && url ? (
6705-
<img
6706-
src={url}
6707-
alt={attachment.name}
6708-
className="h-full w-full object-cover"
6709-
/>
6710-
) : (
6711-
<div className="flex h-full w-full items-center justify-center px-1 text-center text-3xs text-secondary-label">
6712-
{attachment.name}
6713-
</div>
6714-
)}
6715-
<Button
6716-
variant="media-close"
6717-
size="icon-xs"
6718-
className="absolute right-1 top-1"
6719-
onClick={() => onRemoveEditingQueuedAttachment(attachment.id)}
6720-
aria-label={`Remove ${attachment.name}`}
6721-
>
6722-
<XIcon />
6723-
</Button>
6724-
</div>
6725-
))}
6726-
</div>
6630+
<ComposerQueuedAttachmentRow
6631+
attachments={editingQueuedAttachments}
6632+
onRemove={onRemoveEditingQueuedAttachment}
6633+
/>
67276634
)}
67286635

67296636
{composerAttachmentMediaRow}

‎apps/web/src/components/chat/ComposerAttachmentStrip.tsx‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
ChatAttachment,
23
ChatFileAttachment,
34
EnvironmentId,
45
ScopedThreadRef,
@@ -24,6 +25,7 @@ import {
2425
shouldAnimateSnapShotArrival,
2526
} from "../../lib/snapShotAnimation";
2627
import { cn } from "~/lib/utils";
28+
import { getRestingComposerImagePreviewCounts } from "../composerFooterLayout";
2729
import { RefreshIcon } from "~/components/ui/refresh-icon";
2830
import { AttachmentFilePreview } from "../files/AttachmentFilePreview";
2931
import { AttachmentImage } from "../media/AttachmentImage";
@@ -533,3 +535,122 @@ export function ComposerFilePreviewDialog({
533535
</Dialog>
534536
);
535537
}
538+
539+
/** Thumbnails beside the single-line prompt while the composer rests. */
540+
export function ComposerRestingImagePreviews({
541+
images,
542+
allImages,
543+
resolvedTheme,
544+
onExpandImage,
545+
onShowMore,
546+
}: {
547+
readonly images: ReadonlyArray<ComposerImageAttachment>;
548+
readonly allImages: ReadonlyArray<ComposerImageAttachment>;
549+
readonly resolvedTheme: "light" | "dark";
550+
readonly onExpandImage: (preview: ExpandedImagePreview) => void;
551+
readonly onShowMore: () => void;
552+
}) {
553+
const counts = getRestingComposerImagePreviewCounts(images.length);
554+
const visibleImages = images.slice(0, counts.visibleCount);
555+
return (
556+
<div data-chat-composer-resting-images="true" className="flex shrink-0 items-center gap-1 ps-1">
557+
{visibleImages.map((image) => (
558+
<button
559+
key={image.id}
560+
type="button"
561+
className="relative size-7 shrink-0 cursor-zoom-in overflow-hidden rounded-md border border-border/70 bg-muted/60"
562+
aria-label={`Preview ${image.name}`}
563+
onPointerDown={(event) => event.preventDefault()}
564+
onClick={() => {
565+
const preview = buildExpandedImagePreview(allImages, image.id);
566+
if (preview) onExpandImage(preview);
567+
}}
568+
>
569+
{image.previewUrl ? (
570+
<ComposerImageThumbnail
571+
file={image.file}
572+
alt=""
573+
className="size-full object-cover"
574+
fallback={
575+
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
576+
<AttachmentImage
577+
name={image.name}
578+
mimeType={image.mimeType}
579+
src={image.previewUrl}
580+
alt=""
581+
className="size-full object-cover"
582+
/>
583+
) : (
584+
<PierreEntryIcon
585+
pathValue={image.name}
586+
kind="file"
587+
theme={resolvedTheme}
588+
className="m-auto size-3.5"
589+
/>
590+
)
591+
}
592+
/>
593+
) : (
594+
<PierreEntryIcon
595+
pathValue={image.name}
596+
kind="file"
597+
theme={resolvedTheme}
598+
className="m-auto size-3.5"
599+
/>
600+
)}
601+
</button>
602+
))}
603+
{counts.overflowCount > 0 ? (
604+
<button
605+
type="button"
606+
className="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-md border border-border/70 bg-muted/60 font-medium text-secondary-label text-xs tabular-nums outline-none hover:bg-muted focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
607+
aria-label={`Show ${String(counts.overflowCount)} more image attachments`}
608+
onPointerDown={(event) => event.preventDefault()}
609+
onClick={onShowMore}
610+
>
611+
+{counts.overflowCount}
612+
</button>
613+
) : null}
614+
</div>
615+
);
616+
}
617+
618+
/** Attachments of the queued message being edited; they are already uploaded. */
619+
export function ComposerQueuedAttachmentRow({
620+
attachments,
621+
onRemove,
622+
}: {
623+
readonly attachments: ReadonlyArray<{
624+
readonly attachment: ChatAttachment;
625+
readonly url: string | null;
626+
}>;
627+
readonly onRemove: (attachmentId: string) => void;
628+
}) {
629+
return (
630+
<div className="mb-3 flex flex-wrap gap-2">
631+
{attachments.map(({ attachment, url }) => (
632+
<div
633+
key={attachment.id}
634+
className="relative h-16 w-16 overflow-hidden rounded-lg border border-border/80 bg-background"
635+
>
636+
{attachment.type === "image" && url ? (
637+
<img src={url} alt={attachment.name} className="h-full w-full object-cover" />
638+
) : (
639+
<div className="flex h-full w-full items-center justify-center px-1 text-center text-3xs text-secondary-label">
640+
{attachment.name}
641+
</div>
642+
)}
643+
<Button
644+
variant="media-close"
645+
size="icon-xs"
646+
className="absolute right-1 top-1"
647+
onClick={() => onRemove(attachment.id)}
648+
aria-label={`Remove ${attachment.name}`}
649+
>
650+
<XIcon />
651+
</Button>
652+
</div>
653+
))}
654+
</div>
655+
);
656+
}

0 commit comments

Comments
 (0)