|
| 1 | +import type { DevframeConnectionStatus } from 'devframe/client' |
| 2 | +import { button, cx } from './design' |
| 3 | + |
| 4 | +interface StateCopy { |
| 5 | + icon: string |
| 6 | + title: string |
| 7 | + body: string |
| 8 | +} |
| 9 | + |
| 10 | +const COPY: Record<Exclude<DevframeConnectionStatus, 'connected'>, StateCopy> = { |
| 11 | + connecting: { |
| 12 | + icon: 'i-ph-plugs-connected-duotone', |
| 13 | + title: 'Connecting…', |
| 14 | + body: 'Establishing a connection to the devframe server.', |
| 15 | + }, |
| 16 | + disconnected: { |
| 17 | + icon: 'i-ph-plugs-duotone', |
| 18 | + title: 'Disconnected', |
| 19 | + body: 'Lost the connection to the devframe server. Reload once it is back up.', |
| 20 | + }, |
| 21 | + unauthorized: { |
| 22 | + icon: 'i-ph-lock-key-duotone', |
| 23 | + title: 'Not authorized', |
| 24 | + body: 'Reopen the link printed by your dev server, then reload.', |
| 25 | + }, |
| 26 | + error: { |
| 27 | + icon: 'i-ph-warning-octagon-duotone', |
| 28 | + title: 'Connection failed', |
| 29 | + body: 'Could not reach the devframe server.', |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +export interface ConnectionStateHandle { |
| 34 | + /** Render for a status. Returns `true` when it took over the container. */ |
| 35 | + update: (status: DevframeConnectionStatus, error?: string | null) => boolean |
| 36 | + dispose: () => void |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A full-container connection state, shown whenever the devframe client isn't |
| 41 | + * `connected` so the launcher never sits on an unexplained blank while the |
| 42 | + * socket is down. Reload is the recovery path (no auto-reconnect). |
| 43 | + */ |
| 44 | +export function createConnectionState(container: HTMLElement): ConnectionStateHandle { |
| 45 | + const root = document.createElement('div') |
| 46 | + root.className = 'absolute inset-0 flex flex-col items-center justify-center gap-4 bg-base color-base p-8 text-center z-nav' |
| 47 | + root.hidden = true |
| 48 | + container.append(root) |
| 49 | + |
| 50 | + function update(status: DevframeConnectionStatus, error?: string | null): boolean { |
| 51 | + if (status === 'connected') { |
| 52 | + root.hidden = true |
| 53 | + root.replaceChildren() |
| 54 | + return false |
| 55 | + } |
| 56 | + const copy = COPY[status] |
| 57 | + root.replaceChildren() |
| 58 | + |
| 59 | + const glyph = document.createElement('div') |
| 60 | + glyph.className = cx(copy.icon, 'text-4xl color-active') |
| 61 | + root.append(glyph) |
| 62 | + |
| 63 | + const text = document.createElement('div') |
| 64 | + text.className = 'flex flex-col gap-1' |
| 65 | + const title = document.createElement('p') |
| 66 | + title.className = 'text-lg font-medium' |
| 67 | + title.textContent = copy.title |
| 68 | + const body = document.createElement('p') |
| 69 | + body.className = 'text-sm color-muted max-w-sm' |
| 70 | + body.textContent = copy.body |
| 71 | + text.append(title, body) |
| 72 | + if (error && status === 'error') { |
| 73 | + const code = document.createElement('code') |
| 74 | + code.className = 'mt-1 max-w-sm break-words font-mono text-xs color-faint' |
| 75 | + code.textContent = error |
| 76 | + text.append(code) |
| 77 | + } |
| 78 | + root.append(text) |
| 79 | + |
| 80 | + if (status !== 'connecting') { |
| 81 | + const reload = document.createElement('button') |
| 82 | + reload.type = 'button' |
| 83 | + reload.className = button({ variant: 'primary', size: 'sm' }) |
| 84 | + reload.textContent = 'Reload' |
| 85 | + reload.addEventListener('click', () => location.reload()) |
| 86 | + root.append(reload) |
| 87 | + } |
| 88 | + |
| 89 | + root.hidden = false |
| 90 | + return true |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + update, |
| 95 | + dispose() { |
| 96 | + root.remove() |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
0 commit comments