-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
145 lines (118 loc) · 4.52 KB
/
main.js
File metadata and controls
145 lines (118 loc) · 4.52 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function sendToBackend(formData) {
window.localApi.sendNodeForm(formData);
}
const nodeName = document.querySelector('#node-name');
const nodePort = document.querySelector('#node-port');
const nodeRpc = document.querySelector('#node-rpc');
const selectDir = document.querySelector('#select-dir');
const submit = document.querySelector('#submit');
// Initially disable submit button
submit.disabled = true;
// Enable/disable submit button based on node name input
nodeName.addEventListener('input', () => {
submit.disabled = !nodeName.value.trim();
});
selectDir.addEventListener('click', () => {
window.localApi.sendAction('open-directory-dialog');
});
window.localApi.onSelectedDirectory(path => {
document.getElementById('node-name').value = path;
// Enable submit button when directory is selected
submit.disabled = !path;
// Gray out the select directory button
selectDir.classList.add('grayed-out');
});
submit.addEventListener('click', (event) => {
event.preventDefault();
if (!nodeName.value) {
alert('Node name is required');
return;
}
const formData = {
nodeName: nodeName.value,
nodePort: nodePort.value || null,
nodeRpc: nodeRpc.value || null
};
sendToBackend(formData);
checkAndRedirect((nodePort.value) ? nodePort.value : '8080');
});
function checkAndRedirect(port) {
console.log(`Redirecting to http://localhost:${port}`);
fetch(`http://localhost:${port}`, { mode: 'no-cors' })
.then(() => {
window.localApi.sendGoHome(port);
})
.catch(() => {
setTimeout(() => checkAndRedirect(port), 1000);
});
}
// Handle update notifications
window.localApi.onUpdateAvailable((updateInfo) => {
const notification = document.getElementById('update-notification');
const message = document.getElementById('update-message');
const downloadBtn = document.getElementById('download-update');
const dismissBtn = document.getElementById('dismiss-update');
message.textContent = `Version ${updateInfo.latestVersion} is available (current: ${updateInfo.currentVersion})`;
notification.classList.add('show');
downloadBtn.addEventListener('click', () => {
window.localApi.openDownloadUrl(updateInfo.downloadUrl);
notification.classList.remove('show');
});
dismissBtn.addEventListener('click', () => {
notification.classList.remove('show');
});
});
// Handle previously booted nodes
let allNodes = [];
let showingAll = false;
function displayNodes(nodes, showAll = false) {
const nodesList = document.getElementById('node-dirs-list');
const showMoreBtn = document.getElementById('show-more-btn');
const nodeDirsContainer = document.getElementById('node-dirs');
// Clear existing list
nodesList.innerHTML = '';
if (nodes.length === 0) {
nodeDirsContainer.classList.add('hidden');
return;
}
nodeDirsContainer.classList.remove('hidden');
// Determine how many nodes to show
const nodesToShow = showAll ? nodes : nodes.slice(0, 3);
nodesToShow.forEach(node => {
const li = document.createElement('li');
li.className = 'node-item';
li.innerHTML = `
<div class="node-path">${node.path}</div>
<div class="node-details">Port: ${node.port}${node.rpc ? ', RPC: ' + node.rpc : ''}</div>
`;
li.addEventListener('click', () => {
document.getElementById('node-name').value = node.path;
document.getElementById('node-port').value = node.port !== '8080' ? node.port : '';
document.getElementById('node-rpc').value = node.rpc || '';
// Enable submit button when a node is selected
submit.disabled = false;
// Gray out the select directory button
selectDir.classList.add('grayed-out');
});
nodesList.appendChild(li);
});
// Show/hide "Show more" button
if (nodes.length > 3 && !showAll) {
showMoreBtn.style.display = 'block';
showMoreBtn.textContent = `Show ${nodes.length - 3} more`;
} else if (showAll) {
showMoreBtn.style.display = 'block';
showMoreBtn.textContent = 'Show less';
} else {
showMoreBtn.style.display = 'none';
}
}
window.localApi.onBootedNodes((nodes) => {
allNodes = nodes;
displayNodes(nodes, false);
});
const showMoreBtn = document.getElementById('show-more-btn');
showMoreBtn.addEventListener('click', () => {
showingAll = !showingAll;
displayNodes(allNodes, showingAll);
});