import { createIcons, ExternalLink, Laptop, LogOut, RefreshCw, Server, WifiOff } from 'lucide' import { emptyState, relativeTime, type AppState } from './common.js' const lucideIcons = { ExternalLink, Laptop, LogOut, RefreshCw, Server, WifiOff } const lucideNames: Record = { ExternalLink: 'external-link', Laptop: 'laptop', LogOut: 'log-out', RefreshCw: 'refresh-cw', Server: 'server', WifiOff: 'wifi-off', } function icon(name: keyof typeof lucideIcons, size = 16): string { return `` } async function sendCommand(action: string, payload: Record = {}): Promise { const response = await chrome.runtime.sendMessage({ type: 'command', action, payload }) as { ok?: unknown; error?: unknown } | undefined if (response?.ok !== true) throw new Error(typeof response?.error === 'string' ? response.error : 'The launcher did not respond. Try again.') } export class Ui { private state: AppState = { ...emptyState } private localError?: string async start(): Promise { chrome.runtime.onMessage.addListener((message: unknown) => { if (typeof message !== 'object' || message === null || (message as { type?: string }).type !== 'state') return this.state = (message as { state: AppState }).state this.render() }) const response = await chrome.runtime.sendMessage({ type: 'getState' }).catch(() => undefined) if (response !== undefined && typeof response === 'object' && 'state' in response) this.state = (response as { state: AppState }).state this.render() if (this.state.authorized) void sendCommand('refresh').catch(error => this.showError(error)) window.setInterval(() => { if (this.state.authorized && this.state.openingHostId === undefined) void sendCommand('refresh').catch(() => undefined) }, 15_000) } private render(): void { const root = document.getElementById('root') if (root === null) return root.innerHTML = this.state.authorized ? this.renderLauncher() : this.renderAuthorization() this.wire() createIcons({ icons: lucideIcons }) } private renderLauncher(): string { const copy = launcherCopy() const onlineHosts = this.state.hosts.filter(host => host.online) const hosts = [...this.state.hosts].sort((left, right) => Number(right.online) - Number(left.online)) const rows = hosts.map(host => { const opening = this.state.openingHostId === host.deviceId const detail = [platformLabel(host.platform), host.harnessVersion ? `Harness ${host.harnessVersion}` : undefined] .filter((value): value is string => value !== undefined) .join(' · ') const action = host.online ? opening ? `${icon('RefreshCw', 15)}${escapeHtml(copy.opening)}` : '' : host.lastSeenAt === undefined ? '' : escapeHtml(copy.lastSeen(relativeTime(host.lastSeenAt))) const label = host.online ? `${opening ? copy.opening : copy.open} ${host.name}` : undefined return `` }).join('') const empty = `
${icon('WifiOff', 25)}${escapeHtml(copy.noMachines)}

${escapeHtml(copy.noMachinesHint)}

` const notice = this.state.notice ?? this.localError return `
${this.renderHeader(true)}

${escapeHtml(copy.myMachines)}

${escapeHtml(copy.machineHint)}

${escapeHtml(copy.onlineCount(onlineHosts.length, hosts.length))}
${notice === undefined ? '' : ``}
${rows || empty}
${icon('Server', 13)}${escapeHtml(serverHost(this.state.settings?.serverUrl))}
` } private renderAuthorization(): string { const copy = authorizationCopy() const error = this.state.authorizationError ?? this.localError return `
${this.renderHeader(false)}

${escapeHtml(copy.title)}

${escapeHtml(copy.overview)}

${error === undefined ? '' : ``}

${escapeHtml(copy.note)}

` } private renderHeader(actions: boolean): string { const copy = launcherCopy() return `
DeepSeek Harness Remote${this.state.account === undefined ? '' : `${escapeHtml(this.state.account)}`}
${actions ? `
` : ''}
` } private wire(): void { if (!this.state.authorized) { document.getElementById('authorizeWeb')?.addEventListener('click', () => { this.localError = undefined void sendCommand('authorizeFromWeb').catch(error => this.showError(error)) }) return } document.getElementById('refresh')?.addEventListener('click', () => { this.localError = undefined void sendCommand('refresh').catch(error => this.showError(error)) }) document.getElementById('signOut')?.addEventListener('click', () => { if (!window.confirm(launcherCopy().signOutConfirm)) return void sendCommand('signOut').catch(error => this.showError(error)) }) document.querySelectorAll('[data-host-id]').forEach(row => row.addEventListener('click', () => { const deviceId = row.dataset.hostId if (deviceId === undefined) return this.localError = undefined void sendCommand('openHost', { deviceId }).then(() => window.close()).catch(error => this.showError(error)) })) } private showError(error: unknown): void { this.localError = error instanceof Error && error.message ? error.message : 'Something went wrong. Try again.' this.render() } } function serverHost(value: string | undefined): string { if (value === undefined) return '' try { return new URL(value).host } catch { return value } } function platformLabel(value: string): string { if (value === 'darwin') return 'macOS' if (value === 'win32') return 'Windows' if (value === 'linux') return 'Linux' return value || 'Computer' } function escapeHtml(value: string): string { return value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''') } function launcherCopy() { if (navigator.language.toLowerCase().startsWith('zh')) return { myMachines: '我的设备', machineHint: '查看所有机器,选择在线机器继续工作', onlineCount: (online: number, total: number) => `${online}/${total} 台在线`, online: '在线', offline: '离线', open: '进入', ready: '可以进入', opening: '正在打开', refresh: '刷新', signOut: '退出', signOutConfirm: '退出插件并清理插件自己的授权?这不会退出 Remote Web。', noMachines: '暂无机器', noMachinesHint: '在远端电脑启动 DeepSeek Harness,并确认 Remote Host 已启用。', lastSeen: (time: string) => `最近在线 ${time.replace(' ago', '前')}`, } return { myMachines: 'My devices', machineHint: 'See every machine and choose one that is online', onlineCount: (online: number, total: number) => `${online}/${total} online`, online: 'Online', offline: 'Offline', open: 'Open', ready: 'Ready to open', opening: 'Opening', refresh: 'Refresh', signOut: 'Sign out', signOutConfirm: 'Sign out of the extension and remove only its authorization? Remote Web stays signed in.', noMachines: 'No machines yet', noMachinesHint: 'Start DeepSeek Harness on the remote computer and make sure Remote Host is enabled.', lastSeen: (time: string) => `Last seen ${time}`, } } function authorizationCopy() { if (navigator.language.toLowerCase().startsWith('zh')) return { title: '一次连接,随时可用。', overview: '登录 Remote Web,查看设备并继续工作。', authorize: '使用 Web 登录', authorizing: '正在连接…', note: '尚未登录时,会先打开 Remote Web;登录后回到插件继续。', } return { title: 'Connect once. Ready whenever you are.', overview: 'Sign in to Remote Web to see your devices and continue working.', authorize: 'Use Web sign-in', authorizing: 'Connecting…', note: 'If needed, sign in to Remote Web first, then return here to continue.', } }