feat: enhance connections display order

This commit is contained in:
GyDi 2022-03-23 14:02:08 +08:00 committed by GitHub
parent 5d0ffbe453
commit 4b6189af5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,7 @@ const ConnectionsPage = () => {
const initConn = { uploadTotal: 0, downloadTotal: 0, connections: [] }; const initConn = { uploadTotal: 0, downloadTotal: 0, connections: [] };
const { t } = useTranslation(); const { t } = useTranslation();
const [conn, setConn] = useState<ApiType.Connections>(initConn); const [connData, setConnData] = useState<ApiType.Connections>(initConn);
useEffect(() => { useEffect(() => {
let ws: WebSocket | null = null; let ws: WebSocket | null = null;
@ -23,7 +23,35 @@ const ConnectionsPage = () => {
ws.addEventListener("message", (event) => { ws.addEventListener("message", (event) => {
const data = JSON.parse(event.data) as ApiType.Connections; const data = JSON.parse(event.data) as ApiType.Connections;
setConn(data); setConnData((old) => {
const oldConn = old.connections;
const oldList = oldConn.map((each) => each.id);
const maxLen = data.connections.length;
const connections: typeof oldConn = [];
// 与前一次连接的顺序尽量保持一致
data.connections
.filter((each) => {
const index = oldList.indexOf(each.id);
if (index >= 0 && index < maxLen) {
connections[index] = each;
return false;
}
return true;
})
.forEach((each) => {
for (let i = 0; i < maxLen; ++i) {
if (!connections[i]) {
connections[i] = each;
return;
}
}
});
return { ...data, connections };
});
}); });
}); });
@ -50,7 +78,7 @@ const ConnectionsPage = () => {
<Paper sx={{ boxShadow: 2, height: "100%" }}> <Paper sx={{ boxShadow: 2, height: "100%" }}>
<Virtuoso <Virtuoso
initialTopMostItemIndex={999} initialTopMostItemIndex={999}
data={conn.connections} data={connData.connections}
itemContent={(index, item) => <ConnectionItem value={item} />} itemContent={(index, item) => <ConnectionItem value={item} />}
/> />
</Paper> </Paper>