File size: 6,889 Bytes
eb846d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
180
181
182
183
184
185
186
187
188
189
190
191
import React, { useEffect, useRef, useState } from 'react';
import { LogEntry } from '../services/logService';
import { Button } from './ui/Button';
import { Badge } from './ui/Badge';
import { useTranslation } from 'react-i18next';

interface LogViewerProps {
  logs: LogEntry[];
  isLoading?: boolean;
  error?: Error | null;
  onClear?: () => void;
}

const LogViewer: React.FC<LogViewerProps> = ({ logs, isLoading = false, error = null, onClear }) => {
  const { t } = useTranslation();
  const logContainerRef = useRef<HTMLDivElement>(null);
  const [autoScroll, setAutoScroll] = useState(true);
  const [filter, setFilter] = useState<string>('');
  const [typeFilter, setTypeFilter] = useState<Array<'info' | 'error' | 'warn' | 'debug'>>(['info', 'error', 'warn', 'debug']);
  const [sourceFilter, setSourceFilter] = useState<Array<'main' | 'child'>>(['main', 'child']);

  // Auto scroll to bottom when new logs come in if autoScroll is enabled
  useEffect(() => {
    if (autoScroll && logContainerRef.current) {
      logContainerRef.current.scrollTop = logContainerRef.current.scrollHeight;
    }
  }, [logs, autoScroll]);

  // Filter logs based on current filter settings
  const filteredLogs = logs.filter(log => {
    const matchesText = filter ? log.message.toLowerCase().includes(filter.toLowerCase()) : true;
    const matchesType = typeFilter.includes(log.type);
    const matchesSource = sourceFilter.includes(log.source as 'main' | 'child');
    return matchesText && matchesType && matchesSource;
  });

  // Format timestamp to readable format
  const formatTimestamp = (timestamp: number) => {
    const date = new Date(timestamp);
    return date.toLocaleTimeString([], {
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
      hour12: false
    });
  };

  // Get badge color based on log type
  const getLogTypeColor = (type: string) => {
    switch (type) {
      case 'error': return 'bg-red-400';
      case 'warn': return 'bg-yellow-400';
      case 'debug': return 'bg-purple-400';
      default: return 'bg-blue-400';
    }
  };

  // Get badge color based on log source
  const getSourceColor = (source: string) => {
    switch (source) {
      case 'main': return 'bg-green-400';
      case 'child': return 'bg-orange-400';
      default: return 'bg-gray-400';
    }
  };

  return (
    <div className="flex flex-col h-full">
      <div className="bg-card p-3 rounded-t-md border-b flex flex-wrap items-center justify-between gap-2">
        <div className="flex flex-wrap items-center gap-2">
          <span className="font-semibold text-sm">{t('logs.filters')}:</span>

          {/* Text search filter */}
          <input
            type="text"
            placeholder={t('logs.search')}
            className="px-2 py-1 text-sm border rounded"
            value={filter}
            onChange={(e) => setFilter(e.target.value)}
          />

          {/* Log type filters */}
          <div className="flex gap-1 items-center">
            {(['info', 'error', 'warn', 'debug'] as const).map(type => (
              <Badge
                key={type}
                variant={typeFilter.includes(type) ? 'default' : 'outline'}
                className={`cursor-pointer ${typeFilter.includes(type) ? getLogTypeColor(type) : ''}`}
                onClick={() => {
                  if (typeFilter.includes(type)) {
                    setTypeFilter(prev => prev.filter(t => t !== type));
                  } else {
                    setTypeFilter(prev => [...prev, type]);
                  }
                }}
              >
                {type}
              </Badge>
            ))}
          </div>

          {/* Log source filters */}
          <div className="flex gap-1 items-center ml-2">
            {(['main', 'child'] as const).map(source => (
              <Badge
                key={source}
                variant={sourceFilter.includes(source) ? 'default' : 'outline'}
                className={`cursor-pointer ${sourceFilter.includes(source) ? getSourceColor(source) : ''}`}
                onClick={() => {
                  if (sourceFilter.includes(source)) {
                    setSourceFilter(prev => prev.filter(s => s !== source));
                  } else {
                    setSourceFilter(prev => [...prev, source]);
                  }
                }}
              >
                {source === 'main' ? t('logs.mainProcess') : t('logs.childProcess')}
              </Badge>
            ))}
          </div>
        </div>

        <div className="flex items-center gap-2">
          <label className="flex items-center gap-1 text-sm">
            <input
              type="checkbox"
              checked={autoScroll}
              onChange={() => setAutoScroll(!autoScroll)}
              className="form-checkbox h-4 w-4"
            />
            {t('logs.autoScroll')}
          </label>
          <Button
            variant="outline"
            size="sm"
            onClick={onClear}
            disabled={isLoading || logs.length === 0}
          >
            {t('logs.clearLogs')}
          </Button>
        </div>
      </div>

      <div
        ref={logContainerRef}
        className="flex-grow p-2 overflow-auto bg-card rounded-b-md font-mono text-sm"
        style={{ maxHeight: 'calc(100vh - 300px)' }}
      >
        {isLoading ? (
          <div className="flex justify-center items-center h-full">
            <span>{t('logs.loading')}</span>
          </div>
        ) : error ? (
          <div className="text-red-500 p-2">
            {error.message}
          </div>
        ) : filteredLogs.length === 0 ? (
          <div className="text-center text-muted-foreground p-8">
            {filter || typeFilter.length < 4 || sourceFilter.length < 2
              ? t('logs.noMatch')
              : t('logs.noLogs')}
          </div>
        ) : (
          filteredLogs.map((log, index) => (
            <div
              key={`${log.timestamp}-${index}`}
              className={`py-1 border-b border-gray-100 dark:border-gray-800 ${log.type === 'error' ? 'text-red-500' :
                log.type === 'warn' ? 'text-yellow-500' : ''
                }`}
            >
              <span className="text-gray-400">[{formatTimestamp(log.timestamp)}]</span>
              <Badge className={`ml-2 mr-1 ${getLogTypeColor(log.type)}`}>
                {log.type}
              </Badge>
              <Badge
                variant="default"
                className={`mr-2 ${getSourceColor(log.source)}`}
              >
                {log.source === 'main' ? t('logs.main') : t('logs.child')}
                {log.processId ? ` (${log.processId})` : ''}
              </Badge>
              <span className="whitespace-pre-wrap">{log.message}</span>
            </div>
          ))
        )}
      </div>
    </div>
  );
};

export default LogViewer;