mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 07:57:15 +00:00
feat(webui): show comments count badge in file card preview area (#403)
* feat(webui): show comments count badge in file card preview area * perf(db): add index on asset_comments asset_id
This commit is contained in:
@@ -4420,4 +4420,194 @@ describe('AssetService — natural sort by name', () => {
|
||||
expect(folderRes.data.length).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('commentsCount in toAssetInfos', () => {
|
||||
it('returns commentsCount 0 when file has no comments', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
parentId: rootFolder.id,
|
||||
creatorId: user.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.listChildren({
|
||||
assetId: rootFolder.id,
|
||||
assetType: 'file',
|
||||
})
|
||||
const fileInfo = res.data.find((a) => a.id === file.id)
|
||||
expect(fileInfo).toBeDefined()
|
||||
expect(fileInfo?.commentsCount).toBe(0)
|
||||
})
|
||||
|
||||
it('returns total comments count including replies for a file', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
parentId: rootFolder.id,
|
||||
creatorId: user.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
const rootComment = await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: file.id,
|
||||
creatorId: user.id,
|
||||
message: 'Root comment 1',
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: file.id,
|
||||
creatorId: user.id,
|
||||
message: 'Reply to comment 1',
|
||||
replyToId: rootComment.id,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: file.id,
|
||||
creatorId: user.id,
|
||||
message: 'Root comment 2',
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.listChildren({
|
||||
assetId: rootFolder.id,
|
||||
assetType: 'file',
|
||||
})
|
||||
const fileInfo = res.data.find((a) => a.id === file.id)
|
||||
expect(fileInfo).toBeDefined()
|
||||
expect(fileInfo?.commentsCount).toBe(3)
|
||||
})
|
||||
|
||||
it('returns comments count of the active version for version stack', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video_stack',
|
||||
type: AssetType.version_stack,
|
||||
projectId: project.id,
|
||||
parentId: rootFolder.id,
|
||||
creatorId: user.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// Older version (v1): larger sortIndex
|
||||
const fileV1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video_v1.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
parentId: stack.id,
|
||||
creatorId: user.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sortIndex: 'b',
|
||||
},
|
||||
})
|
||||
|
||||
// Active / latest version (v2): smaller sortIndex
|
||||
const fileV2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video_v2.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
parentId: stack.id,
|
||||
creatorId: user.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sortIndex: 'a',
|
||||
},
|
||||
})
|
||||
|
||||
// Add 2 comments to v1
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: fileV1.id,
|
||||
creatorId: user.id,
|
||||
message: 'Comment on v1',
|
||||
},
|
||||
})
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: fileV1.id,
|
||||
creatorId: user.id,
|
||||
message: 'Another comment on v1',
|
||||
},
|
||||
})
|
||||
|
||||
// Add 1 comment to v2 (active version)
|
||||
await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: fileV2.id,
|
||||
creatorId: user.id,
|
||||
message: 'Comment on v2',
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.listChildren({
|
||||
assetId: rootFolder.id,
|
||||
assetType: 'file',
|
||||
})
|
||||
const stackInfo = res.data.find((a) => a.id === stack.id)
|
||||
expect(stackInfo).toBeDefined()
|
||||
// Should reflect active version (v2) comments count
|
||||
expect(stackInfo?.commentsCount).toBe(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2026,6 +2026,38 @@ export class AssetService {
|
||||
: []
|
||||
const hasAgentMdSet = new Set(agentMdRecords.map((r) => r.assetId))
|
||||
|
||||
const commentTargetIds = new Set<string>()
|
||||
for (const a of assets) {
|
||||
if (
|
||||
a.type === AssetType.version_stack ||
|
||||
(a.type === AssetType.symlink && a.target?.type === AssetType.version_stack)
|
||||
) {
|
||||
const stackId = a.type === AssetType.symlink ? a.targetId! : a.id
|
||||
const versions = versionsMap.get(stackId) || []
|
||||
if (versions.length > 0) {
|
||||
commentTargetIds.add(versions[0].id)
|
||||
} else {
|
||||
commentTargetIds.add(stackId)
|
||||
}
|
||||
} else if (a.type === AssetType.symlink && a.target) {
|
||||
commentTargetIds.add(a.target.id)
|
||||
} else {
|
||||
commentTargetIds.add(a.id)
|
||||
}
|
||||
}
|
||||
|
||||
const commentCounts =
|
||||
commentTargetIds.size > 0
|
||||
? await this.prismaClient.assetComment.groupBy({
|
||||
by: ['assetId'],
|
||||
where: {
|
||||
assetId: { in: Array.from(commentTargetIds) },
|
||||
},
|
||||
_count: { _all: true },
|
||||
})
|
||||
: []
|
||||
const commentCountMap = new Map(commentCounts.map((c) => [c.assetId, c._count._all]))
|
||||
|
||||
const result: AssetInfo[] = []
|
||||
for (const a of assets) {
|
||||
let latestVersion:
|
||||
@@ -2207,6 +2239,7 @@ export class AssetService {
|
||||
fieldValues,
|
||||
sortIndex: a.sortIndex,
|
||||
hasAgentsMd: hasAgentMdSet.has(a.id),
|
||||
commentsCount: commentCountMap.get(latestVersion.id) ?? 0,
|
||||
media: media as unknown as AssetInfo['media'],
|
||||
versionStack,
|
||||
})
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
-- CreateIndex
|
||||
CREATE INDEX "asset_comments_asset_id_idx" ON "asset_comments"("asset_id");
|
||||
@@ -657,6 +657,7 @@ model AssetComment {
|
||||
|
||||
attachments AssetCommentAttachment[] @relation("CommentToAttachment")
|
||||
|
||||
@@index([assetId])
|
||||
@@map("asset_comments")
|
||||
}
|
||||
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -83,6 +83,7 @@ export const assetInfoSchema = z.object({
|
||||
startTime: z.number().optional().nullable(),
|
||||
endTime: z.number().optional().nullable(),
|
||||
hasAgentsMd: z.boolean().optional(),
|
||||
commentsCount: z.number().optional(),
|
||||
|
||||
media: z
|
||||
.object({
|
||||
|
||||
@@ -214,4 +214,43 @@ describe('FileCard', () => {
|
||||
expect(tooltip.textContent).toContain('Very Long Creator Name')
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('renders comments count badge in preview area when commentsCount > 0', () => {
|
||||
const itemWithComments: AssetInfo = {
|
||||
...fileItem,
|
||||
commentsCount: 5,
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: itemWithComments })
|
||||
|
||||
const commentsBadge = screen.getByTestId('file-card-comments-count')
|
||||
expect(commentsBadge).toBeTruthy()
|
||||
expect(commentsBadge.textContent).toContain('5')
|
||||
expect(commentsBadge.className).toContain('absolute')
|
||||
expect(commentsBadge.className).toContain('bottom-1')
|
||||
expect(commentsBadge.className).toContain('left-1')
|
||||
expect(commentsBadge.className).toContain('bg-black/60')
|
||||
expect(commentsBadge.className).toContain('text-white')
|
||||
expect(commentsBadge.className).toContain('pointer-events-none')
|
||||
expect(commentsBadge.className).toContain('tabular-nums')
|
||||
})
|
||||
|
||||
it('does not render comments count badge when commentsCount is 0 or undefined', () => {
|
||||
const itemZeroComments: AssetInfo = {
|
||||
...fileItem,
|
||||
commentsCount: 0,
|
||||
} as AssetInfo
|
||||
|
||||
const { unmount } = renderComponent({ item: itemZeroComments })
|
||||
expect(screen.queryByTestId('file-card-comments-count')).toBeNull()
|
||||
unmount()
|
||||
|
||||
const itemNoComments: AssetInfo = {
|
||||
...fileItem,
|
||||
commentsCount: undefined,
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: itemNoComments })
|
||||
expect(screen.queryByTestId('file-card-comments-count')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -27,7 +27,15 @@ import { selectFileNameWithoutExtension } from '@/ui/lib/rename-utils'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import { useDraggable, useDroppable } from '@dnd-kit/react'
|
||||
import { Download, Edit, History, Layers, MoreHorizontal, Trash2 } from 'lucide-react'
|
||||
import {
|
||||
Download,
|
||||
Edit,
|
||||
History,
|
||||
Layers,
|
||||
MessageCircleMore,
|
||||
MoreHorizontal,
|
||||
Trash2,
|
||||
} from 'lucide-react'
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import type { DragState } from '../dnd-types'
|
||||
import FieldRenderer from '../field-renderer'
|
||||
@@ -323,7 +331,18 @@ export function FileCard({
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<FilePreview item={displayItem} showDuration />
|
||||
<>
|
||||
<FilePreview item={displayItem} showDuration />
|
||||
{typeof displayItem.commentsCount === 'number' && displayItem.commentsCount > 0 && (
|
||||
<span
|
||||
data-testid="file-card-comments-count"
|
||||
className="pointer-events-none absolute bottom-1 left-1 z-10 flex items-center gap-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
|
||||
>
|
||||
<MessageCircleMore className="h-3.5 w-3.5" />
|
||||
<span>{displayItem.commentsCount}</span>
|
||||
</span>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user