Skip to content

Commit

Permalink
tsrelay/handler: add offline peer group (#186)
Browse files Browse the repository at this point in the history
Put offline peers in a separate group. This improves the UI when there
are a lot of peers, and makes it easier to see and reach for the online
peers in "All nodes".

Fixes: #185

---------

Co-authored-by: Marwan Sulaiman <marwan.sameer@gmail.com>
  • Loading branch information
shayne and marwan-at-work committed Aug 9, 2023
1 parent 89d730f commit cc64ced
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions tsrelay/handler/get_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (h *handler) getPeers(ctx context.Context, body io.Reader) (*getPeersRespon
return nil, err
}

s := getPeersResponse{
PeerGroups: []*peerGroup{
{Name: "My nodes"},
{Name: "All nodes"},
},
s := getPeersResponse{PeerGroups: []*peerGroup{}}
peerGroups := [...]*peerGroup{
{Name: "My nodes"},
{Name: "All nodes"},
{Name: "Offline nodes"},
}

if st.BackendState == "NeedsLogin" || (st.Self != nil && !st.Self.Online) {
Expand Down Expand Up @@ -130,33 +130,26 @@ func (h *handler) getPeers(ctx context.Context, body io.Reader) (*getPeersRespon
SSHEnabled: len(p.SSH_HostKeys) > 0,
Address: addr,
}
if p.UserID == st.Self.UserID {
s.PeerGroups[0].Peers = append(s.PeerGroups[0].Peers, peer)

if !p.Online {
peerGroups[2].Peers = append(peerGroups[2].Peers, peer)
} else if p.UserID == st.Self.UserID {
peerGroups[0].Peers = append(peerGroups[0].Peers, peer)
} else {
s.PeerGroups[1].Peers = append(s.PeerGroups[1].Peers, peer)
peerGroups[1].Peers = append(peerGroups[1].Peers, peer)
}
}

myNodes := len(s.PeerGroups[0].Peers)
allNodes := len(s.PeerGroups[1].Peers)
if myNodes == 0 && allNodes > 0 {
s.PeerGroups = s.PeerGroups[1:]
} else if allNodes == 0 && myNodes > 0 {
s.PeerGroups = s.PeerGroups[0:1]
} else if myNodes == 0 && allNodes == 0 {
s.PeerGroups = nil
for _, pg := range peerGroups {
if len(pg.Peers) > 0 {
s.PeerGroups = append(s.PeerGroups, pg)
}
}

for _, pg := range s.PeerGroups {
peers := pg.Peers
sort.Slice(peers, func(i, j int) bool {
if peers[i].Online && !peers[j].Online {
return true
}
if peers[j].Online && !peers[i].Online {
return false
}
return peers[i].HostName < peers[j].HostName
return peers[i].ServerName < peers[j].ServerName
})
}

Expand Down

0 comments on commit cc64ced

Please sign in to comment.