diff --git a/packages/webui/components/chat/message-card.test.tsx b/packages/webui/components/chat/message-card.test.tsx new file mode 100644 index 00000000..ead1c418 --- /dev/null +++ b/packages/webui/components/chat/message-card.test.tsx @@ -0,0 +1,101 @@ +// @vitest-environment happy-dom +import React from 'react' +import { cleanup, render, screen, fireEvent } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import type { CommentInfo } from '@shumai/dtos' +import { MessageCard } from './message-card' + +function createWrapper() { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }) + return ({ children }: { children: React.ReactNode }) => ( + {children} + ) +} + +describe('MessageCard component', () => { + afterEach(() => { + cleanup() + vi.clearAllMocks() + }) + + it('renders message text and attachment rows with 2x height for image and standard height for other files', () => { + const mockMessage: CommentInfo = { + id: 'msg-1', + assetId: 'asset-1', + message: 'Here is the feedback on design', + annotations: null, + second: null, + creator: { id: 'u-1', name: 'Bob Reviewer' }, + replies: [], + attachments: [ + { + id: 'att-img-1', + assetId: 'asset-1', + url: 'https://example.com/screenshot.png?AWSAccessKeyId=123', + proxyType: null, + }, + { + id: 'att-doc-2', + assetId: 'asset-1', + url: 'https://example.com/notes.pdf?AWSAccessKeyId=123', + proxyType: null, + }, + { + id: 'att-psd-3', + assetId: 'asset-1', + url: 'https://example.com/mockup.psd', + proxyType: null, + }, + ], + mentions: [], + createdAt: '2026-08-20T10:30:00.000Z', + updatedAt: '2026-08-20T10:30:00.000Z', + sessionId: null, + isCompleted: false, + completionLastChangedBy: null, + } + + const onReply = vi.fn() + const getUser = vi.fn().mockReturnValue({ id: 'u-1', name: 'Bob Reviewer' }) + + render(, { + wrapper: createWrapper(), + }) + + expect(screen.getByText('Here is the feedback on design')).toBeDefined() + expect(screen.getByText('screenshot.png')).toBeDefined() + expect(screen.getByText('notes.pdf')).toBeDefined() + expect(screen.getByText('mockup.psd')).toBeDefined() + + // Verify row height classes: image row is h-18 (72px), file row is h-9 (36px) + const imgEl = screen.getByAltText('screenshot.png') + const imgRow = imgEl.closest('.group') + expect(imgRow?.className).toContain('h-18') + + const fileEl = screen.getByText('notes.pdf') + const fileRow = fileEl.closest('.group') + expect(fileRow?.className).toContain('h-9') + + // PSD should NOT be an image row (web browsers cannot render PSD in ) + const psdEl = screen.getByText('mockup.psd') + const psdRow = psdEl.closest('.group') + expect(psdRow?.className).toContain('h-9') + + // Clicking image row opens image preview dialog + fireEvent.click(imgRow!) + expect(screen.getAllByAltText('screenshot.png').length).toBeGreaterThanOrEqual(2) + + // Clicking file row opens URL in a new tab + const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null) + fireEvent.click(fileRow!) + expect(windowOpenSpy).toHaveBeenCalledWith( + 'https://example.com/notes.pdf?AWSAccessKeyId=123', + '_blank', + 'noreferrer', + ) + windowOpenSpy.mockRestore() + }) +}) diff --git a/packages/webui/components/chat/message-card.tsx b/packages/webui/components/chat/message-card.tsx index ef8004c8..23d98c84 100644 --- a/packages/webui/components/chat/message-card.tsx +++ b/packages/webui/components/chat/message-card.tsx @@ -16,10 +16,11 @@ import { AlertDialogHeader, AlertDialogTitle, } from '@/ui/components/ui/alert-dialog' -import type { AttachmentInfo, CommentInfo, UserInfo } from '@shumai/dtos' +import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog' +import type { CommentInfo, UserInfo } from '@shumai/dtos' import type { MemberInfo } from '@/ui/stores/members' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' -import { Download, File, MoreHorizontal, Trash2 } from 'lucide-react' +import { Download, FileText, MoreHorizontal, Trash2 } from 'lucide-react' import React from 'react' import Markdown from 'react-markdown' import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar' @@ -30,6 +31,7 @@ import { m } from '@/ui/paraglide/messages.js' import { useUiStore } from '@/ui/stores/ui' import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip' import { AgentSessionLogsDialog } from '../agent/agent-session-logs-dialog' +import { isImageFileName } from '@/ui/lib/media' interface MessageCardProps { teamId?: string @@ -39,7 +41,6 @@ interface MessageCardProps { isLastReply?: boolean getUser: (id: string) => MemberInfo | UserInfo onReply: (message: CommentInfo) => void - onViewAttachment: (attachment: AttachmentInfo) => void isSelected?: boolean onSelect?: () => void frameRate?: number @@ -100,7 +101,6 @@ export const MessageCard: React.FC = ({ isLastReply, getUser, onReply, - onViewAttachment, isSelected, onSelect, frameRate, @@ -116,6 +116,7 @@ export const MessageCard: React.FC = ({ const loadingText = (message.message && AI_PLACEHOLDERS[message.message]) || 'Generating...' const [isLogsOpen, setIsLogsOpen] = React.useState(false) + const [previewImage, setPreviewImage] = React.useState<{ url: string; name: string } | null>(null) const { videoTimeDisplayMode } = useUiStore() const displayTime = React.useMemo(() => { @@ -325,39 +326,56 @@ export const MessageCard: React.FC = ({ {/* Attachments */} {message.attachments && message.attachments.length > 0 && ( - + {message.attachments.map((att) => { - const isImage = att.proxyType === 'image' - const name = att.url?.split('/').pop()?.split('?')[0] || 'file' + const rawName = att.url?.split('/').pop()?.split('?')[0] || 'file' + const name = decodeURIComponent(rawName) + const isImage = isImageFileName(name) return ( isImage && onViewAttachment(att)} + className={`group relative flex items-center w-full max-w-full rounded-lg border border-border bg-card hover:bg-muted/40 transition-colors overflow-hidden ${ + isImage ? 'h-18 p-1.5 gap-2.5 cursor-pointer' : 'h-9 px-2.5 gap-2.5' + }`} + onClick={() => { + if (isImage) { + setPreviewImage({ url: att.url, name }) + } else { + window.open(att.url, '_blank', 'noreferrer') + } + }} > {isImage ? ( - - ) : ( - - - - {name} - + + + ) : ( + )} + + + + {name} + + + e.stopPropagation()} + title={m.download()} > - + ) @@ -458,6 +476,30 @@ export const MessageCard: React.FC = ({ + + {/* Image Preview Dialog */} + !open && setPreviewImage(null)}> + e.stopPropagation()} + > + + {previewImage?.name || 'Image Preview'} + + {previewImage && ( + + + + {previewImage.name} + + + )} + + ) } diff --git a/packages/webui/components/file-viewer-right-sidebar.tsx b/packages/webui/components/file-viewer-right-sidebar.tsx index ca86a15c..21fba44b 100644 --- a/packages/webui/components/file-viewer-right-sidebar.tsx +++ b/packages/webui/components/file-viewer-right-sidebar.tsx @@ -1,4 +1,4 @@ -import type { AssetInfo, AttachmentInfo, CommentInfo, FieldValueInfo } from '@shumai/dtos' +import type { AssetInfo, CommentInfo, FieldValueInfo } from '@shumai/dtos' import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos' import type { MemberInfo } from '@/ui/stores/members' import { client } from '@/ui/api/client' @@ -7,7 +7,6 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/ui/components/ui/tab import { useFieldStore } from '@/ui/stores/fields' import type { Annotation } from '@/ui/types' import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' -import { XIcon } from 'lucide-react' import React, { useEffect, useMemo, useState } from 'react' import { useInView } from 'react-intersection-observer' import { MessageCard } from './chat/message-card' @@ -56,7 +55,6 @@ export function FileViewerRightSidebar({ const queryClient = useQueryClient() const { ref, inView } = useInView() const [replyingTo, setReplyingTo] = useState(null) - const [viewingAttachment, setViewingAttachment] = useState(null) const [isGuestPopupOpen, setIsGuestPopupOpen] = useState(false) const [pendingComment, setPendingComment] = useState<{ text: string @@ -290,26 +288,6 @@ export function FileViewerRightSidebar({ return ( - {/* Lightbox */} - {viewingAttachment && viewingAttachment.proxyType === 'image' && ( - setViewingAttachment(null)} - > - - - - - - setViewingAttachment(null)} - /> - - - )} @@ -329,7 +307,6 @@ export function FileViewerRightSidebar({ message={comment} getUser={getUser} onReply={setReplyingTo} - onViewAttachment={setViewingAttachment} hasReplies={!!comment.replies?.length} formatTimestamp={viewerDef?.commentsConfig?.formatTimestamp} frameRate={ @@ -355,7 +332,6 @@ export function FileViewerRightSidebar({ message={reply} getUser={getUser} onReply={setReplyingTo} - onViewAttachment={setViewingAttachment} hasReplies={false} isLastReply={index === (comment.replies?.length ?? 0) - 1} formatTimestamp={viewerDef?.commentsConfig?.formatTimestamp} diff --git a/packages/webui/components/file-viewer/mobile-file-bottom-sheet.tsx b/packages/webui/components/file-viewer/mobile-file-bottom-sheet.tsx index 3623be4f..2afb934e 100644 --- a/packages/webui/components/file-viewer/mobile-file-bottom-sheet.tsx +++ b/packages/webui/components/file-viewer/mobile-file-bottom-sheet.tsx @@ -12,10 +12,9 @@ import { m } from '@/ui/paraglide/messages.js' import { useFieldStore } from '@/ui/stores/fields' import type { MemberInfo } from '@/ui/stores/members' import type { Annotation } from '@/ui/types' -import type { AssetInfo, AttachmentInfo, CommentInfo, FieldValueInfo } from '@shumai/dtos' +import type { AssetInfo, CommentInfo, FieldValueInfo } from '@shumai/dtos' import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos' import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' -import { XIcon } from 'lucide-react' import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useInView } from 'react-intersection-observer' import { toast } from 'sonner' @@ -62,7 +61,6 @@ export function MobileFileBottomSheet({ const queryClient = useQueryClient() const { ref, inView } = useInView() const [replyingTo, setReplyingTo] = useState(null) - const [viewingAttachment, setViewingAttachment] = useState(null) const [isGuestPopupOpen, setIsGuestPopupOpen] = useState(false) const [pendingComment, setPendingComment] = useState<{ text: string @@ -326,30 +324,6 @@ export function MobileFileBottomSheet({ className="shrink-0 flex flex-col bg-background border-t border-border shadow-2xl rounded-t-2xl overflow-hidden select-none z-20" data-testid="mobile-bottom-sheet" > - {/* Lightbox for attachment viewing */} - {viewingAttachment && viewingAttachment.proxyType === 'image' && ( - setViewingAttachment(null)} - > - - - - - - setViewingAttachment(null)} - /> - - - )} - {/* Drag Handle Bar */} void } export function TaskCommentCard({ @@ -42,10 +43,10 @@ export function TaskCommentCard({ comment, currentUserId, isOwnerOrAdmin, - onViewAttachment, }: TaskCommentCardProps) { const queryClient = useQueryClient() const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) + const [previewImage, setPreviewImage] = useState<{ url: string; name: string } | null>(null) const canDelete = isOwnerOrAdmin || comment.author.id === currentUserId @@ -88,7 +89,7 @@ export function TaskCommentCard({ .toUpperCase() return ( - + {/* Left Avatar */} {comment.author.image && ( @@ -100,7 +101,7 @@ export function TaskCommentCard({ {/* Main Content Area */} - + {/* Header: Author + Timestamp + Actions */} @@ -143,62 +144,60 @@ export function TaskCommentCard({ {/* Attachments Section */} {comment.attachments && comment.attachments.length > 0 && ( - + {comment.attachments.map((att) => { - const isImage = att.proxyType === 'image' || att.contentType?.startsWith('image/') + const isImage = isImageFileName(att.name || att.url) return ( { + if (isImage) { + setPreviewImage({ url: att.url, name: att.name }) + } else { + window.open(att.url, '_blank', 'noreferrer') + } + }} > {isImage ? ( - onViewAttachment?.(att)} - > + - e.stopPropagation()} - title="Download" - > - - ) : ( - - - - - {att.name} - - - {formatSize(att.sizeByte)} - - - - - - + )} + + + + {att.name} + + {att.sizeByte !== undefined && att.sizeByte > 0 && ( + + {formatSize(att.sizeByte)} + + )} + + + e.stopPropagation()} + title={m.download()} + > + + ) })} @@ -225,6 +224,30 @@ export function TaskCommentCard({ + + {/* Image Preview Dialog */} + !open && setPreviewImage(null)}> + e.stopPropagation()} + > + + {previewImage?.name || 'Image Preview'} + + {previewImage && ( + + + + {previewImage.name} + + + )} + + ) } diff --git a/packages/webui/components/kanban/edit-task-dialog/task-comment-thread.tsx b/packages/webui/components/kanban/edit-task-dialog/task-comment-thread.tsx index 1fed2320..3a1d45fd 100644 --- a/packages/webui/components/kanban/edit-task-dialog/task-comment-thread.tsx +++ b/packages/webui/components/kanban/edit-task-dialog/task-comment-thread.tsx @@ -1,13 +1,12 @@ -import { useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { client } from '@/ui/api/client' import { m } from '@/ui/paraglide/messages.js' import { ScrollArea } from '@/ui/components/ui/scroll-area' import { toast } from 'sonner' -import { Loader2, MessageSquare, X } from 'lucide-react' +import { Loader2, MessageSquare } from 'lucide-react' import { TaskCommentCard } from './task-comment-card' import { TaskCommentInput } from './task-comment-input' -import type { KanbanAttachmentInfo, KanbanAttachmentPayload, KanbanCommentInfo } from '@shumai/dtos' +import type { KanbanAttachmentPayload, KanbanCommentInfo } from '@shumai/dtos' interface TaskCommentThreadProps { teamId: string @@ -17,7 +16,6 @@ interface TaskCommentThreadProps { export function TaskCommentThread({ teamId, taskId, initialComments }: TaskCommentThreadProps) { const queryClient = useQueryClient() - const [viewingAttachment, setViewingAttachment] = useState(null) // Query Current User Info const { data: me } = useQuery({ @@ -92,35 +90,9 @@ export function TaskCommentThread({ teamId, taskId, initialComments }: TaskComme return ( - {/* Lightbox / Full-screen Attachment Viewer */} - {viewingAttachment && ( - setViewingAttachment(null)} - > - setViewingAttachment(null)} - className="absolute top-4 right-4 text-white/80 hover:text-white bg-white/10 hover:bg-white/20 rounded-full p-2 transition-colors z-50 cursor-pointer" - title="Close" - > - - - - - e.stopPropagation()} - /> - - - )} - {/* Comments List */} - - + + {isLoading ? ( @@ -141,7 +113,6 @@ export function TaskCommentThread({ teamId, taskId, initialComments }: TaskComme comment={comment} currentUserId={me?.id} isOwnerOrAdmin={isOwnerOrAdmin} - onViewAttachment={(att) => setViewingAttachment(att)} /> )) )} diff --git a/packages/webui/components/kanban/kanban.test.tsx b/packages/webui/components/kanban/kanban.test.tsx index e920707d..5a0bba12 100644 --- a/packages/webui/components/kanban/kanban.test.tsx +++ b/packages/webui/components/kanban/kanban.test.tsx @@ -449,7 +449,6 @@ describe('Kanban UI Unit & Component Tests', () => { updatedAt: '2026-08-20T00:00:00.000Z', } - const onViewAttachment = vi.fn() render( { comment={mockComment} currentUserId="u-1" isOwnerOrAdmin={true} - onViewAttachment={onViewAttachment} />, { wrapper: createWrapper() }, ) expect(screen.getByText('Alice Author')).toBeDefined() expect(screen.getByText('bold')).toBeDefined() + expect(screen.getByText('preview.png')).toBeDefined() expect(screen.getByText('specs.pdf')).toBeDefined() - // Click image attachment - const img = screen.getByAltText('preview.png') - fireEvent.click(img) - expect(onViewAttachment).toHaveBeenCalledWith(mockComment.attachments[0]) + // Verify row classes for 2x image height (h-18) vs standard file height (h-9) + const previewImg = screen.getByAltText('preview.png') + const imageRow = previewImg.closest('.group\\/att') + expect(imageRow?.className).toContain('h-18') + + const pdfText = screen.getByText('specs.pdf') + const pdfRow = pdfText.closest('.group\\/att') + expect(pdfRow?.className).toContain('h-9') + + // Click image attachment row opens preview dialog + fireEvent.click(imageRow!) + expect(screen.getAllByAltText('preview.png').length).toBeGreaterThanOrEqual(2) + + // Click PDF attachment row + const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null) + fireEvent.click(pdfRow!) + expect(windowOpenSpy).toHaveBeenCalledWith( + 'https://example.com/specs.pdf', + '_blank', + 'noreferrer', + ) + windowOpenSpy.mockRestore() }) it('renders TaskCommentInput and triggers send on button click', async () => { diff --git a/packages/webui/lib/media.test.ts b/packages/webui/lib/media.test.ts new file mode 100644 index 00000000..7a938e58 --- /dev/null +++ b/packages/webui/lib/media.test.ts @@ -0,0 +1,51 @@ +import { describe, it, expect } from 'vitest' +import { isImageFileName, getBestTranscode } from './media' + +describe('isImageFileName', () => { + it('identifies web-supported image formats as images', () => { + expect(isImageFileName('photo.png')).toBe(true) + expect(isImageFileName('photo.jpg')).toBe(true) + expect(isImageFileName('photo.jpeg')).toBe(true) + expect(isImageFileName('photo.webp')).toBe(true) + expect(isImageFileName('animation.gif')).toBe(true) + expect(isImageFileName('icon.svg')).toBe(true) + expect(isImageFileName('image.avif')).toBe(true) + expect(isImageFileName('bitmap.bmp')).toBe(true) + expect(isImageFileName('favicon.ico')).toBe(true) + }) + + it('handles uppercase extensions and paths/urls with query params', () => { + expect(isImageFileName('PHOTO.PNG')).toBe(true) + expect(isImageFileName('PHOTO.JPEG')).toBe(true) + expect(isImageFileName('/path/to/my-image.webp')).toBe(true) + }) + + it('does not treat non-web-renderable or non-image files as images', () => { + expect(isImageFileName('design.psd')).toBe(false) + expect(isImageFileName('photo.raw')).toBe(false) + expect(isImageFileName('document.pdf')).toBe(false) + expect(isImageFileName('video.mp4')).toBe(false) + expect(isImageFileName('archive.zip')).toBe(false) + expect(isImageFileName('notes.txt')).toBe(false) + expect(isImageFileName('')).toBe(false) + expect(isImageFileName(null)).toBe(false) + expect(isImageFileName(undefined)).toBe(false) + }) +}) + +describe('getBestTranscode', () => { + it('returns null if transcodes array is empty or undefined', () => { + expect(getBestTranscode(undefined, 800)).toBeNull() + expect(getBestTranscode([], 800)).toBeNull() + }) + + it('picks smallest transcode >= screenWidth or falls back to largest', () => { + const transcodes = [ + { id: '1', width: 400, height: 300, url: '', key: '', size: 100 }, + { id: '2', width: 800, height: 600, url: '', key: '', size: 200 }, + { id: '3', width: 1200, height: 900, url: '', key: '', size: 300 }, + ] + expect(getBestTranscode(transcodes, 700)?.width).toBe(800) + expect(getBestTranscode(transcodes, 1500)?.width).toBe(1200) + }) +}) diff --git a/packages/webui/lib/media.ts b/packages/webui/lib/media.ts index f2c01a24..59b3c3da 100644 --- a/packages/webui/lib/media.ts +++ b/packages/webui/lib/media.ts @@ -2,6 +2,13 @@ import type { ImageTranscode, VideoTranscode } from '@shumai/dtos' type Transcode = ImageTranscode | VideoTranscode +const WEB_IMAGE_EXTENSION_REGEX = /\.(png|jpe?g|webp|gif|svg|avif|bmp|ico)$/i + +export function isImageFileName(filename?: string | null): boolean { + if (!filename) return false + return WEB_IMAGE_EXTENSION_REGEX.test(filename) +} + export function getBestTranscode( transcodes: Transcode[] | undefined, screenWidth: number,
+ {name} +
+ {previewImage.name} +
- {att.name} -
- {formatSize(att.sizeByte)} -
+ {att.name} +
+ {formatSize(att.sizeByte)} +