File size: 5,136 Bytes
8aa87db
 
 
0c591a7
8aa87db
 
 
0c591a7
 
8aa87db
 
 
 
 
 
 
 
0c591a7
 
8aa87db
0c591a7
8aa87db
 
 
0c591a7
 
8aa87db
0c591a7
 
8aa87db
0c591a7
 
 
8aa87db
 
 
 
 
 
 
 
 
0c591a7
8aa87db
 
 
 
 
 
 
0c591a7
 
8aa87db
 
 
 
 
 
 
 
 
 
0c591a7
8aa87db
0c591a7
8aa87db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c591a7
8aa87db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c591a7
 
8aa87db
 
 
 
 
 
 
 
 
 
 
 
0c591a7
8aa87db
 
0c591a7
8aa87db
 
0c591a7
8aa87db
 
0c591a7
8aa87db
 
 
 
 
 
 
 
 
 
 
 
 
0c591a7
8aa87db
 
 
 
 
 
 
 
 
0c591a7
 
8aa87db
 
 
 
 
 
0c591a7
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { useEffect, useRef } from "react"
import { Download } from "lucide-react"
import type { MetricEntry, ActivityLogEntry } from "@/lib/api"

interface UserEvent {
  timestamp: string
  message: string
}

interface ActivityLogProps {
  metrics: MetricEntry[]
  activityLog: ActivityLogEntry[]
  currentStep: string
  revisionCount: number
  score: number
  isTyping?: boolean
  userEvents?: UserEvent[]
}

function formatTime(timestamp: string): string {
  try {
    const date = new Date(timestamp)
    return date.toLocaleTimeString('en-US', {
      hour12: false,
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  } catch {
    return ''
  }
}

function formatValue(value: string | number): string {
  if (typeof value === "number") {
    if (Math.abs(value) >= 1e9) return `$${(value / 1e9).toFixed(1)}B`
    if (Math.abs(value) >= 1e6) return `$${(value / 1e6).toFixed(1)}M`
    if (Math.abs(value) < 100) return value.toFixed(2)
    return value.toLocaleString()
  }
  return String(value)
}

function getCurrentTime(): string {
  return new Date().toLocaleTimeString('en-US', {
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

export function ActivityLog({
  metrics,
  activityLog,
  currentStep,
  revisionCount,
  score,
  isTyping = false,
  userEvents = [],
}: ActivityLogProps) {
  const logRef = useRef<HTMLDivElement>(null)

  // Auto-scroll to bottom when new entries arrive
  useEffect(() => {
    if (logRef.current) {
      logRef.current.scrollTop = logRef.current.scrollHeight
    }
  }, [metrics, activityLog, isTyping, userEvents])

  // Combine and sort all log entries
  const allEntries = React.useMemo(() => {
    const entries: Array<{ time: string; text: string; type: 'metric' | 'activity' | 'user' }> = []

    // Add user events first
    for (const e of userEvents) {
      entries.push({
        time: formatTime(e.timestamp),
        text: `[user] ${e.message}`,
        type: 'user'
      })
    }

    // Add metrics
    for (const m of metrics) {
      entries.push({
        time: formatTime(m.timestamp),
        text: `[${m.source}] ${m.metric}: ${formatValue(m.value)}`,
        type: 'metric'
      })
    }

    // Add activity log
    for (const a of activityLog) {
      entries.push({
        time: formatTime(a.timestamp),
        text: `[${a.step}] ${a.message}`,
        type: 'activity'
      })
    }

    // Sort by timestamp
    return entries.sort((a, b) => a.time.localeCompare(b.time))
  }, [metrics, activityLog, userEvents])

  // Download log as text file
  const handleDownload = () => {
    const logContent = allEntries.map(e => `${e.time} ${e.text}`).join('\n')
    const blob = new Blob([logContent], { type: 'text/plain' })
    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = `analysis-log-${new Date().toISOString().split('T')[0]}.txt`
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
    URL.revokeObjectURL(url)
  }

  return (
    <div className="h-full w-full flex flex-col bg-black/90 rounded border border-zinc-700 font-mono text-[11px]">
      {/* Header */}
      <div className="px-2 py-1 border-b border-zinc-700 text-zinc-400 flex justify-between items-center">
        <span>Activity Log</span>
        <button
          onClick={handleDownload}
          disabled={allEntries.length === 0}
          className="p-1 hover:bg-zinc-700 rounded transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
          title="Download log"
        >
          <Download className="h-3 w-3" />
        </button>
      </div>

      {/* Log content */}
      <div
        ref={logRef}
        className="flex-1 overflow-y-auto overflow-x-auto p-2 space-y-0.5"
      >
        {allEntries.length === 0 && !isTyping ? (
          <div className="text-zinc-500">Waiting for input...</div>
        ) : (
          <>
            {allEntries.map((entry, i) => (
              <div key={i} className="text-zinc-300 whitespace-nowrap">
                <span className="text-zinc-500">{entry.time}</span>
                {' '}
                <span className={
                  entry.type === 'metric' ? 'text-green-400' :
                  entry.type === 'user' ? 'text-blue-400' :
                  'text-zinc-300'
                }>
                  {entry.text}
                </span>
              </div>
            ))}
            {/* Live typing indicator */}
            {isTyping && (
              <div className="text-zinc-300 whitespace-nowrap">
                <span className="text-zinc-500">{getCurrentTime()}</span>
                {' '}
                <span className="text-blue-400">[user] Typing...</span>
              </div>
            )}
          </>
        )}
      </div>

      {/* Footer */}
      <div className="px-2 py-1 border-t border-zinc-700 text-zinc-500 flex justify-between">
        <span>Step: {currentStep}</span>
        {revisionCount > 0 && <span>Rev #{revisionCount}</span>}
      </div>
    </div>
  )
}

export default ActivityLog