86 lines
5.2 KiB
JavaScript
86 lines
5.2 KiB
JavaScript
// IDE Remote: the Share dialog (the screen icon beside Remote Control in the Claude panel). Scope, connection, link + QR code, devices.
|
|
import { el, dialog, copyText, notify, prefs, t } from './ui.js';
|
|
import { remoteHost, DEFAULT_HOURS, SCOPE_LABELS } from './remote-host.js';
|
|
import { qrSvg } from './remote/qr.js';
|
|
|
|
const SCOPE_OPTIONS = [
|
|
['claude', 'The Claude panel'],
|
|
['terminal', 'The Terminal'],
|
|
['all', 'The whole IDE (Claude, Terminal, read-only files and Git status, run output)'],
|
|
];
|
|
const MODE_OPTIONS = [
|
|
['auto', 'Automatic (this network if possible, else the server)'],
|
|
['server', 'Via the server (works from anywhere)'],
|
|
['direct', 'Direct on this network (nothing leaves your network)'],
|
|
];
|
|
const HOURS = [1, 4, 8, 24];
|
|
|
|
function radios(name, options, value, onPick) {
|
|
const box = el('div.remote-opts');
|
|
for (const [id, label] of options) {
|
|
const input = el('input', { type: 'radio', name, value: id, onchange: () => onPick(id) });
|
|
input.checked = id === value;
|
|
box.append(el('label.remote-opt', {}, input, el('span', {}, t(label))));
|
|
}
|
|
return box;
|
|
}
|
|
|
|
function statusText(host) {
|
|
if (host.error && host.relayState === 'error' && !host.addresses.length) return t('Server unreachable: {error}', { error: host.error });
|
|
const parts = [];
|
|
if (host.mode !== 'direct') parts.push(host.relayState === 'ready' ? t('Server: ready') : t('Server: connecting…'));
|
|
if (host.mode !== 'server' && host.addresses.length) parts.push(t('This network: {addr}', { addr: host.addresses.join(', ') }));
|
|
if (host.mode === 'auto' && !host.addresses.length) parts.push(t('This network: not available'));
|
|
return parts.join(' · ');
|
|
}
|
|
|
|
/** Opens the Share dialog of this IDE window. */
|
|
export function openShareDialog(app) {
|
|
const host = remoteHost(app);
|
|
const draft = { scope: prefs.get('remoteScope', 'claude'), mode: prefs.get('remoteMode', 'auto'), hours: prefs.get('remoteHours', DEFAULT_HOURS) };
|
|
if (!SCOPE_OPTIONS.some(([id]) => id === draft.scope)) draft.scope = 'claude';
|
|
if (!MODE_OPTIONS.some(([id]) => id === draft.mode)) draft.mode = 'auto';
|
|
const content = el('div.remote-share');
|
|
const dlg = dialog({ title: t('Share this IDE with a phone or another computer'), body: content, buttons: [{ label: t('Close') }], wide: true, onClose: () => off() });
|
|
|
|
const draw = () => {
|
|
content.textContent = '';
|
|
if (!host.active) {
|
|
const hours = el('select.input', { onchange: () => { draft.hours = Number(hours.value); prefs.set('remoteHours', draft.hours); } },
|
|
...HOURS.map((h) => el('option', { value: String(h) }, t('{n} h', { n: h }))));
|
|
hours.value = String(HOURS.includes(draft.hours) ? draft.hours : DEFAULT_HOURS);
|
|
const start = el('button.btn.primary', { onclick: async () => {
|
|
start.disabled = true;
|
|
prefs.set('remoteScope', draft.scope); prefs.set('remoteMode', draft.mode);
|
|
try { await host.start({ scope: draft.scope, mode: draft.mode, hours: draft.hours }); } catch (e) { notify(e.message || String(e), 'error', { title: t('Remote') }); start.disabled = false; }
|
|
} }, t('Start sharing'));
|
|
content.append(
|
|
el('p', {}, t('A device pairs with a link or a QR code, and you allow it here. Only data is shared (conversation, terminal text), never your screen.')),
|
|
el('h4', {}, t('What to share')), radios('remote-scope', SCOPE_OPTIONS, draft.scope, (v) => { draft.scope = v; }),
|
|
el('h4', {}, t('Connection')), radios('remote-mode', MODE_OPTIONS, draft.mode, (v) => { draft.mode = v; }),
|
|
el('h4', {}, t('Stops after')), hours,
|
|
el('div.remote-actions', {}, start));
|
|
return;
|
|
}
|
|
const link = host.link();
|
|
const qr = el('div.remote-qr');
|
|
try { qr.innerHTML = qrSvg(link, { margin: 2 }); } catch { qr.textContent = t('The link is too long for a QR code: copy it.'); }
|
|
const field = el('input.input.remote-link', { value: link, readonly: '', onfocus: (e) => e.target.select() });
|
|
const devices = host.connected();
|
|
content.append(
|
|
el('p', {}, t('Scan this code with the phone, or open the link on the other computer. Keep it private: it holds the key.')),
|
|
el('div.remote-pair', {}, qr, el('div.remote-pair-side', {},
|
|
field,
|
|
el('div.remote-actions', {}, el('button.btn.primary', { onclick: async () => { await copyText(link); notify(t('Link copied.'), 'success', { title: t('Remote') }); } }, t('Copy the link'))),
|
|
el('p.remote-status', {}, statusText(host)),
|
|
el('p.remote-status', {}, t('Sharing {scope}, until {time}.', { scope: t(SCOPE_LABELS[host.scope]), time: new Date(host.expires * 1000).toLocaleTimeString() })))),
|
|
el('h4', {}, t('Connected devices')),
|
|
devices.length ? el('div.remote-devices', {}, ...devices.map((d) => el('div.remote-device', {},
|
|
el('span.remote-device-name', {}, `${d.name} · ${d.via === 'direct' ? t('this network') : t('the server')}`),
|
|
el('button.btn.small.danger', { onclick: () => host.revoke(d.key) }, t('Revoke'))))) : el('p.remote-status', {}, t('No device connected yet.')),
|
|
el('div.remote-actions', {}, el('button.btn.danger', { onclick: () => host.stop() }, t('Stop sharing'))));
|
|
};
|
|
const off = host.onChange(() => { if (content.isConnected) draw(); });
|
|
draw();
|
|
return dlg;
|
|
}
|