mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 04:44:28 +00:00
refactor(monorepo): decouple api and webui into workspaces packages
This commit is contained in:
@@ -0,0 +1,577 @@
|
||||
import type { AttachmentInfo, CommentInfo } from '@shumai/dtos'
|
||||
import type { UserInfo } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/ui/components/ui/dialog'
|
||||
import { Separator } from '@/ui/components/ui/separator'
|
||||
import { formatTimeAgo } from '@/ui/lib/time'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Download, File, Terminal } from 'lucide-react'
|
||||
import React from 'react'
|
||||
import Markdown from 'react-markdown'
|
||||
import { Avatar, AvatarFallback } from '../ui/avatar'
|
||||
import { DrawAnnotation } from '../ui/icons'
|
||||
import { Skeleton } from '../ui/skeleton'
|
||||
import { formatTimestamp } from '../viewers/utils'
|
||||
|
||||
interface MessageCardProps {
|
||||
teamId?: string
|
||||
message: CommentInfo
|
||||
isReply?: boolean
|
||||
hasReplies?: boolean
|
||||
isLastReply?: boolean
|
||||
getUser: (id: string) => UserInfo
|
||||
onReply: (message: CommentInfo) => void
|
||||
onViewAttachment: (attachment: AttachmentInfo) => void
|
||||
isSelected?: boolean
|
||||
onSelect?: () => void
|
||||
frameRate?: number
|
||||
rootParentId?: string
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
const AI_PLACEHOLDERS: Record<string, string> = {
|
||||
__CHAT__: 'Generating...',
|
||||
__AUTOFILL__: 'Autofilling metadata...',
|
||||
__EMBEDDING__: 'Generating embeddings...',
|
||||
__RUNNING__: 'Generating...',
|
||||
}
|
||||
|
||||
export const MessageCard: React.FC<MessageCardProps> = ({
|
||||
teamId,
|
||||
message: initialMessage,
|
||||
isReply,
|
||||
hasReplies,
|
||||
isLastReply,
|
||||
getUser,
|
||||
onReply,
|
||||
onViewAttachment,
|
||||
isSelected,
|
||||
onSelect,
|
||||
frameRate,
|
||||
}) => {
|
||||
const message = initialMessage
|
||||
const hasDrawInfo =
|
||||
!!message.annotations && Array.isArray(message.annotations) && message.annotations.length > 0
|
||||
const creator = message.creator
|
||||
const showSkeleton =
|
||||
!!message.sessionId && !!message.message && message.message in AI_PLACEHOLDERS
|
||||
const loadingText = (message.message && AI_PLACEHOLDERS[message.message]) || 'Generating...'
|
||||
|
||||
const [isLogsOpen, setIsLogsOpen] = React.useState(false)
|
||||
|
||||
const { data: me } = useQuery({
|
||||
queryKey: ['teams', teamId, 'me'],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.teams[':teamId'].me.$get({
|
||||
param: { teamId: teamId! },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch current member info')
|
||||
return await res.json()
|
||||
},
|
||||
enabled: !!teamId,
|
||||
staleTime: 300000,
|
||||
})
|
||||
|
||||
const isAdmin = me?.role === 'owner'
|
||||
|
||||
const { data: logs, isLoading: isLogsLoading } = useQuery({
|
||||
queryKey: ['agent-sessions', message.sessionId, 'entries'],
|
||||
queryFn: async () => {
|
||||
const res = await client.api['agent-sessions'][':sessionId'].entries.$get({
|
||||
param: { sessionId: message.sessionId! },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch session logs')
|
||||
return await res.json()
|
||||
},
|
||||
enabled: isLogsOpen && !!message.sessionId,
|
||||
})
|
||||
|
||||
const renderFormattedMessage = (text: string) => {
|
||||
const parts = text.split(/(<@[a-zA-Z0-9_-]+>)/g)
|
||||
return parts.map((part, index) => {
|
||||
if (part.match(/^<@[a-zA-Z0-9_-]+>$/)) {
|
||||
const userId = part.slice(2, -1)
|
||||
const mentionedUser = message.mentions?.find((m) => m.id === userId) || getUser(userId)
|
||||
return (
|
||||
<span key={index} className="text-primary font-medium hover:underline cursor-pointer">
|
||||
@{mentionedUser?.name || 'Unknown'}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
return <span key={index}>{part}</span>
|
||||
})
|
||||
}
|
||||
|
||||
const preprocessMarkdown = (text: string): string => {
|
||||
return text.replace(/<@([a-zA-Z0-9_-]+)>/g, '@$1').replace(/^\[([^\]]+)\]:/gm, '\\[$1\\]:')
|
||||
}
|
||||
|
||||
const handleReply = () => {
|
||||
onReply(message)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
onClick={onSelect}
|
||||
className={`relative flex gap-4 ${isReply ? 'mt-[-1px]' : 'mt-3'} mr-3 group p-3 py-4 border transition-all duration-200 cursor-pointer ${
|
||||
isReply
|
||||
? isLastReply
|
||||
? 'rounded-b-xl rounded-t-none'
|
||||
: 'rounded-none'
|
||||
: hasReplies
|
||||
? 'rounded-t-xl rounded-b-none'
|
||||
: 'rounded-xl'
|
||||
} ${
|
||||
isSelected
|
||||
? 'z-20 border-blue-500/40 dark:border-blue-400/40 bg-blue-500/10 dark:bg-blue-400/10 shadow-sm'
|
||||
: 'z-10 border-transparent bg-foreground/2 dark:bg-foreground/10 hover:bg-foreground/4 dark:hover:bg-foreground/15'
|
||||
}`}
|
||||
>
|
||||
{/* Left Column: Avatar & Thread Lines */}
|
||||
<div className="flex flex-col items-center shrink-0 w-8 relative">
|
||||
{/* Thread line above avatar (for replies) */}
|
||||
{isReply && (
|
||||
<div className="absolute top-[-1rem] h-8 w-0.5 bg-foreground/10 left-1/2 -translate-x-1/2 z-0" />
|
||||
)}
|
||||
|
||||
{/* Thread line under avatar (for parent comments with replies, or intermediate replies) */}
|
||||
{((hasReplies && !isReply) || (isReply && !isLastReply)) && (
|
||||
<div className="absolute top-4 bottom-[-1rem] w-0.5 bg-foreground/10 left-1/2 -translate-x-1/2 z-0" />
|
||||
)}
|
||||
|
||||
<Avatar className="z-10 relative">
|
||||
<AvatarFallback className="bg-rose-400 text-black">
|
||||
{creator?.name?.[0]?.toUpperCase() || 'U'}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<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>
|
||||
<span className="text-xs text-gray-400">
|
||||
{new Date(message.createdAt!).toLocaleTimeString([], {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flow-root mt-1">
|
||||
{message.second !== null && message.second !== undefined && frameRate && (
|
||||
<div className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-1 mt-[2px] mr-2 rounded-sm text-xs leading-none font-mono font-bold flex items-center gap-1">
|
||||
{formatTimestamp(message.second, frameRate)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{hasDrawInfo && (
|
||||
<div
|
||||
className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-1 py-1 mt-[2px] mr-2 rounded-sm text-xs leading-none font-bold flex items-center gap-1"
|
||||
title="Contains drawing annotations"
|
||||
>
|
||||
<DrawAnnotation className="w-3.5 h-3.5" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showSkeleton ? (
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-4 w-3/4" />
|
||||
<Skeleton className="h-4 w-1/2" />
|
||||
<span className="text-xs text-muted-foreground animate-pulse">{loadingText}</span>
|
||||
</div>
|
||||
) : message.sessionId ? (
|
||||
<div className="text-sm leading-relaxed prose prose-sm dark:prose-invert max-w-none break-words">
|
||||
<Markdown>{preprocessMarkdown(message.message!)}</Markdown>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-sm leading-relaxed whitespace-pre-wrap break-words text-wrap break-all">
|
||||
{renderFormattedMessage(message.message!)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Attachments */}
|
||||
{message.attachments && message.attachments.length > 0 && (
|
||||
<div className="flex flex-wrap gap-2 mt-3">
|
||||
{message.attachments.map((att) => {
|
||||
const isImage = att.mediaType?.startsWith('image/')
|
||||
const name = att.url?.split('/').pop()?.split('?')[0] || 'file'
|
||||
return (
|
||||
<div
|
||||
key={att.id}
|
||||
className="relative group rounded-lg overflow-hidden border bg-white shadow-sm hover:shadow-md transition-shadow w-48 h-32"
|
||||
onClick={() => isImage && onViewAttachment(att)}
|
||||
>
|
||||
{isImage ? (
|
||||
<img
|
||||
src={att.url}
|
||||
alt={name}
|
||||
className="w-full h-full object-cover cursor-pointer"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-full h-full bg-gray-100 flex flex-col items-center justify-center p-2 text-center">
|
||||
<File className="w-8 h-8 text-gray-500 mb-2" />
|
||||
<span className="text-xs font-medium text-gray-700 break-all line-clamp-2">
|
||||
{name}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
<a
|
||||
href={att.url}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
download={name}
|
||||
className="absolute right-2 bottom-2 bg-black/60 text-white hover:bg-black/80 p-1.5 rounded-full opacity-0 group-hover:opacity-100 transition-opacity"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<Download className="w-4 h-4" />
|
||||
</a>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Agent Footer */}
|
||||
{message.sessionId && (
|
||||
<div className="mt-3 pt-2 border-t border-foreground/5 flex items-center justify-between text-xs text-muted-foreground/60">
|
||||
<span className="flex items-center gap-1.5 font-medium text-muted-foreground/80">
|
||||
Created by Agent
|
||||
</span>
|
||||
{isAdmin && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="xs"
|
||||
onClick={() => setIsLogsOpen(true)}
|
||||
className="cursor-pointer border-violet-500/20 text-violet-600 dark:text-violet-400 hover:bg-violet-500/10 hover:text-violet-700 hover:border-violet-500/30 font-semibold"
|
||||
>
|
||||
Logs
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
<div className="mt-2 opacity-0 group-hover:opacity-100 transition-opacity duration-200 flex items-center gap-4">
|
||||
<button
|
||||
onClick={handleReply}
|
||||
className="text-xs font-medium text-gray-500 hover:text-blue-600 flex items-center gap-1 cursor-pointer"
|
||||
>
|
||||
Reply
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Logs Dialog */}
|
||||
<Dialog open={isLogsOpen} onOpenChange={setIsLogsOpen}>
|
||||
<DialogContent className="sm:max-w-2xl max-h-[80vh] flex flex-col p-6 overflow-hidden bg-background/95 backdrop-blur-md border border-foreground/15 shadow-2xl">
|
||||
<DialogHeader className="pb-4 border-b border-foreground/15">
|
||||
<DialogTitle className="flex items-center gap-2 text-xl font-bold">
|
||||
<Terminal className="w-5 h-5 text-violet-500" />
|
||||
Agent Session Logs
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
Step-by-step execution trace of the agent's background tasks and tool calls.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex-1 overflow-y-auto pr-1 py-4 space-y-4 min-h-0 select-text">
|
||||
{isLogsLoading ? (
|
||||
<div className="space-y-4">
|
||||
<div className="flex gap-4">
|
||||
<Skeleton className="h-5 w-5 rounded-full shrink-0" />
|
||||
<div className="flex-1 space-y-2">
|
||||
<Skeleton className="h-4 w-1/4" />
|
||||
<Skeleton className="h-20 w-full" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-4">
|
||||
<Skeleton className="h-5 w-5 rounded-full shrink-0" />
|
||||
<div className="flex-1 space-y-2">
|
||||
<Skeleton className="h-4 w-1/4" />
|
||||
<Skeleton className="h-12 w-full" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : logs && logs.length > 0 ? (
|
||||
<div className="flex flex-col">
|
||||
{logs.map((entry) => {
|
||||
const piEntry = entry.entry as Record<string, unknown> | null
|
||||
const timestampStr =
|
||||
piEntry?.timestamp || (entry as Record<string, unknown>).timestamp
|
||||
let timestamp = ''
|
||||
if (typeof timestampStr === 'string' || typeof timestampStr === 'number') {
|
||||
const dateObj = new Date(timestampStr)
|
||||
if (!isNaN(dateObj.getTime())) {
|
||||
timestamp = formatTimeAgo(dateObj.toISOString())
|
||||
}
|
||||
}
|
||||
|
||||
if (piEntry && typeof piEntry === 'object') {
|
||||
if (piEntry.type === 'message' && piEntry.message) {
|
||||
const msg = piEntry.message as Record<string, unknown>
|
||||
let badgeColor =
|
||||
'bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-200'
|
||||
let roleName: string = String(msg.role || '')
|
||||
|
||||
if (msg.role === 'user') {
|
||||
badgeColor =
|
||||
'bg-slate-100 text-slate-800 dark:bg-slate-900/30 dark:text-slate-300'
|
||||
roleName = 'User'
|
||||
} else if (msg.role === 'assistant') {
|
||||
badgeColor =
|
||||
'bg-violet-100 text-violet-800 dark:bg-violet-900/30 dark:text-violet-300'
|
||||
roleName = 'Agent'
|
||||
} else if (msg.role === 'toolResult') {
|
||||
badgeColor = msg.isError
|
||||
? 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300'
|
||||
: 'bg-emerald-100 text-emerald-800 dark:bg-emerald-900/30 dark:text-emerald-300'
|
||||
roleName = `Tool Result: ${String(msg.toolName || 'Unknown')}`
|
||||
} else if (msg.role === 'thought') {
|
||||
badgeColor =
|
||||
'bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300'
|
||||
roleName = 'Thinking'
|
||||
}
|
||||
|
||||
const renderToolCallArguments = (args: unknown) => {
|
||||
let parsedArgs: unknown = args
|
||||
if (typeof args === 'string') {
|
||||
try {
|
||||
parsedArgs = JSON.parse(args)
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
parsedArgs &&
|
||||
typeof parsedArgs === 'object' &&
|
||||
!Array.isArray(parsedArgs)
|
||||
) {
|
||||
const obj = parsedArgs as Record<string, unknown>
|
||||
return (
|
||||
<div className="mt-1 space-y-1.5 pt-3">
|
||||
{Object.entries(obj).map(([key, value]) => {
|
||||
let valueStr = ''
|
||||
if (value && typeof value === 'object') {
|
||||
valueStr = JSON.stringify(value, null, 2)
|
||||
} else {
|
||||
valueStr = String(value)
|
||||
}
|
||||
return (
|
||||
<div key={key} className="text-xs leading-relaxed break-words">
|
||||
<span className="font-semibold text-violet-600 dark:text-violet-400">
|
||||
{key}:
|
||||
</span>{' '}
|
||||
<span className="text-foreground/90 font-mono whitespace-pre-wrap">
|
||||
{valueStr}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="text-xs font-mono whitespace-pre-wrap">
|
||||
{String(args)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const renderMessageContent = () => {
|
||||
const content = msg.content
|
||||
if (!content) return null
|
||||
|
||||
if (typeof content === 'string') {
|
||||
if (!content.trim()) return null
|
||||
return (
|
||||
<div className="text-xs text-foreground/80 dark:text-foreground/95 leading-relaxed prose prose-sm dark:prose-invert max-w-none break-words">
|
||||
<Markdown>{preprocessMarkdown(content)}</Markdown>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (Array.isArray(content)) {
|
||||
const renderedBlocks: React.ReactNode[] = []
|
||||
|
||||
content.forEach((item, idx) => {
|
||||
if (!item) return
|
||||
|
||||
if (typeof item === 'string') {
|
||||
if (!item.trim()) return
|
||||
renderedBlocks.push(
|
||||
<div
|
||||
key={`str-${idx}`}
|
||||
className="text-xs text-foreground/80 dark:text-foreground/95 leading-relaxed prose prose-sm dark:prose-invert max-w-none break-words"
|
||||
>
|
||||
<Markdown>{preprocessMarkdown(item)}</Markdown>
|
||||
</div>,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if (typeof item === 'object') {
|
||||
const itemObj = item as Record<string, unknown>
|
||||
const type = itemObj.type
|
||||
if (type === 'text') {
|
||||
const text = itemObj.text
|
||||
if (typeof text !== 'string' || !text.trim()) return
|
||||
renderedBlocks.push(
|
||||
<div
|
||||
key={`txt-${idx}`}
|
||||
className="text-xs text-foreground/80 dark:text-foreground/95 leading-relaxed prose prose-sm dark:prose-invert max-w-none break-words"
|
||||
>
|
||||
<Markdown>{preprocessMarkdown(text)}</Markdown>
|
||||
</div>,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if (type === 'toolCall') {
|
||||
const toolName = String(
|
||||
itemObj.name || itemObj.toolName || 'Unknown',
|
||||
)
|
||||
const toolArgs = itemObj.arguments || itemObj.args
|
||||
renderedBlocks.push(
|
||||
<div key={`tool-${idx}`} className="text-xs space-y-2 py-1">
|
||||
<div className="font-semibold text-violet-600 dark:text-violet-400 flex items-center gap-1.5">
|
||||
Calling Tool:{' '}
|
||||
<span className="font-mono bg-violet-500/10 px-1 py-0.5 rounded text-[11px]">
|
||||
{toolName}
|
||||
</span>
|
||||
</div>
|
||||
{!!toolArgs && (
|
||||
<div className="space-y-1 pt-3">
|
||||
<div className="text-[10px] text-muted-foreground/60 uppercase font-bold tracking-wider">
|
||||
Arguments
|
||||
</div>
|
||||
{renderToolCallArguments(toolArgs)}
|
||||
</div>
|
||||
)}
|
||||
</div>,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if (type === 'image') {
|
||||
renderedBlocks.push(
|
||||
<div
|
||||
key={`img-${idx}`}
|
||||
className="text-xs text-muted-foreground italic flex items-center gap-1.5 py-1"
|
||||
>
|
||||
[Image Object]
|
||||
</div>,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Fallback for other object types
|
||||
const stringified = JSON.stringify(item, null, 2)
|
||||
if (stringified === '{}') return
|
||||
renderedBlocks.push(
|
||||
<pre
|
||||
key={`other-${idx}`}
|
||||
className="text-xs text-foreground/80 font-mono whitespace-pre-wrap break-words bg-muted/20 p-2 rounded"
|
||||
>
|
||||
{stringified}
|
||||
</pre>,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
if (renderedBlocks.length === 0) return null
|
||||
|
||||
return (
|
||||
<div className="space-y-3 w-full">
|
||||
{renderedBlocks.map((block, index) => (
|
||||
<React.Fragment key={index}>
|
||||
{index > 0 && <Separator className="my-3 opacity-50" />}
|
||||
{block}
|
||||
</React.Fragment>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<div key={entry.id} className="relative pl-6 pb-6 last:pb-0">
|
||||
{/* Timeline Line */}
|
||||
<div className="absolute left-[9px] top-2 bottom-0 w-0.5 bg-foreground/10 last:hidden" />
|
||||
|
||||
{/* Timeline Node */}
|
||||
<div
|
||||
className={`absolute left-0 top-1.5 h-5 w-5 rounded-full border-4 border-background flex items-center justify-center ${
|
||||
msg.role === 'user'
|
||||
? 'bg-slate-500'
|
||||
: msg.role === 'assistant'
|
||||
? 'bg-violet-500'
|
||||
: msg.role === 'toolResult'
|
||||
? msg.isError
|
||||
? 'bg-red-500'
|
||||
: 'bg-emerald-500'
|
||||
: 'bg-amber-500'
|
||||
}`}
|
||||
/>
|
||||
|
||||
<div className="flex flex-col gap-1.5 bg-muted/30 dark:bg-muted/10 p-3 rounded-lg border border-foreground/5 backdrop-blur-xs w-full">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<span
|
||||
className={`text-[10px] px-2 py-0.5 rounded-full font-semibold uppercase tracking-wider ${badgeColor}`}
|
||||
>
|
||||
{roleName}
|
||||
</span>
|
||||
<span className="text-[10px] text-muted-foreground/60">
|
||||
{timestamp}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{renderMessageContent()}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for non-message entries or arbitrary entries
|
||||
return (
|
||||
<div key={entry.id} className="relative pl-6 pb-6 last:pb-0">
|
||||
<div className="absolute left-[9px] top-2 bottom-0 w-0.5 bg-foreground/10 last:hidden" />
|
||||
<div className="absolute left-0 top-1.5 h-5 w-5 rounded-full border-4 border-background bg-gray-400" />
|
||||
<div className="bg-muted/30 p-3 rounded-lg border border-foreground/5">
|
||||
<div className="text-[10px] text-muted-foreground/60">{timestamp}</div>
|
||||
<pre className="text-xs text-foreground/80 font-mono whitespace-pre-wrap break-words">
|
||||
{JSON.stringify(entry.entry, null, 2)}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col items-center justify-center py-12 text-center text-muted-foreground">
|
||||
<Terminal className="w-8 h-8 text-muted-foreground/40 mb-2 animate-pulse" />
|
||||
<p className="text-sm font-medium">No logs found for this session.</p>
|
||||
<p className="text-xs text-muted-foreground/60 mt-1">
|
||||
The session might have been initialized but has not produced any entries yet.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,784 @@
|
||||
import type { CommentInfo, PostAttachmentRequest, PostAttachmentResponse } from '@shumai/dtos'
|
||||
import type { BotInfo } from '@shumai/dtos'
|
||||
import type { UserInfo } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import type { Annotation } from '@/ui/types'
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import {
|
||||
ArrowLeft,
|
||||
ArrowUp,
|
||||
Brush,
|
||||
ChevronDown,
|
||||
Clock,
|
||||
FileIcon,
|
||||
Minus,
|
||||
Paperclip,
|
||||
Redo,
|
||||
Square,
|
||||
Undo,
|
||||
XIcon,
|
||||
} from 'lucide-react'
|
||||
import React, { useCallback, useEffect, useRef, useState, type ChangeEvent } from 'react'
|
||||
import { Button } from '../ui/button'
|
||||
import { DrawAnnotation } from '../ui/icons'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
|
||||
import { ProgressCircle } from '../ui/progress-circle'
|
||||
import { formatTimestamp } from '../viewers/utils'
|
||||
|
||||
type UploadingFile = {
|
||||
id: string // A unique ID for the file, e.g., timestamp + name
|
||||
file: File
|
||||
progress: number
|
||||
attachmentId?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
interface ChatInputProps {
|
||||
projectId: string
|
||||
onSendMessage: (
|
||||
text: string,
|
||||
attachmentIds: string[],
|
||||
annotations?: Annotation[],
|
||||
replyToId?: string | null,
|
||||
second?: number | null,
|
||||
) => void
|
||||
replyingTo?: CommentInfo | null
|
||||
onCancelReply?: () => void
|
||||
users?: UserInfo[]
|
||||
bots?: BotInfo[]
|
||||
initialText?: string
|
||||
hideAnnotationControl?: boolean
|
||||
disableMentions?: boolean
|
||||
currentTime?: number
|
||||
frameRate?: number
|
||||
onTyping?: () => void
|
||||
}
|
||||
|
||||
const PREDEFINED_COLORS = [
|
||||
'#ef4444', // Red
|
||||
'#f97316', // Orange
|
||||
'#eab308', // Yellow
|
||||
'#22c55e', // Green
|
||||
'#3b82f6', // Blue
|
||||
'#a855f7', // Purple
|
||||
'#ec4899', // Pink
|
||||
'#000000', // Black
|
||||
'#ffffff', // White
|
||||
]
|
||||
|
||||
type MentionEntity = { type: 'user'; data: UserInfo } | { type: 'bot'; data: BotInfo }
|
||||
|
||||
export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
(
|
||||
{
|
||||
projectId,
|
||||
onSendMessage,
|
||||
replyingTo,
|
||||
onCancelReply,
|
||||
users = [],
|
||||
bots = [],
|
||||
initialText = '',
|
||||
hideAnnotationControl = false,
|
||||
disableMentions = false,
|
||||
currentTime,
|
||||
frameRate,
|
||||
onTyping,
|
||||
},
|
||||
ref,
|
||||
) => {
|
||||
const [uploadingFiles, setUploadingFiles] = useState<UploadingFile[]>([])
|
||||
const [viewingFile, setViewingFile] = useState<File | null>(null)
|
||||
const [isTimestampEnabled, setIsTimestampEnabled] = useState(true)
|
||||
|
||||
// Annotation Store
|
||||
const {
|
||||
isDrawing,
|
||||
setIsDrawing,
|
||||
currentTool,
|
||||
setTool,
|
||||
currentColor,
|
||||
setColor,
|
||||
undo,
|
||||
redo,
|
||||
history,
|
||||
historyIndex,
|
||||
annotations,
|
||||
reset: resetAnnotations,
|
||||
} = useAnnotationStore()
|
||||
|
||||
const [showMentionList, setShowMentionList] = useState(false)
|
||||
const [mentionQuery, setMentionQuery] = useState('')
|
||||
const [highlightedIndex, setHighlightedIndex] = useState(0)
|
||||
const [collapsedSections, setCollapsedSections] = useState({ bots: false, users: false })
|
||||
const [hasContent, setHasContent] = useState(false)
|
||||
|
||||
const editorRef = useRef<HTMLDivElement>(null)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
const mentionRangeRef = useRef<Range | null>(null)
|
||||
|
||||
const { mutateAsync: uploadAttachment } = useMutation({
|
||||
mutationFn: async ({
|
||||
projectId,
|
||||
data,
|
||||
}: {
|
||||
projectId: string
|
||||
data: PostAttachmentRequest
|
||||
}) => {
|
||||
const res = await client.api.projects[':projectId'].attachments.$post({
|
||||
param: { projectId: projectId },
|
||||
json: data,
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to create attachment')
|
||||
return (await res.json()) as PostAttachmentResponse
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (initialText && editorRef.current) {
|
||||
editorRef.current.innerText = initialText
|
||||
setHasContent(!!initialText.trim())
|
||||
// Focus and move cursor to end
|
||||
editorRef.current.focus()
|
||||
const range = document.createRange()
|
||||
const sel = window.getSelection()
|
||||
range.selectNodeContents(editorRef.current)
|
||||
range.collapse(false)
|
||||
sel?.removeAllRanges()
|
||||
sel?.addRange(range)
|
||||
}
|
||||
}, [initialText])
|
||||
|
||||
// Focus editor when replyingTo becomes truthy
|
||||
useEffect(() => {
|
||||
if (replyingTo && editorRef.current) {
|
||||
editorRef.current.focus()
|
||||
// Focus and move cursor to end
|
||||
const range = document.createRange()
|
||||
const sel = window.getSelection()
|
||||
range.selectNodeContents(editorRef.current)
|
||||
range.collapse(false)
|
||||
sel?.removeAllRanges()
|
||||
sel?.addRange(range)
|
||||
}
|
||||
}, [replyingTo])
|
||||
|
||||
// Clean up drawing mode on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
setIsDrawing(false)
|
||||
}
|
||||
}, [setIsDrawing])
|
||||
|
||||
const filteredAgents = [
|
||||
...users
|
||||
.filter(
|
||||
(u) => u.type === 'agent' && u.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()),
|
||||
)
|
||||
.map((u) => ({ type: 'user' as const, data: u })),
|
||||
...bots
|
||||
.filter((b) => b.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()))
|
||||
.map((b) => ({ type: 'bot' as const, data: b })),
|
||||
]
|
||||
|
||||
const filteredHumans = users
|
||||
.filter(
|
||||
(u) => u.type !== 'agent' && u.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()),
|
||||
)
|
||||
.map((u) => ({ type: 'user' as const, data: u }))
|
||||
|
||||
const filteredEntities: MentionEntity[] = [
|
||||
...(collapsedSections.bots ? [] : filteredAgents),
|
||||
...(collapsedSections.users ? [] : filteredHumans),
|
||||
]
|
||||
|
||||
useEffect(() => {
|
||||
setHighlightedIndex(0)
|
||||
}, [mentionQuery, showMentionList, collapsedSections])
|
||||
|
||||
const handleInput = () => {
|
||||
if (!editorRef.current) return
|
||||
setHasContent(!!editorRef.current.innerText.trim())
|
||||
onTyping?.()
|
||||
|
||||
if (disableMentions) {
|
||||
setShowMentionList(false)
|
||||
mentionRangeRef.current = null
|
||||
return
|
||||
}
|
||||
|
||||
const sel = window.getSelection()
|
||||
if (!sel || sel.rangeCount === 0) return
|
||||
|
||||
const range = sel.getRangeAt(0)
|
||||
const node = range.startContainer
|
||||
const offset = range.startOffset
|
||||
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
const textContent = node.textContent || ''
|
||||
const textBeforeCursor = textContent.slice(0, offset)
|
||||
const lastAtPos = textBeforeCursor.lastIndexOf('@')
|
||||
|
||||
if (lastAtPos !== -1) {
|
||||
const isStartOfLine = lastAtPos === 0
|
||||
const charBeforeAt = textBeforeCursor[lastAtPos - 1]
|
||||
const hasSpaceBefore = isStartOfLine || /\s/.test(charBeforeAt)
|
||||
|
||||
if (hasSpaceBefore) {
|
||||
const query = textBeforeCursor.slice(lastAtPos + 1)
|
||||
if (!/\s/.test(query)) {
|
||||
setMentionQuery(query)
|
||||
setShowMentionList(true)
|
||||
|
||||
// Save the range for later insertion
|
||||
const mentionRange = document.createRange()
|
||||
mentionRange.setStart(node, lastAtPos)
|
||||
mentionRange.setEnd(node, offset)
|
||||
mentionRangeRef.current = mentionRange
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
setShowMentionList(false)
|
||||
mentionRangeRef.current = null
|
||||
}
|
||||
|
||||
const handleSelectEntity = (entity: MentionEntity) => {
|
||||
if (!mentionRangeRef.current || !editorRef.current) return
|
||||
|
||||
const range = mentionRangeRef.current
|
||||
range.deleteContents()
|
||||
|
||||
const mentionNode = document.createElement('span')
|
||||
mentionNode.contentEditable = 'false'
|
||||
mentionNode.dataset.type = 'mention'
|
||||
mentionNode.dataset.id = entity.data.id
|
||||
mentionNode.className =
|
||||
'inline-block bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300 px-1.5 py-0.5 rounded-md text-sm font-medium align-middle select-none mx-0.5'
|
||||
mentionNode.innerText = `@${entity.data.name}`
|
||||
|
||||
range.insertNode(mentionNode)
|
||||
|
||||
// Insert a space after the mention
|
||||
const spaceNode = document.createTextNode('\u00A0')
|
||||
mentionNode.after(spaceNode)
|
||||
|
||||
// Move cursor after the space
|
||||
const newRange = document.createRange()
|
||||
const sel = window.getSelection()
|
||||
newRange.setStartAfter(spaceNode)
|
||||
newRange.collapse(true)
|
||||
sel?.removeAllRanges()
|
||||
sel?.addRange(newRange)
|
||||
|
||||
setShowMentionList(false)
|
||||
mentionRangeRef.current = null
|
||||
setHasContent(true)
|
||||
editorRef.current.focus()
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
||||
if (showMentionList && filteredEntities.length > 0) {
|
||||
if (e.key === 'ArrowUp') {
|
||||
e.preventDefault()
|
||||
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredEntities.length - 1))
|
||||
return
|
||||
}
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault()
|
||||
setHighlightedIndex((prev) => (prev < filteredEntities.length - 1 ? prev + 1 : 0))
|
||||
return
|
||||
}
|
||||
if (e.key === 'Enter' || e.key === 'Tab') {
|
||||
e.preventDefault()
|
||||
handleSelectEntity(filteredEntities[highlightedIndex])
|
||||
return
|
||||
}
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault()
|
||||
setShowMentionList(false)
|
||||
return
|
||||
}
|
||||
}
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault()
|
||||
handleSend()
|
||||
}
|
||||
}
|
||||
|
||||
const handleFileSelect = useCallback(
|
||||
async (e: ChangeEvent<HTMLInputElement>) => {
|
||||
if (!e.target.files) return
|
||||
const newFiles = Array.from(e.target.files)
|
||||
|
||||
for (const file of newFiles) {
|
||||
const tempId = `${Date.now()}-${file.name}`
|
||||
const newUploadingFile: UploadingFile = {
|
||||
id: tempId,
|
||||
file,
|
||||
progress: 0,
|
||||
}
|
||||
setUploadingFiles((prev) => [...prev, newUploadingFile])
|
||||
|
||||
try {
|
||||
const data: PostAttachmentResponse = await uploadAttachment({
|
||||
projectId,
|
||||
data: {
|
||||
fileName: file.name,
|
||||
contentType: file.type,
|
||||
size: file.size,
|
||||
},
|
||||
})
|
||||
|
||||
if (data.uploadUrl && data.id) {
|
||||
await fetch(data.uploadUrl, {
|
||||
method: 'PUT',
|
||||
body: file,
|
||||
headers: {
|
||||
'Content-Type': file.type,
|
||||
},
|
||||
})
|
||||
|
||||
setUploadingFiles((prev) =>
|
||||
prev.map((uf) =>
|
||||
uf.id === tempId ? { ...uf, progress: 100, attachmentId: data.id } : uf,
|
||||
),
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
setUploadingFiles((prev) =>
|
||||
prev.map((uf) =>
|
||||
uf.id === tempId
|
||||
? {
|
||||
...uf,
|
||||
error: error instanceof Error ? error.message : 'Upload failed',
|
||||
}
|
||||
: uf,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = ''
|
||||
}
|
||||
},
|
||||
[projectId, uploadAttachment],
|
||||
)
|
||||
|
||||
const removeFile = (id: string) => {
|
||||
setUploadingFiles((prev) => prev.filter((f) => f.id !== id))
|
||||
}
|
||||
|
||||
const handleSend = () => {
|
||||
if (!editorRef.current) return
|
||||
const isUploading = uploadingFiles.some((f) => !f.attachmentId && !f.error)
|
||||
if (isUploading) return
|
||||
|
||||
const successfulAttachmentIds = uploadingFiles
|
||||
.map((f) => f.attachmentId)
|
||||
.filter((id): id is string => !!id)
|
||||
|
||||
// Serialize content
|
||||
let processedText = ''
|
||||
editorRef.current.childNodes.forEach((node) => {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
processedText += node.textContent
|
||||
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
const el = node as HTMLElement
|
||||
if (el.dataset.type === 'mention') {
|
||||
processedText += `<@${el.dataset.id}>`
|
||||
} else if (el.tagName === 'BR') {
|
||||
processedText += '\n'
|
||||
} else if (el.tagName === 'DIV' || el.tagName === 'P') {
|
||||
// contentEditable often uses div/p for new lines
|
||||
processedText += '\n' + el.innerText
|
||||
} else {
|
||||
processedText += el.innerText
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
processedText = processedText.trim()
|
||||
|
||||
// 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,
|
||||
isTimestampEnabled ? currentTime : undefined,
|
||||
)
|
||||
|
||||
// Reset editor
|
||||
editorRef.current.innerHTML = ''
|
||||
setHasContent(false)
|
||||
setUploadingFiles([])
|
||||
setViewingFile(null)
|
||||
setIsDrawing(false)
|
||||
resetAnnotations()
|
||||
setShowMentionList(false)
|
||||
if (onCancelReply) onCancelReply()
|
||||
}
|
||||
|
||||
const isUploading = uploadingFiles.some((f) => !f.attachmentId && !f.error)
|
||||
const hasSuccessfulUploads = uploadingFiles.some((f) => !!f.attachmentId)
|
||||
const canSend = hasContent || hasSuccessfulUploads || annotations.length > 0
|
||||
const showUpperPart = uploadingFiles.length > 0 || !!replyingTo
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
className="relative flex flex-col w-full border border-foreground/35 rounded-3xl shadow-sm transition-all duration-200"
|
||||
>
|
||||
<style>{`
|
||||
[contenteditable]:empty:before {
|
||||
content: attr(data-placeholder);
|
||||
color: #9ca3af;
|
||||
cursor: text;
|
||||
}
|
||||
`}</style>
|
||||
{showMentionList && (filteredAgents.length > 0 || filteredHumans.length > 0) && (
|
||||
<div className="absolute bottom-full left-4 mb-2 w-64 rounded-xl shadow-lg border border-border overflow-hidden z-20 bg-popover text-popover-foreground animate-in fade-in slide-in-from-bottom-2 duration-200">
|
||||
<div className="h-64 overflow-y-auto">
|
||||
{filteredAgents.length > 0 && (
|
||||
<>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setCollapsedSections((prev) => ({ ...prev, bots: !prev.bots }))
|
||||
}}
|
||||
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border sticky top-0 flex items-center justify-between hover:bg-muted transition-colors"
|
||||
>
|
||||
<span>Agents</span>
|
||||
<ChevronDown
|
||||
className={`w-3 h-3 transition-transform ${collapsedSections.bots ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
{!collapsedSections.bots &&
|
||||
filteredAgents.map((item, index) => {
|
||||
const actualIndex = index
|
||||
return (
|
||||
<button
|
||||
key={item.data.id}
|
||||
onClick={() => handleSelectEntity(item)}
|
||||
onMouseEnter={() => setHighlightedIndex(actualIndex)}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
|
||||
actualIndex === highlightedIndex
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'hover:bg-accent/50'
|
||||
}`}
|
||||
>
|
||||
<div className="w-6 h-6 rounded-full bg-purple-100 dark:bg-purple-900/40 flex items-center justify-center text-xs font-bold text-purple-600 dark:text-purple-300">
|
||||
{item.data.name?.[0]}
|
||||
</div>
|
||||
<span className="text-sm">{item.data.name}</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
|
||||
{filteredHumans.length > 0 && (
|
||||
<>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setCollapsedSections((prev) => ({ ...prev, users: !prev.users }))
|
||||
}}
|
||||
className="w-full text-xs font-semibold text-muted-foreground px-3 py-2 bg-muted/50 border-b border-border sticky top-0 flex items-center justify-between hover:bg-muted transition-colors"
|
||||
>
|
||||
<span>Members</span>
|
||||
<ChevronDown
|
||||
className={`w-3 h-3 transition-transform ${collapsedSections.users ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
{!collapsedSections.users &&
|
||||
filteredHumans.map((item, index) => {
|
||||
const actualIndex =
|
||||
(collapsedSections.bots ? 0 : filteredAgents.length) + index
|
||||
return (
|
||||
<button
|
||||
key={item.data.id}
|
||||
onClick={() => handleSelectEntity(item)}
|
||||
onMouseEnter={() => setHighlightedIndex(actualIndex)}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-left ${
|
||||
actualIndex === highlightedIndex
|
||||
? 'bg-accent text-accent-foreground'
|
||||
: 'hover:bg-accent/50'
|
||||
}`}
|
||||
>
|
||||
<div className="w-6 h-6 rounded-full bg-blue-100 dark:bg-blue-900/40 flex items-center justify-center text-xs font-bold text-blue-600 dark:text-blue-300">
|
||||
{item.data.name?.[0]}
|
||||
</div>
|
||||
<span className="text-sm">{item.data.name}</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showUpperPart && (
|
||||
<div className="flex flex-col gap-2 p-3 pb-0 animate-in fade-in slide-in-from-bottom-2 duration-200">
|
||||
{replyingTo && (
|
||||
<div className="flex items-center justify-between bg-muted p-2 px-3 rounded-lg border border-border">
|
||||
<div className="text-xs text-muted-foreground truncate mr-2">
|
||||
reply to: {replyingTo.message}
|
||||
</div>
|
||||
<button
|
||||
onClick={onCancelReply}
|
||||
className="text-muted-foreground hover:text-foreground p-1 rounded-full hover:bg-muted-foreground/10 shrink-0"
|
||||
>
|
||||
<XIcon className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{uploadingFiles.length > 0 && (
|
||||
<div className="flex gap-3 overflow-x-auto py-2 px-1 scrollbar-hide">
|
||||
{uploadingFiles.map((upload) => (
|
||||
<div key={upload.id} className="relative group flex-shrink-0">
|
||||
<div className="w-16 h-16 rounded-lg border border-gray-200 overflow-hidden bg-gray-50 flex items-center justify-center">
|
||||
{upload.file.type.startsWith('image/') ? (
|
||||
<img
|
||||
src={URL.createObjectURL(upload.file)}
|
||||
alt="preview"
|
||||
onClick={() => setViewingFile(upload.file)}
|
||||
className={`w-full h-full object-cover ${
|
||||
!upload.attachmentId ? 'opacity-50' : ''
|
||||
}`}
|
||||
/>
|
||||
) : (
|
||||
<FileIcon className="w-8 h-8 text-gray-400" />
|
||||
)}
|
||||
{isUploading && !upload.attachmentId && !upload.error && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/50">
|
||||
<ProgressCircle progress={upload.progress} className="w-10 h-10" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
onClick={() => removeFile(upload.id)}
|
||||
className="absolute -top-1.5 -right-1.5 bg-gray-500 text-white rounded-full p-0.5 shadow-md opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-500 z-10"
|
||||
>
|
||||
<XIcon className="w-3 h-3" />
|
||||
</button>
|
||||
<span className="text-[10px] text-gray-500 mt-1 block max-w-[4rem] truncate text-center">
|
||||
{upload.file.name}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div
|
||||
onClick={() => editorRef.current?.focus()}
|
||||
className="w-full px-4 pt-1 pb-1 cursor-text flow-root max-h-60 overflow-y-auto"
|
||||
>
|
||||
{isTimestampEnabled && currentTime !== undefined && frameRate !== undefined && (
|
||||
<div
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
editorRef.current?.focus()
|
||||
}}
|
||||
className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-0.5 mt-3 mr-2 rounded-sm text-xs font-mono font-bold flex items-center gap-1 cursor-text"
|
||||
>
|
||||
{formatTimestamp(currentTime, frameRate)}
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
ref={editorRef}
|
||||
contentEditable
|
||||
suppressContentEditableWarning
|
||||
onInput={handleInput}
|
||||
onKeyDown={handleKeyDown}
|
||||
data-placeholder={replyingTo ? 'Reply...' : 'Message...'}
|
||||
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block ${isTimestampEnabled && currentTime !== undefined && frameRate !== undefined && !hasContent ? 'pl-[120px]' : ''}`}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-between items-center px-2 pb-2">
|
||||
{isDrawing ? (
|
||||
<div className="flex items-center gap-2 overflow-x-auto scrollbar-hide w-full mr-2">
|
||||
<Button
|
||||
onClick={() => {
|
||||
setIsDrawing(false)
|
||||
resetAnnotations() // Cancel drawing
|
||||
}}
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="p-2 rounded-full cursor-pointer text-gray-500 hover:text-gray-700 hover:bg-gray-100 mr-2"
|
||||
title="Back"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
</Button>
|
||||
|
||||
<div className="h-4 w-px bg-gray-300 mx-1"></div>
|
||||
|
||||
<button
|
||||
onClick={() => setTool('arrow')}
|
||||
className={`p-1.5 rounded hover:bg-gray-100 ${currentTool === 'arrow' ? 'text-blue-500 bg-blue-50' : 'text-gray-500'}`}
|
||||
title="Arrow"
|
||||
>
|
||||
<ArrowUp size={16} className="rotate-45" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTool('line')}
|
||||
className={`p-1.5 rounded hover:bg-gray-100 ${currentTool === 'line' ? 'text-blue-500 bg-blue-50' : 'text-gray-500'}`}
|
||||
title="Line"
|
||||
>
|
||||
<Minus size={16} className="-rotate-45" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTool('box')}
|
||||
className={`p-1.5 rounded hover:bg-gray-100 ${currentTool === 'box' ? 'text-blue-500 bg-blue-50' : 'text-gray-500'}`}
|
||||
title="Box"
|
||||
>
|
||||
<Square size={16} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTool('freehand')}
|
||||
className={`p-1.5 rounded hover:bg-gray-100 ${currentTool === 'freehand' ? 'text-blue-500 bg-blue-50' : 'text-gray-500'}`}
|
||||
title="Freehand"
|
||||
>
|
||||
<Brush size={16} />
|
||||
</button>
|
||||
|
||||
<div className="h-4 w-px bg-gray-300 mx-1"></div>
|
||||
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<button className="flex items-center gap-1 p-1 rounded hover:bg-gray-100">
|
||||
<div
|
||||
className="w-4 h-4 rounded-full border border-gray-200"
|
||||
style={{ backgroundColor: currentColor }}
|
||||
/>
|
||||
<ChevronDown size={12} className="text-gray-500" />
|
||||
</button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-auto p-2" align="center" side="top">
|
||||
<div className="flex items-center gap-1">
|
||||
{PREDEFINED_COLORS.map((c) => (
|
||||
<button
|
||||
key={c}
|
||||
onClick={() => setColor(c)}
|
||||
className={`w-6 h-6 rounded-full border border-gray-200 transition-transform hover:scale-110 ${currentColor === c ? 'ring-2 ring-blue-500 ring-offset-1' : ''}`}
|
||||
style={{ backgroundColor: c }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
<div className="h-4 w-px bg-gray-300 mx-1"></div>
|
||||
|
||||
<button
|
||||
onClick={undo}
|
||||
disabled={historyIndex <= 0}
|
||||
className="p-1.5 rounded hover:bg-gray-100 disabled:opacity-30 disabled:cursor-not-allowed text-gray-500"
|
||||
>
|
||||
<Undo size={16} />
|
||||
</button>
|
||||
<button
|
||||
onClick={redo}
|
||||
disabled={historyIndex >= history.length - 1}
|
||||
className="p-1.5 rounded hover:bg-gray-100 disabled:opacity-30 disabled:cursor-not-allowed text-gray-500"
|
||||
>
|
||||
<Redo size={16} />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center gap-1">
|
||||
<input
|
||||
type="file"
|
||||
ref={fileInputRef}
|
||||
className="hidden"
|
||||
multiple
|
||||
onChange={handleFileSelect}
|
||||
/>
|
||||
{currentTime !== undefined && frameRate !== undefined && (
|
||||
<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"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
title="Attach files"
|
||||
>
|
||||
<Paperclip className="w-4 h-4" />
|
||||
</Button>
|
||||
{!hideAnnotationControl && (
|
||||
<Button
|
||||
onClick={() => setIsDrawing(true)}
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="p-2 rounded-full cursor-pointer"
|
||||
title="Toggle Annotation"
|
||||
>
|
||||
<DrawAnnotation className="w-4 h-4" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
onClick={handleSend}
|
||||
disabled={!canSend || isUploading}
|
||||
className={`p-2 rounded-full transition-all duration-200 flex items-center justify-center shrink-0 ${
|
||||
canSend && !isUploading
|
||||
? 'bg-blue-600 text-white shadow-md hover:bg-blue-700 hover:shadow-lg transform hover:-translate-y-0.5'
|
||||
: 'bg-gray-400 cursor-not-allowed'
|
||||
}`}
|
||||
>
|
||||
<ArrowUp className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{viewingFile && (
|
||||
<div
|
||||
className="fixed inset-0 z-[100] bg-black/95 flex flex-col items-center justify-center p-4 animate-in fade-in duration-200"
|
||||
onClick={() => setViewingFile(null)}
|
||||
>
|
||||
<button className="absolute top-4 right-4 text-white/70 hover:text-white hover:bg-white/20 rounded-full p-2 transition-colors z-50">
|
||||
<XIcon className="w-8 h-8" />
|
||||
</button>
|
||||
<div className="w-full h-full flex items-center justify-center p-2">
|
||||
<img
|
||||
src={URL.createObjectURL(viewingFile)}
|
||||
alt="Enlarged view"
|
||||
className="max-w-full max-h-[85vh] object-contain rounded shadow-2xl cursor-zoom-out"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
setViewingFile(null)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="absolute bottom-6 text-white text-sm font-medium bg-black/50 px-4 py-2 rounded-full backdrop-blur-sm">
|
||||
{viewingFile.name}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
)
|
||||
ChatInput.displayName = 'ChatInput'
|
||||
Reference in New Issue
Block a user