forked from HuolalaTech/page-spy-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
120 lines (111 loc) · 3.8 KB
/
index.tsx
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
import { useSocketMessageStore } from '@/store/socket-message';
import { DoubleRightOutlined } from '@ant-design/icons';
import { Row, Col, Button } from 'antd';
import { useCallback, useEffect, useRef, useState } from 'react';
import ConsoleNode from '../ConsoleNode';
import {
isPlaceholderNode,
PlaceholderNode,
} from '../ConsoleNode/PlaceholderNode';
import { isErrorTraceNode, ErrorTraceNode } from '../ConsoleNode/ErrorTrace';
import LogType from '../LogType';
import Timestamp from '../Timestamp';
import { useTranslation } from 'react-i18next';
export const MainContent = () => {
const { t } = useTranslation('translation', { keyPrefix: 'console' });
const containerEl = useRef<HTMLDivElement | null>(null);
const scrollToBottom = useCallback(() => {
const container = containerEl.current;
if (!container) return;
container.scrollTo({
top: container.scrollHeight - container.offsetHeight,
behavior: 'smooth',
});
}, []);
const [data] = useSocketMessageStore((state) => [state.consoleMsg]);
const [dataFilter] = useSocketMessageStore((state) => [
state.consoleMsgTypeFilter,
]);
const [newTips, setNewTips] = useState<boolean>(false);
useEffect(() => {
if (data.length === 0) {
setNewTips(false);
}
}, [data]);
useEffect(() => {
const logType = [...data].pop()?.logType || '';
const isDebug = ['debug-origin', 'debug-eval'].includes(logType);
const evalError =
data.length > 1 && data[data.length - 2].logType === 'debug-origin';
if (isDebug || evalError) {
scrollToBottom();
} else {
const container = containerEl.current;
if (!container) return;
const { offsetHeight, scrollHeight } = container;
if (scrollHeight > offsetHeight) {
setNewTips(true);
}
}
}, [data, scrollToBottom]);
useEffect(() => {
const container = containerEl.current;
if (!container) return;
const fn = () => {
const { offsetHeight, scrollTop, scrollHeight } = container;
if (scrollHeight > offsetHeight) {
if (scrollTop + offsetHeight > scrollHeight - 30) {
setNewTips(false);
}
}
};
container.addEventListener('scrollend', fn);
return () => {
container.removeEventListener('scrollend', fn);
};
}, []);
return (
<div className="console-list" ref={containerEl}>
{data
.filter((item) =>
dataFilter.length ? dataFilter.includes(item.logType) : item,
)
.map((item) => (
<div className={`console-item ${item.logType}`} key={item.id}>
<div className="console-item__title">
<LogType type={item.logType} />
</div>
<div className="console-item__content">
<Row gutter={12} wrap={false}>
<Col style={{ flexShrink: 0 }}>
<Timestamp time={item.time} />
</Col>
<Col flex={1}>
{isPlaceholderNode(item) ? (
<PlaceholderNode data={item.logs} />
) : isErrorTraceNode(item) ? (
<ErrorTraceNode data={item} />
) : (
item.logs?.map((log) => {
return <ConsoleNode data={log} key={log.id} />;
})
)}
</Col>
</Row>
</div>
<div className="console-item__url" title={item.url}>
{item.url?.substring(new URL(item.url).origin.length)}
</div>
</div>
))}
{newTips && (
<div className="console-list__new" onClick={scrollToBottom}>
<Button shape="round" type="primary">
<DoubleRightOutlined rotate={90} />
<span>{t('newContent')}</span>
</Button>
</div>
)}
</div>
);
};