// The customer's network drawn as a map: their private network is the hub,
// servers are nodes around it (coloured by status), the VPN sits on top.
// Returns an SVG string; nodes carry data-vmid / data-open for click handling.
import { esc } from '/shared/ui.js';
import { customOsIcon } from '/shared/icons.js';
const W = 720, H = 330, CX = 360, CY = 172, RX = 262, RY = 104;
const MAX_NODES = 11;
// Neutral glyph paths inside a 24x24 box (same shapes as icons.js).
const GLYPH = {
windows: '',
linux: '',
server: '',
shield: '',
more: '',
};
const glyph = (name, x, y, size = 20) => {
const own = customOsIcon(name);
if (own) return ``;
return ``;
};
const short = (s, n = 16) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
function statusOf(vm, busy) {
if (vm.state === 'failed') return 'failed';
if (vm.state !== 'ready' || busy.has(vm.vmid)) return 'busy';
return vm.status === 'running' ? 'running' : 'stopped';
}
const STATUS_WORD = { running: 'running', stopped: 'stopped', busy: 'working', failed: 'setup failed' };
/**
* @param {object} p
* @param {Array} p.vms server summaries from /api/vms
* @param {object|null} p.network { subnet } or null
* @param {object|null} p.vpn { enabled, devices:[{lastHandshake}] } or null
* @param {Set} p.busy vmids with a running action
* @param {boolean} p.animate play the one-time draw-in
*/
export function netMap({ vms, network, vpn, busy, animate }) {
const shown = vms.slice(0, vms.length > MAX_NODES + 1 ? MAX_NODES : MAX_NODES + 1);
const extra = vms.length - shown.length;
const count = shown.length + (extra > 0 ? 1 : 0);
const withVpn = !!vpn?.enabled;
// Spread nodes around the hub; leave the top free for the VPN node.
const start = withVpn ? -55 : -90, span = withVpn ? 290 : 360;
const pos = (i) => {
const deg = withVpn ? start + (span * (i + 0.5)) / count : start + (span * i) / count;
const rad = (deg * Math.PI) / 180;
return { x: CX + RX * Math.cos(rad), y: CY + RY * Math.sin(rad) };
};
const edges = [];
const nodes = [];
shown.forEach((vm, i) => {
const p = pos(i);
const st = statusOf(vm, busy);
edges.push(``);
nodes.push(`
${glyph(vm.os === 'windows' ? 'windows' : vm.os === 'linux' ? 'linux' : 'server', p.x, p.y)}
${esc(short(vm.name))}
`);
});
if (extra > 0) {
const p = pos(shown.length);
edges.push(``);
nodes.push(`
+${extra}
`);
}
let vpnNode = '';
if (withVpn) {
const devices = vpn.devices ?? [];
const online = devices.filter((d) => d.lastHandshake && Date.now() / 1000 - d.lastHandshake < 180).length;
const label = devices.length
? `${devices.length} device${devices.length === 1 ? '' : 's'}${online ? `, ${online} connected` : ''}`
: 'no devices yet';
edges.push(``);
vpnNode = `
${glyph('shield', CX - 68, 37, 18)}
VPN access
${esc(label)}
`;
}
const hub = `
${network ? 'Private network' : 'Your servers'}
${network ? esc(network.subnet) : `${vms.length} server${vms.length === 1 ? '' : 's'}`}
`;
return `
`;
}