|
| 1 | +import { BottomBanner } from './bottom-banner' |
| 2 | +import { ImageCard } from './image-card' |
| 3 | +import { TextAttachmentCard } from './text-attachment-card' |
| 4 | +import { useTheme } from '../hooks/use-theme' |
| 5 | +import { useChatStore } from '../state/chat-store' |
| 6 | + |
| 7 | +/** |
| 8 | + * Combined banner for both image and text attachments. |
| 9 | + * Displays all attachments in a single horizontal row. |
| 10 | + */ |
| 11 | +export const PendingAttachmentsBanner = () => { |
| 12 | + const theme = useTheme() |
| 13 | + const pendingImages = useChatStore((state) => state.pendingImages) |
| 14 | + const removePendingImage = useChatStore((state) => state.removePendingImage) |
| 15 | + const pendingTextAttachments = useChatStore( |
| 16 | + (state) => state.pendingTextAttachments, |
| 17 | + ) |
| 18 | + const removePendingTextAttachment = useChatStore( |
| 19 | + (state) => state.removePendingTextAttachment, |
| 20 | + ) |
| 21 | + |
| 22 | + // Separate error messages from actual images |
| 23 | + const errorImages: typeof pendingImages = [] |
| 24 | + const validImages: typeof pendingImages = [] |
| 25 | + for (const img of pendingImages) { |
| 26 | + if (img.status === 'error') { |
| 27 | + errorImages.push(img) |
| 28 | + } else { |
| 29 | + validImages.push(img) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const hasValidImages = validImages.length > 0 |
| 34 | + const hasTextAttachments = pendingTextAttachments.length > 0 |
| 35 | + const hasErrorsOnly = errorImages.length > 0 && !hasValidImages && !hasTextAttachments |
| 36 | + |
| 37 | + // Nothing to show |
| 38 | + if (!hasValidImages && !hasTextAttachments && errorImages.length === 0) { |
| 39 | + return null |
| 40 | + } |
| 41 | + |
| 42 | + // If we only have errors (no valid attachments), show just the error messages |
| 43 | + if (hasErrorsOnly) { |
| 44 | + return ( |
| 45 | + <BottomBanner borderColorKey="error"> |
| 46 | + {errorImages.map((image, index) => ( |
| 47 | + <text key={`${image.path}-${index}`} style={{ fg: theme.error }}> |
| 48 | + {image.note} ({image.filename}) |
| 49 | + </text> |
| 50 | + ))} |
| 51 | + </BottomBanner> |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <BottomBanner borderColorKey="imageCardBorder"> |
| 57 | + {/* Error messages shown above the attachments */} |
| 58 | + {errorImages.map((image, index) => ( |
| 59 | + <text key={`error-${image.path}-${index}`} style={{ fg: theme.error }}> |
| 60 | + {image.note} ({image.filename}) |
| 61 | + </text> |
| 62 | + ))} |
| 63 | + |
| 64 | + {/* All attachment cards in a horizontal row */} |
| 65 | + <box |
| 66 | + style={{ |
| 67 | + flexDirection: 'row', |
| 68 | + gap: 1, |
| 69 | + flexWrap: 'wrap', |
| 70 | + }} |
| 71 | + > |
| 72 | + {/* Image cards */} |
| 73 | + {validImages.map((image, index) => ( |
| 74 | + <ImageCard |
| 75 | + key={`img-${image.path}-${index}`} |
| 76 | + image={image} |
| 77 | + onRemove={() => removePendingImage(image.path)} |
| 78 | + /> |
| 79 | + ))} |
| 80 | + |
| 81 | + {/* Text attachment cards */} |
| 82 | + {pendingTextAttachments.map((attachment) => ( |
| 83 | + <TextAttachmentCard |
| 84 | + key={attachment.id} |
| 85 | + attachment={attachment} |
| 86 | + onRemove={() => removePendingTextAttachment(attachment.id)} |
| 87 | + /> |
| 88 | + ))} |
| 89 | + </box> |
| 90 | + </BottomBanner> |
| 91 | + ) |
| 92 | +} |
0 commit comments