|
| 1 | +import type { Meta, StoryObj } from '@storybook/html-vite' |
| 2 | +import type { CodeServerViewState } from './view' |
| 3 | +import { createCodeServerView } from './view' |
| 4 | +import './style.css' |
| 5 | + |
| 6 | +// A stand-in for the real code-server iframe so the "running" story renders |
| 7 | +// without a live server. |
| 8 | +const MOCK_EDITOR = `data:text/html;charset=utf-8,${encodeURIComponent(` |
| 9 | +<!doctype html><html><head><meta name="color-scheme" content="dark light" /><style> |
| 10 | + html,body{height:100%;margin:0;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;background:#1e1e1e;color:#d4d4d4} |
| 11 | + .bar{height:35px;background:#333;display:flex;align-items:center;padding:0 12px;font-size:12px;color:#ccc} |
| 12 | + .body{display:flex;height:calc(100% - 35px)} |
| 13 | + .side{width:48px;background:#333} |
| 14 | + .main{flex:1;padding:16px;font-size:13px;line-height:1.6} |
| 15 | + .c{color:#569cd6}.s{color:#ce9178}.f{color:#dcdcaa} |
| 16 | +</style></head><body> |
| 17 | + <div class="bar">code-server — mock editor (Storybook)</div> |
| 18 | + <div class="body"><div class="side"></div><div class="main"> |
| 19 | + <div><span class="c">export function</span> <span class="f">createCodeServerDevframe</span>(<span class="c">options</span>) {</div> |
| 20 | + <div> <span class="c">return</span> <span class="f">defineDevframe</span>({ <span class="s">id</span>, <span class="s">name</span> })</div> |
| 21 | + <div>}</div> |
| 22 | + </div></div> |
| 23 | +</body></html>`)}` |
| 24 | + |
| 25 | +function renderState(state: CodeServerViewState): HTMLElement { |
| 26 | + const container = document.createElement('div') |
| 27 | + container.style.cssText = 'position:relative;width:100%;height:100vh' |
| 28 | + const view = createCodeServerView(container, { |
| 29 | + actions: { |
| 30 | + launch: () => console.warn('[story] launch'), |
| 31 | + recheck: () => console.warn('[story] recheck'), |
| 32 | + }, |
| 33 | + // Keep stories hermetic: never touch real cookies or hosts. |
| 34 | + resolveEditorUrl: () => MOCK_EDITOR, |
| 35 | + applyAuth: () => {}, |
| 36 | + }) |
| 37 | + view.update(state) |
| 38 | + return container |
| 39 | +} |
| 40 | + |
| 41 | +const meta: Meta = { |
| 42 | + title: 'Code Server/Launcher', |
| 43 | + parameters: { layout: 'fullscreen' }, |
| 44 | +} |
| 45 | + |
| 46 | +export default meta |
| 47 | +type Story = StoryObj |
| 48 | + |
| 49 | +/** Awaiting the devframe connection / first status. */ |
| 50 | +export const Connecting: Story = { |
| 51 | + render: () => renderState({ |
| 52 | + detection: { checked: false, installed: false, bin: 'code-server' }, |
| 53 | + server: { status: 'stopped' }, |
| 54 | + }), |
| 55 | +} |
| 56 | + |
| 57 | +/** Binary missing — install instructions and links. */ |
| 58 | +export const NotInstalled: Story = { |
| 59 | + render: () => renderState({ |
| 60 | + detection: { checked: true, installed: false, bin: 'code-server' }, |
| 61 | + server: { status: 'stopped' }, |
| 62 | + }), |
| 63 | +} |
| 64 | + |
| 65 | +/** Installed and idle — the launch screen. */ |
| 66 | +export const Launch: Story = { |
| 67 | + render: () => renderState({ |
| 68 | + detection: { checked: true, installed: true, version: '4.99.0', bin: 'code-server' }, |
| 69 | + server: { status: 'stopped' }, |
| 70 | + }), |
| 71 | +} |
| 72 | + |
| 73 | +/** A previous launch failed — the error surfaces above the launch button. */ |
| 74 | +export const LaunchError: Story = { |
| 75 | + render: () => renderState({ |
| 76 | + detection: { checked: true, installed: true, version: '4.99.0', bin: 'code-server' }, |
| 77 | + server: { status: 'error', error: 'Failed to spawn code-server: EADDRINUSE 127.0.0.1:8080' }, |
| 78 | + }), |
| 79 | +} |
| 80 | + |
| 81 | +/** Spawned, waiting on the readiness probe (and the auth handoff). */ |
| 82 | +export const Starting: Story = { |
| 83 | + render: () => renderState({ |
| 84 | + detection: { checked: true, installed: true, version: '4.99.0', bin: 'code-server' }, |
| 85 | + server: { status: 'starting', port: 8080 }, |
| 86 | + busy: true, |
| 87 | + }), |
| 88 | +} |
| 89 | + |
| 90 | +/** Ready — the editor fills the panel, signed in, with no chrome over it. */ |
| 91 | +export const Running: Story = { |
| 92 | + render: () => renderState({ |
| 93 | + detection: { checked: true, installed: true, version: '4.99.0', bin: 'code-server' }, |
| 94 | + server: { status: 'running', port: 8080, pid: 4242 }, |
| 95 | + auth: { cookieName: 'code-server-session', cookieValue: 'a'.repeat(64) }, |
| 96 | + }), |
| 97 | +} |
0 commit comments