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
|
import React, { memo, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { useStoreState, useActions } from 'm/store';
import { getClashAPIConfig } from 'd/app';
import Icon from 'c/Icon';
import { FixedSizeList as List, areEqual } from 'react-window';
import ContentHeader from 'c/ContentHeader';
import useRemainingViewPortHeight from '../hooks/useRemainingViewPortHeight';
// TODO move this into a redux action
import { fetchLogs } from '../api/logs';
import { getLogsForDisplay, appendLog } from 'd/logs';
import yacd from 's/yacd.svg';
import s0 from 'c/Logs.module.scss';
const paddingBottom = 30;
const colors = {
debug: 'none',
// debug: '#8a8a8a',
info: '#454545',
// info: '#147d14',
warning: '#b99105',
error: '#c11c1c'
};
function LogLine({ time, even, payload, type }) {
const className = cx({ even }, s0.log);
return (
<div className={className}>
<div className={s0.logMeta}>
<div className={s0.logTime}>{time}</div>
<div className={s0.logType} style={{ backgroundColor: colors[type] }}>
{type}
</div>
<div className={s0.logText}>{payload}</div>
</div>
</div>
);
}
function itemKey(index, data) {
const item = data[index];
return item.id;
}
const Row = memo(({ index, style, data }) => {
const r = data[index];
return (
<div style={style}>
<LogLine {...r} />
</div>
);
}, areEqual);
const actions = { appendLog };
export default function Logs() {
const { hostname, port, secret } = useStoreState(getClashAPIConfig);
const { appendLog } = useActions(actions);
const logs = useStoreState(getLogsForDisplay);
useEffect(
() => {
const x = fetchLogs({ hostname, port, secret }, appendLog);
},
[hostname, port, secret]
);
const [refLogsContainer, containerHeight] = useRemainingViewPortHeight();
return (
<div>
<ContentHeader title="Logs" />
<div ref={refLogsContainer} style={{ paddingBottom }}>
{logs.length === 0 ? (
<div
className={s0.logPlaceholder}
style={{ height: containerHeight - paddingBottom }}
>
<div className={s0.logPlaceholderIcon}>
<Icon id={yacd.id} width={200} height={200} />
</div>
<div>No logs yet, hang tight...</div>
</div>
) : (
<div className={s0.logsWrapper}>
<List
height={containerHeight - paddingBottom}
width="100%"
itemCount={logs.length}
itemSize={80}
itemData={logs}
itemKey={itemKey}
>
{Row}
</List>
</div>
)}
</div>
</div>
);
}
|