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
|
import cx from 'clsx';
import * as React from 'react';
import { Pause, Play } from 'react-feather';
import { useTranslation } from 'react-i18next';
import { areEqual, FixedSizeList as List, ListChildComponentProps } from 'react-window';
import { fetchLogs, reconnect as reconnectLogs,stop as stopLogs } from 'src/api/logs';
import ContentHeader from 'src/components/ContentHeader';
import LogSearch from 'src/components/LogSearch';
import { connect, useStoreActions } from 'src/components/StateProvider';
import SvgYacd from 'src/components/SvgYacd';
import useRemainingViewPortHeight from 'src/hooks/useRemainingViewPortHeight';
import { getClashAPIConfig, getLogStreamingPaused } from 'src/store/app';
import { getLogLevel } from 'src/store/configs';
import { appendLog, getLogsForDisplay } from 'src/store/logs';
import { Log, State } from 'src/store/types';
import s from './Logs.module.scss';
import { Fab, position as fabPosition } from './shared/Fab';
const { useCallback, memo, useEffect } = React;
const paddingBottom = 30;
const colors = {
debug: '#28792c',
info: 'var(--bg-log-info-tag)',
warning: '#b99105',
error: '#c11c1c',
};
type LogLineProps = Partial<Log>;
function LogLine({ time, even, payload, type }: LogLineProps) {
const className = cx({ even }, 'log');
return (
<div className={className}>
<div className={s.logMeta}>
<div className={s.logTime}>{time}</div>
<div className={s.logType} style={{ backgroundColor: colors[type] }}>
{type}
</div>
<div className={s.logText}>{payload}</div>
</div>
</div>
);
}
function itemKey(index: number, data: LogLineProps[]) {
const item = data[index];
return item.id;
}
const Row = memo(({ index, style, data }: ListChildComponentProps<LogLineProps>) => {
const r = data[index];
return (
<div style={style}>
<LogLine {...r} />
</div>
);
}, areEqual);
function Logs({ dispatch, logLevel, apiConfig, logs, logStreamingPaused }) {
const actions = useStoreActions();
const toggleIsRefreshPaused = useCallback(() => {
logStreamingPaused ? reconnectLogs({ ...apiConfig, logLevel }) : stopLogs();
// being lazy here
// ideally we should check the result of previous operation before updating this
actions.app.updateAppConfig('logStreamingPaused', !logStreamingPaused);
}, [apiConfig, logLevel, logStreamingPaused, actions.app]);
const appendLogInternal = useCallback((log) => dispatch(appendLog(log)), [dispatch]);
useEffect(() => {
fetchLogs({ ...apiConfig, logLevel }, appendLogInternal);
}, [apiConfig, logLevel, appendLogInternal]);
const [refLogsContainer, containerHeight] = useRemainingViewPortHeight();
const { t } = useTranslation();
return (
<div>
<ContentHeader title={t('Logs')} />
<LogSearch />
<div ref={refLogsContainer} style={{ paddingBottom }}>
{logs.length === 0 ? (
<div className={s.logPlaceholder} style={{ height: containerHeight - paddingBottom }}>
<div className={s.logPlaceholderIcon}>
<SvgYacd width={200} height={200} />
</div>
<div>{t('no_logs')}</div>
</div>
) : (
<div className={s.logsWrapper}>
<List
height={containerHeight - paddingBottom}
width="100%"
itemCount={logs.length}
itemSize={80}
itemData={logs}
itemKey={itemKey}
>
{Row}
</List>
<Fab
icon={logStreamingPaused ? <Play size={16} /> : <Pause size={16} />}
mainButtonStyles={logStreamingPaused ? { background: '#e74c3c' } : {}}
style={fabPosition}
text={logStreamingPaused ? t('Resume Refresh') : t('Pause Refresh')}
onClick={toggleIsRefreshPaused}
></Fab>
</div>
)}
</div>
</div>
);
}
const mapState = (s: State) => ({
logs: getLogsForDisplay(s),
logLevel: getLogLevel(s),
apiConfig: getClashAPIConfig(s),
logStreamingPaused: getLogStreamingPaused(s),
});
export default connect(mapState)(Logs);
|