|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const EXPIRED_NODES_PAGE_SIZE = 50; |
| 16 | + |
| 17 | +// Where the expired-nodes list resumes, and which opening of the section a |
| 18 | +// fetch belongs to, so a slow page never lands in a list reset since. |
| 19 | +let expiredNodesPageToken = ''; |
| 20 | +let expiredNodesGeneration = 0; |
| 21 | + |
| 22 | +document.addEventListener('DOMContentLoaded', () => { |
| 23 | + const expiredNodes = document.getElementById('expired-nodes'); |
| 24 | + expiredNodes.addEventListener('toggle', () => { |
| 25 | + if (expiredNodes.open) { |
| 26 | + loadExpiredNodes(true); |
| 27 | + } |
| 28 | + }); |
| 29 | + document.getElementById('expired-nodes-more').addEventListener('click', () => loadExpiredNodes(false)); |
| 30 | + document.getElementById('table-expired-nodes').addEventListener('click', (e) => { |
| 31 | + const button = e.target.closest('button[data-peer-id]'); |
| 32 | + if (button) { |
| 33 | + revokeExpiredNode(button); |
| 34 | + } |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +// Fetched on demand, never by the background refresh: expired rows stay for |
| 39 | +// the retention period, and listing them all would slow every refresh. |
| 40 | +async function loadExpiredNodes(reset) { |
| 41 | + const more = document.getElementById('expired-nodes-more'); |
| 42 | + if (reset) { |
| 43 | + expiredNodesGeneration++; |
| 44 | + expiredNodesPageToken = ''; |
| 45 | + more.hidden = true; |
| 46 | + setTableMessage('table-expired-nodes', 5, 'Loading...'); |
| 47 | + } |
| 48 | + const generation = expiredNodesGeneration; |
| 49 | + let url = `api/user/nodes?state=expired&page_size=${EXPIRED_NODES_PAGE_SIZE}`; |
| 50 | + if (expiredNodesPageToken) { |
| 51 | + url += `&page_token=${encodeURIComponent(expiredNodesPageToken)}`; |
| 52 | + } |
| 53 | + |
| 54 | + more.disabled = true; |
| 55 | + try { |
| 56 | + const response = await fetch(url); |
| 57 | + if (response.status === 401 || response.status === 403) { |
| 58 | + showLandingPage(); |
| 59 | + throw new Error('Unauthorized. Please login again.'); |
| 60 | + } |
| 61 | + if (!response.ok) { |
| 62 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 63 | + } |
| 64 | + const data = await response.json(); |
| 65 | + if (generation !== expiredNodesGeneration) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const tbody = document.getElementById('table-expired-nodes'); |
| 70 | + const rows = (data.nodes || []).map(expiredNodeRow).join(''); |
| 71 | + if (reset) { |
| 72 | + tbody.innerHTML = rows; |
| 73 | + } else { |
| 74 | + tbody.insertAdjacentHTML('beforeend', rows); |
| 75 | + } |
| 76 | + if (!tbody.querySelector('tr')) { |
| 77 | + setTableMessage('table-expired-nodes', 5, 'No expired nodes'); |
| 78 | + } |
| 79 | + expiredNodesPageToken = data.next_page_token || ''; |
| 80 | + more.hidden = !expiredNodesPageToken; |
| 81 | + applySearchFilter(); |
| 82 | + } catch (error) { |
| 83 | + if (generation !== expiredNodesGeneration) { |
| 84 | + return; |
| 85 | + } |
| 86 | + console.error('Failed to load expired nodes:', error); |
| 87 | + // A failed "Load more" keeps the rows already shown and the button to retry. |
| 88 | + if (reset) { |
| 89 | + setTableMessage('table-expired-nodes', 5, `Error loading expired nodes: ${error.message}`, true); |
| 90 | + } else { |
| 91 | + showToast(`Failed to load more expired nodes: ${error.message}`, 'error'); |
| 92 | + } |
| 93 | + } finally { |
| 94 | + more.disabled = false; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// The Revoke button carries the peer ID as data, not inside an inline handler, |
| 99 | +// where an HTML-escaped quote would be decoded back into the script. |
| 100 | +function expiredNodeRow(node) { |
| 101 | + const expired = node.expire_time ? new Date(node.expire_time).toLocaleString() : ''; |
| 102 | + return ` |
| 103 | + <tr> |
| 104 | + <td>${peerCell(node.peer_id, node.labels)}</td> |
| 105 | + <td>${escapeHTML(node.role)}</td> |
| 106 | + <td>${escapeHTML(node.owner_id || '')}</td> |
| 107 | + <td>${escapeHTML(expired)}</td> |
| 108 | + <td><button class="btn btn-sm btn-danger" type="button" data-peer-id="${escapeHTML(node.peer_id)}">Revoke</button></td> |
| 109 | + </tr> |
| 110 | + `; |
| 111 | +} |
| 112 | + |
| 113 | +// Paging resumes after the last row served, so dropping a revoked row never |
| 114 | +// shifts the next page. |
| 115 | +async function revokeExpiredNode(button) { |
| 116 | + if (!confirm('Revoke this node? It can no longer renew its expired session or rejoin the mesh.')) { |
| 117 | + return; |
| 118 | + } |
| 119 | + button.disabled = true; |
| 120 | + try { |
| 121 | + await actionRequest(`api/user/revoke?id=${encodeURIComponent(button.dataset.peerId)}`); |
| 122 | + } catch (error) { |
| 123 | + // actionRequest already reported the failure. |
| 124 | + button.disabled = false; |
| 125 | + return; |
| 126 | + } |
| 127 | + const tbody = document.getElementById('table-expired-nodes'); |
| 128 | + button.closest('tr').remove(); |
| 129 | + if (!tbody.querySelector('tr:not([data-filter-empty])')) { |
| 130 | + loadExpiredNodes(true); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// The landing page hides the app without a reload, so the next identity to log |
| 135 | +// in must not inherit rows, or an in-flight page, fetched for this one. |
| 136 | +function resetExpiredNodes() { |
| 137 | + expiredNodesGeneration++; |
| 138 | + expiredNodesPageToken = ''; |
| 139 | + document.getElementById('expired-nodes').open = false; |
| 140 | + document.getElementById('table-expired-nodes').innerHTML = ''; |
| 141 | + document.getElementById('expired-nodes-more').hidden = true; |
| 142 | +} |
0 commit comments