feat(video): add timestamp support to comments

This commit is contained in:
yiling
2026-05-27 11:17:56 +08:00
parent ad8f3931f7
commit c048ceef28
12 changed files with 158 additions and 20 deletions
+6 -4
View File
@@ -157,10 +157,11 @@ describe('file api', () => {
it('POST /files/:fileId/comments', async () => {
vi.mocked(assetService.createComment).mockResolvedValue({
id: 'comment-id',
assetId: 'test-id',
assetId: 'file-id',
message: 'hello',
annotations: null,
creator: { id: 'user1', name: 'user-name' },
second: null,
creator: { id: 'user-id', name: 'Test User' },
replies: [],
attachments: [],
mentions: [],
@@ -207,10 +208,11 @@ describe('file api', () => {
data: [
{
id: 'comment-id',
assetId: 'test-id',
assetId: 'file-id',
message: 'hello',
annotations: null,
creator: { id: 'user1', name: 'user-name' },
second: null,
creator: { id: 'user-id', name: 'Test User' },
replies: [],
attachments: [],
mentions: [],
+2
View File
@@ -189,6 +189,7 @@ export type CommentInfo = {
message: string | null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
annotations: any
second: number | null
creator: UserInfo
replies: CommentInfo[]
attachments: AttachmentInfo[]
@@ -204,6 +205,7 @@ export const commentInfoSchema: z.ZodType<CommentInfo> = z.object({
message: z.string().nullable(),
annotations: z.any(),
second: z.number().nullable(),
creator: userInfoSchema,
replies: z.array(z.lazy(() => commentInfoSchema)),
attachments: z.array(attachmentInfoSchema),
+22
View File
@@ -664,6 +664,28 @@ describe('AssetService', () => {
expect(list.data[0].replies.length).toBe(1)
})
it('can create and retrieve comments with video timestamp', async () => {
const { user, assets } = await setupBasicAssets()
const comment = await assetService.createComment({
assetId: assets.fileA1.id,
userId: user.id,
message: 'Interesting point at 5.5s',
second: 5.5,
attachmentIds: [],
})
expect(comment.second).toBe(5.5)
const retrieved = await assetService.getComment(comment.id)
expect(retrieved.second).toBe(5.5)
const list = await assetService.listComments(assets.fileA1.id, {
first: 10,
})
expect(list.data[0].second).toBe(5.5)
})
it('correctly handles AI comments with agent identity', async () => {
const { assets, project } = await setupBasicAssets()
+1
View File
@@ -1690,6 +1690,7 @@ export class AssetService {
assetId: c.assetId,
message: c.message,
annotations: c.annotation,
second: c.second,
creator,
replies,
attachments,
+18 -1
View File
@@ -11,12 +11,13 @@ import {
} from '@/ui/components/ui/dialog'
import { Separator } from '@/ui/components/ui/separator'
import { useQuery } from '@tanstack/react-query'
import { Download, File, Terminal } from 'lucide-react'
import { Download, File, Terminal, Clock } from 'lucide-react'
import React from 'react'
import { formatTimeAgo } from '@/ui/lib/time'
import Markdown from 'react-markdown'
import { Avatar, AvatarFallback } from '../ui/avatar'
import { Skeleton } from '../ui/skeleton'
import { formatTimestamp } from '../viewers/utils'
interface MessageCardProps {
teamId?: string
@@ -27,6 +28,8 @@ interface MessageCardProps {
getUser: (id: string) => UserInfo
onReply: (message: CommentInfo) => void
onViewAttachment: (attachment: AttachmentInfo) => void
onTimestampClick?: (seconds: number) => void
frameRate?: number
rootParentId?: string
}
@@ -48,6 +51,8 @@ export const MessageCard: React.FC<MessageCardProps> = ({
getUser,
onReply,
onViewAttachment,
onTimestampClick,
frameRate,
}) => {
const isRunning =
!!initialMessage.sessionId &&
@@ -153,6 +158,18 @@ export const MessageCard: React.FC<MessageCardProps> = ({
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<span className="font-semibold text-sm">{creator?.name || 'Unknown'}</span>
{message.second !== null && message.second !== undefined && frameRate && (
<button
onClick={(e) => {
e.stopPropagation()
onTimestampClick?.(message.second!)
}}
className="bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2 py-0.5 rounded-full text-[10px] font-mono font-bold flex items-center gap-1 border border-blue-200 dark:border-blue-800 shadow-sm hover:bg-blue-200 dark:hover:bg-blue-900/60 transition-colors"
>
<Clock className="w-2.5 h-2.5" />
{formatTimestamp(message.second, frameRate)}
</button>
)}
<span className="text-xs text-gray-400">
{new Date(message.createdAt!).toLocaleTimeString([], {
hour: '2-digit',
+33 -1
View File
@@ -8,6 +8,7 @@ import {
ArrowUp,
Brush,
ChevronDown,
Clock,
FileIcon,
Minus,
Paperclip,
@@ -24,6 +25,7 @@ import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
import { useAnnotationStore } from '@/ui/stores/annotation-store'
import type { Annotation } from '@/ui/types'
import { useMutation } from '@tanstack/react-query'
import { formatTimestamp } from '../viewers/utils'
type UploadingFile = {
id: string // A unique ID for the file, e.g., timestamp + name
@@ -40,6 +42,7 @@ interface ChatInputProps {
attachmentIds: string[],
annotations?: Annotation[],
replyToId?: string | null,
second?: number | null,
) => void
replyingTo?: CommentInfo | null
onCancelReply?: () => void
@@ -48,6 +51,8 @@ interface ChatInputProps {
initialText?: string
hideAnnotationControl?: boolean
disableMentions?: boolean
currentTime?: number
frameRate?: number
}
const PREDEFINED_COLORS = [
@@ -76,11 +81,14 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
initialText = '',
hideAnnotationControl = false,
disableMentions = false,
currentTime,
frameRate,
},
ref,
) => {
const [uploadingFiles, setUploadingFiles] = useState<UploadingFile[]>([])
const [viewingFile, setViewingFile] = useState<File | null>(null)
const [isTimestampEnabled, setIsTimestampEnabled] = useState(true)
// Annotation Store
const {
@@ -395,7 +403,13 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
// Allow sending if annotations exist, even if text is empty
if (!processedText && successfulAttachmentIds.length === 0 && annotations.length === 0) return
onSendMessage(processedText, successfulAttachmentIds, annotations, replyingTo?.id)
onSendMessage(
processedText,
successfulAttachmentIds,
annotations,
replyingTo?.id,
isTimestampEnabled ? currentTime : undefined,
)
// Reset editor
editorRef.current.innerHTML = ''
@@ -566,6 +580,15 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
</div>
)}
{isTimestampEnabled && currentTime !== undefined && frameRate !== undefined && (
<div className="px-4 pt-3 flex">
<div className="bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-3 py-1 rounded-full text-xs font-mono font-bold flex items-center gap-1.5 border border-blue-200 dark:border-blue-800 shadow-sm animate-in fade-in zoom-in duration-200">
<Clock className="w-3 h-3" />
{formatTimestamp(currentTime, frameRate)}
</div>
</div>
)}
<div className="w-full px-4 pt-1 pb-1">
<div
ref={editorRef}
@@ -679,6 +702,15 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
multiple
onChange={handleFileSelect}
/>
<Button
onClick={() => setIsTimestampEnabled(!isTimestampEnabled)}
variant={isTimestampEnabled ? 'secondary' : 'ghost'}
size="icon"
className={`p-2 rounded-full cursor-pointer ${isTimestampEnabled ? 'text-blue-600 bg-blue-50 dark:bg-blue-900/20' : ''}`}
title={isTimestampEnabled ? 'Disable Timestamp' : 'Enable Timestamp'}
>
<Clock className="w-4 h-4" />
</Button>
<Button
onClick={() => fileInputRef.current?.click()}
className="p-2 rounded-full cursor-pointer"
@@ -27,6 +27,7 @@ interface FileViewerRightSidebarProps {
publicFields?: MetadataFieldInfo[]
isPublic?: boolean
shareId?: string
currentTime?: number
}
export function FileViewerRightSidebar({
@@ -41,6 +42,7 @@ export function FileViewerRightSidebar({
publicFields,
isPublic = false,
shareId,
currentTime,
}: FileViewerRightSidebarProps) {
const { fields, setFields } = useFieldStore()
const queryClient = useQueryClient()
@@ -53,6 +55,7 @@ export function FileViewerRightSidebar({
attachmentIds: string[]
annotations?: Annotation[]
replyToId?: string | null
second?: number | null
} | null>(null)
const { data: apiFields } = useQuery({
@@ -139,12 +142,14 @@ export function FileViewerRightSidebar({
annotations,
replyToId,
guestUserId,
second,
}: {
text: string
attachmentIds: string[]
annotations?: Annotation[]
replyToId?: string | null
guestUserId?: string
second?: number | null
}) => {
if (isPublic) {
const password = localStorage.getItem(`share_pwd_${shareId}`) || ''
@@ -156,6 +161,7 @@ export function FileViewerRightSidebar({
attachmentIds: attachmentIds,
replyToId: replyToId ?? undefined,
annotations: annotations,
second: second ?? undefined,
},
},
{
@@ -175,6 +181,7 @@ export function FileViewerRightSidebar({
attachmentIds: attachmentIds,
replyToId: replyToId ?? undefined,
annotations: annotations,
second: second ?? undefined,
},
})
if (!res.ok) throw new Error('Failed to create comment')
@@ -225,6 +232,7 @@ export function FileViewerRightSidebar({
attachmentIds: string[],
annotations?: Annotation[],
replyToId?: string | null,
second?: number | null,
) => {
if (!file?.id) return
@@ -240,16 +248,16 @@ export function FileViewerRightSidebar({
console.log('[handleSendMessage] guestUserId from storage:', guestUserId)
if (!guestUserId) {
console.log('[handleSendMessage] no guest ID, opening popup')
setPendingComment({ text, attachmentIds, annotations, replyToId })
setPendingComment({ text, attachmentIds, annotations, replyToId, second })
setIsGuestPopupOpen(true)
return
}
createComment({ text, attachmentIds, annotations, replyToId, guestUserId })
createComment({ text, attachmentIds, annotations, replyToId, guestUserId, second })
} else {
createComment({ text, attachmentIds, annotations, replyToId })
createComment({ text, attachmentIds, annotations, replyToId, second })
}
} else {
createComment({ text, attachmentIds, annotations, replyToId })
createComment({ text, attachmentIds, annotations, replyToId, second })
}
setReplyingTo(null)
}
@@ -340,6 +348,8 @@ export function FileViewerRightSidebar({
onReply={setReplyingTo}
onViewAttachment={setViewingAttachment}
hasReplies={!!comment.replies?.length}
frameRate={file?.media?.metadata?.frameRate || 30}
onTimestampClick={(sec) => onCommentSelect?.({ ...comment, second: sec })}
/>
{comment.replies?.map((reply, index) => (
<MessageCard
@@ -352,6 +362,8 @@ export function FileViewerRightSidebar({
onViewAttachment={setViewingAttachment}
hasReplies={false}
isLastReply={index === (comment.replies?.length ?? 0) - 1}
frameRate={file?.media?.metadata?.frameRate || 30}
onTimestampClick={(sec) => onCommentSelect?.({ ...reply, second: sec })}
/>
))}
</div>
@@ -375,6 +387,8 @@ export function FileViewerRightSidebar({
bots={enabledBots}
hideAnnotationControl={hideAnnotationControl}
disableMentions={isPublic}
currentTime={currentTime}
frameRate={file?.media?.metadata?.frameRate || 30}
/>
</GuestIdentityPopup>
</div>
+10 -1
View File
@@ -16,10 +16,18 @@ type FileViewerProps = {
videoRef?: RefObject<Player | null>
onPlay?: () => void
onPause?: () => void
onTimeUpdate?: (time: number) => void
annotations?: Annotation[]
}
export function FileViewer({ file, videoRef, onPlay, onPause, annotations }: FileViewerProps) {
export function FileViewer({
file,
videoRef,
onPlay,
onPause,
onTimeUpdate,
annotations,
}: FileViewerProps) {
const { width: screenWidth } = useScreenSize()
const isImage = file.mediaType?.startsWith('image/')
const isVideo = file.mediaType?.startsWith('video/')
@@ -186,6 +194,7 @@ export function FileViewer({ file, videoRef, onPlay, onPause, annotations }: Fil
playerRef={videoRef}
onPlay={onPlay}
onPause={onPause}
onTimeUpdate={onTimeUpdate}
annotations={displayAnnotations}
/>
)}
+13 -2
View File
@@ -89,6 +89,7 @@ export function PublicShareManager({
const [isRightSidebarCollapsed] = useState(false)
const videoRef = useRef<Player | null>(null)
const [annotations, setAnnotations] = useState<Annotation[]>([])
const [currentTime, setCurrentTime] = useState(0)
const {
data: foldersData,
@@ -257,11 +258,19 @@ export function PublicShareManager({
const newAnnotations = comment.annotations as Annotation[]
if (newAnnotations && newAnnotations.length > 0) {
setAnnotations(newAnnotations)
} else {
setAnnotations([])
}
if (comment.second !== null && comment.second !== undefined) {
if (videoRef.current) {
videoRef.current.currentTime(comment.second)
videoRef.current.pause()
}
} else if (newAnnotations && newAnnotations.length > 0) {
if (videoRef.current) {
videoRef.current.pause()
}
} else {
setAnnotations([])
}
}
@@ -373,6 +382,7 @@ export function PublicShareManager({
file={viewingFileData}
videoRef={videoRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
annotations={annotations}
/>
)}
@@ -430,6 +440,7 @@ export function PublicShareManager({
onCommentSelect={handleCommentSelect}
isPublic={true}
shareId={shareInfo.id}
currentTime={currentTime}
/>
</div>
</>
+10
View File
@@ -10,6 +10,16 @@ export const formatTime = (seconds: number): string => {
return `${mm}:${ss}`
}
export const formatTimestamp = (seconds: number, fps: number): string => {
if (isNaN(seconds)) return '00:00:00:00'
const hh = Math.floor(seconds / 3600)
const mm = Math.floor((seconds % 3600) / 60)
const ss = Math.floor(seconds % 60)
const ff = Math.floor((seconds % 1) * fps)
return `${hh.toString().padStart(2, '0')}:${mm.toString().padStart(2, '0')}:${ss.toString().padStart(2, '0')}:${ff.toString().padStart(2, '0')}`
}
export const formatFrame = (seconds: number, fps: number): number => {
if (isNaN(seconds) || !fps) return 0
return Math.floor(seconds * fps)
@@ -51,6 +51,7 @@ interface VideoPlayerProps {
playerRef?: React.RefObject<Player | null>
onPlay?: () => void
onPause?: () => void
onTimeUpdate?: (time: number) => void
annotations?: Annotation[]
}
@@ -299,6 +300,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
playerRef: passedPlayerRef,
onPlay,
onPause,
onTimeUpdate,
annotations,
}) => {
const localPlayerRef = useRef<Player | null>(null)
@@ -494,6 +496,10 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
duration: duration || prev.duration,
progress: duration > 0 ? (current / duration) * 100 : 0,
}))
if (onTimeUpdate) {
onTimeUpdate(current)
}
})
player.on('volumechange', () => {
@@ -34,6 +34,7 @@ function FileViewPage() {
const [leftSidebarWidth, setLeftSidebarWidth] = useState(280)
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
const [annotations, setAnnotations] = useState<Annotation[]>([])
const [currentTime, setCurrentTime] = useState(0)
const $patchMetadata = client.api.files[':fileId'].metadata.$patch
const { mutate: patchMetadata } = useMutation<
InferResponseType<typeof $patchMetadata>,
@@ -197,11 +198,19 @@ function FileViewPage() {
const newAnnotations = comment.annotations as Annotation[]
if (newAnnotations && newAnnotations.length > 0) {
setAnnotations(newAnnotations)
} else {
setAnnotations([])
}
if (comment.second !== null && comment.second !== undefined) {
if (videoRef.current) {
videoRef.current.currentTime(comment.second)
videoRef.current.pause()
}
} else if (newAnnotations && newAnnotations.length > 0) {
if (videoRef.current) {
videoRef.current.pause()
}
} else {
setAnnotations([])
}
}
@@ -244,29 +253,32 @@ function FileViewPage() {
file={fileData as unknown as AssetInfo}
videoRef={videoRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
annotations={annotations}
/>
</div>
{!isRightSidebarCollapsed && (
</div>
{!isRightSidebarCollapsed && (
<>
<ResizeHandle
onResize={(delta) => {
setRightSidebarWidth((prev) => Math.max(240, Math.min(600, prev - delta)))
setRightSidebarWidth((prev) => Math.max(300, Math.min(600, prev - delta)))
}}
className="hidden md:block"
/>
<div style={{ width: rightSidebarWidth }} className="flex-shrink-0">
<FileViewerRightSidebar
teamId={teamId!}
teamId={teamId}
projectId={projectId}
file={fileData as unknown as AssetInfo}
onSaveField={handleSaveField}
members={members}
onCommentSelect={handleCommentSelect}
currentTime={currentTime}
/>
</div>
</>
)}
)}
</div>
</div>
)