@bakapiano/ccsm 0.10.3 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CLAUDE.md +475 -475
  2. package/README.md +190 -190
  3. package/bin/ccsm.js +194 -194
  4. package/lib/cliSessionWatcher.js +249 -249
  5. package/lib/config.js +185 -185
  6. package/lib/folders.js +96 -96
  7. package/lib/localCliSessions.js +489 -177
  8. package/lib/persistedSessions.js +134 -134
  9. package/lib/webTerminal.js +208 -208
  10. package/lib/workspace.js +230 -255
  11. package/package.json +57 -57
  12. package/public/css/base.css +99 -99
  13. package/public/css/cards.css +183 -183
  14. package/public/css/feedback.css +303 -303
  15. package/public/css/forms.css +405 -405
  16. package/public/css/layout.css +160 -160
  17. package/public/css/modal.css +190 -183
  18. package/public/css/responsive.css +10 -10
  19. package/public/css/sidebar.css +616 -601
  20. package/public/css/terminals.css +294 -294
  21. package/public/css/tokens.css +81 -79
  22. package/public/css/wco.css +98 -98
  23. package/public/css/widgets.css +1596 -1375
  24. package/public/index.html +105 -103
  25. package/public/js/api.js +272 -260
  26. package/public/js/components/AdoptModal.js +343 -171
  27. package/public/js/components/App.js +35 -35
  28. package/public/js/components/DirectoryPicker.js +203 -203
  29. package/public/js/components/EntityFormModal.js +105 -105
  30. package/public/js/components/Modal.js +51 -51
  31. package/public/js/components/OfflineBanner.js +93 -93
  32. package/public/js/components/PageTitleBar.js +13 -13
  33. package/public/js/components/Picker.js +179 -179
  34. package/public/js/components/Popover.js +55 -55
  35. package/public/js/components/Sidebar.js +270 -270
  36. package/public/js/components/TerminalView.js +298 -298
  37. package/public/js/components/useDragSort.js +67 -67
  38. package/public/js/dialog.js +67 -67
  39. package/public/js/icons.js +177 -177
  40. package/public/js/main.js +140 -140
  41. package/public/js/pages/AboutPage.js +165 -165
  42. package/public/js/pages/ConfigurePage.js +475 -487
  43. package/public/js/pages/LaunchPage.js +369 -369
  44. package/public/js/pages/SessionsPage.js +97 -97
  45. package/public/js/state.js +231 -231
  46. package/public/manifest.webmanifest +15 -15
  47. package/scripts/install.js +137 -137
  48. package/server.js +1126 -1117
@@ -1,203 +1,203 @@
1
- // Directory browser used by Launch page. Windows-Explorer-style:
2
- // ┌───────────────────────────────────────────────┐
3
- // │ [←] [→] [↑] Home > Users > Admin > foo [✎] │ nav + breadcrumb
4
- // ├──────────────┬────────────────────────────────┤
5
- // │ Quick access │ folder rows (large, hoverable)│
6
- // │ Home │ ⌃ .. │
7
- // │ Work dir │ 📁 AppData │
8
- // │ C:\ │ 📁 ccsm-workspaces │
9
- // │ D:\ │ │
10
- // └──────────────┴────────────────────────────────┘
11
- //
12
- // The picker streams the currently-selected path back to the parent via
13
- // `onPick(path)` on every selection change. Confirm/cancel UI lives in
14
- // the parent (the workdir modal's shared footer) so auto + cwd modes
15
- // share one CTA.
16
- //
17
- // Props: { initialPath, onPick }
18
-
19
- import { html } from '../html.js';
20
- import { useEffect, useState } from 'preact/hooks';
21
- import { api } from '../api.js';
22
- import { setToast } from '../toast.js';
23
- import { IconFolder, IconHome, IconChevronLeft, IconChevronRight, IconChevronUp, IconPencil } from '../icons.js';
24
-
25
- export function DirectoryPicker({ initialPath, onPick }) {
26
- const [data, setData] = useState(null);
27
- const [path, setPath] = useState(initialPath || '');
28
- const [history, setHistory] = useState({ stack: [], cursor: -1 }); // back/forward
29
- const [editing, setEditing] = useState(false);
30
- const [input, setInput] = useState('');
31
- const [loading, setLoading] = useState(false);
32
-
33
- // Push the current selection up on every change so the parent's
34
- // shared "Use folder" CTA can act on it.
35
- const select = (p) => { setPath(p); setInput(p); onPick?.(p); };
36
-
37
- const browse = async (p, { pushHistory = true } = {}) => {
38
- setLoading(true);
39
- try {
40
- const url = '/api/browse' + (p ? `?path=${encodeURIComponent(p)}` : '');
41
- const r = await api('GET', url);
42
- setData(r);
43
- select(r.path);
44
- if (pushHistory) {
45
- setHistory((h) => {
46
- const head = h.stack.slice(0, h.cursor + 1);
47
- head.push(r.path);
48
- return { stack: head, cursor: head.length - 1 };
49
- });
50
- }
51
- } catch (e) { setToast(e.message, 'error'); }
52
- finally { setLoading(false); }
53
- };
54
-
55
- useEffect(() => { browse(initialPath); }, []);
56
-
57
- const canBack = history.cursor > 0;
58
- const canForward = history.cursor >= 0 && history.cursor < history.stack.length - 1;
59
-
60
- const goBack = () => {
61
- if (!canBack) return;
62
- const idx = history.cursor - 1;
63
- setHistory({ ...history, cursor: idx });
64
- browse(history.stack[idx], { pushHistory: false });
65
- };
66
- const goForward = () => {
67
- if (!canForward) return;
68
- const idx = history.cursor + 1;
69
- setHistory({ ...history, cursor: idx });
70
- browse(history.stack[idx], { pushHistory: false });
71
- };
72
- const goUp = () => {
73
- if (!data?.parent) return;
74
- browse(data.parent);
75
- };
76
- const onAddressSubmit = (ev) => {
77
- ev?.preventDefault?.();
78
- const p = (input || '').trim();
79
- setEditing(false);
80
- if (p && p !== path) browse(p);
81
- };
82
-
83
- if (!data) {
84
- return html`<div class="filex"><div class="filex-loading">Loading…</div></div>`;
85
- }
86
-
87
- // Build breadcrumb segments. Windows-style: "C:\Users\Admin\foo" →
88
- // [C:, Users, Admin, foo] with each segment clickable.
89
- const segments = breadcrumbSegments(data.path);
90
-
91
- return html`
92
- <div class="filex">
93
- <div class="filex-toolbar">
94
- <div class="filex-navbtns">
95
- <button class="filex-navbtn" title="Back" disabled=${!canBack} onClick=${goBack}>
96
- <${IconChevronLeft} />
97
- </button>
98
- <button class="filex-navbtn" title="Forward" disabled=${!canForward} onClick=${goForward}>
99
- <${IconChevronRight} />
100
- </button>
101
- <button class="filex-navbtn" title="Up" disabled=${!data.parent} onClick=${goUp}>
102
- <${IconChevronUp} />
103
- </button>
104
- </div>
105
-
106
- ${editing ? html`
107
- <form class="filex-address-edit" onSubmit=${onAddressSubmit}>
108
- <input class="filex-address-input mono" value=${input}
109
- autoFocus
110
- onInput=${(e) => setInput(e.target.value)}
111
- onBlur=${onAddressSubmit}
112
- onKeyDown=${(e) => { if (e.key === 'Escape') { setInput(data.path); setEditing(false); } }}
113
- spellcheck="false" />
114
- </form>
115
- ` : html`
116
- <div class="filex-breadcrumb"
117
- onClick=${(e) => { if (e.target === e.currentTarget) { setInput(data.path); setEditing(true); } }}>
118
- ${segments.map((seg, i) => html`
119
- <button key=${seg.path} class="filex-crumb"
120
- title=${seg.path}
121
- onClick=${() => browse(seg.path)}>
122
- ${i === 0 && seg.label.match(/^[a-z]:\\?$/i) ? null : null}
123
- ${seg.label}
124
- </button>
125
- ${i < segments.length - 1
126
- ? html`<span class="filex-crumb-sep" aria-hidden="true">›</span>`
127
- : null}
128
- `)}
129
- <button class="filex-address-edit-btn" title="Edit path"
130
- onClick=${() => { setInput(data.path); setEditing(true); }}>
131
- <${IconPencil} />
132
- </button>
133
- </div>
134
- `}
135
- </div>
136
-
137
- <div class="filex-body">
138
- <aside class="filex-side">
139
- <div class="filex-side-label">Quick access</div>
140
- ${(data.starts || []).map((s) => html`
141
- <button key=${s.path} class=${`filex-side-item${path === s.path ? ' is-active' : ''}`}
142
- onClick=${() => browse(s.path)}
143
- title=${s.path}>
144
- <span class="filex-side-icon">
145
- ${s.label === 'Home' ? html`<${IconHome} />` : html`<${IconFolder} />`}
146
- </span>
147
- <span class="filex-side-name">${s.label}</span>
148
- </button>`)}
149
- </aside>
150
-
151
- <div class="filex-main">
152
- ${!data.exists ? html`
153
- <div class="filex-empty">Directory not found.</div>
154
- ` : html`
155
- <div class="filex-list">
156
- ${data.entries.length === 0 ? html`
157
- <div class="filex-empty">This folder is empty.</div>
158
- ` : data.entries.map((e) => html`
159
- <button key=${e.path} class="filex-row"
160
- onDblClick=${() => browse(e.path)}
161
- onClick=${() => select(e.path)}
162
- data-active=${path === e.path}>
163
- <span class="filex-row-icon"><${IconFolder} /></span>
164
- <span class="filex-row-name">${e.name}</span>
165
- </button>`)}
166
- </div>`}
167
- </div>
168
- </div>
169
-
170
- <div class="filex-foot">
171
- <span class="filex-foot-current mono" title=${path}>${path || ' '}</span>
172
- </div>
173
- </div>`;
174
- }
175
-
176
- // "C:\Users\Admin\foo" → [{label:'C:', path:'C:\\'}, {label:'Users', path:'C:\\Users'}, …]
177
- // "/home/me/foo" → [{label:'/', path:'/'}, {label:'home', path:'/home'}, …]
178
- function breadcrumbSegments(p) {
179
- if (!p) return [];
180
- // Windows: split on backslash. Posix: forward slash.
181
- const isWin = /^[a-zA-Z]:[\\/]/.test(p);
182
- const sep = isWin ? '\\' : '/';
183
- const norm = p.replace(/[\\\/]+/g, sep);
184
- const parts = norm.split(sep).filter(Boolean);
185
- const segs = [];
186
- if (isWin) {
187
- // first part is drive like "C:"
188
- let acc = parts[0] + sep;
189
- segs.push({ label: parts[0], path: acc });
190
- for (let i = 1; i < parts.length; i++) {
191
- acc = (acc.endsWith(sep) ? acc.slice(0, -1) : acc) + sep + parts[i];
192
- segs.push({ label: parts[i], path: acc });
193
- }
194
- } else {
195
- let acc = '';
196
- segs.push({ label: '/', path: '/' });
197
- for (const part of parts) {
198
- acc = acc + sep + part;
199
- segs.push({ label: part, path: acc });
200
- }
201
- }
202
- return segs;
203
- }
1
+ // Directory browser used by Launch page. Windows-Explorer-style:
2
+ // ┌───────────────────────────────────────────────┐
3
+ // │ [←] [→] [↑] Home > Users > Admin > foo [✎] │ nav + breadcrumb
4
+ // ├──────────────┬────────────────────────────────┤
5
+ // │ Quick access │ folder rows (large, hoverable)│
6
+ // │ Home │ ⌃ .. │
7
+ // │ Work dir │ 📁 AppData │
8
+ // │ C:\ │ 📁 ccsm-workspaces │
9
+ // │ D:\ │ │
10
+ // └──────────────┴────────────────────────────────┘
11
+ //
12
+ // The picker streams the currently-selected path back to the parent via
13
+ // `onPick(path)` on every selection change. Confirm/cancel UI lives in
14
+ // the parent (the workdir modal's shared footer) so auto + cwd modes
15
+ // share one CTA.
16
+ //
17
+ // Props: { initialPath, onPick }
18
+
19
+ import { html } from '../html.js';
20
+ import { useEffect, useState } from 'preact/hooks';
21
+ import { api } from '../api.js';
22
+ import { setToast } from '../toast.js';
23
+ import { IconFolder, IconHome, IconChevronLeft, IconChevronRight, IconChevronUp, IconPencil } from '../icons.js';
24
+
25
+ export function DirectoryPicker({ initialPath, onPick }) {
26
+ const [data, setData] = useState(null);
27
+ const [path, setPath] = useState(initialPath || '');
28
+ const [history, setHistory] = useState({ stack: [], cursor: -1 }); // back/forward
29
+ const [editing, setEditing] = useState(false);
30
+ const [input, setInput] = useState('');
31
+ const [loading, setLoading] = useState(false);
32
+
33
+ // Push the current selection up on every change so the parent's
34
+ // shared "Use folder" CTA can act on it.
35
+ const select = (p) => { setPath(p); setInput(p); onPick?.(p); };
36
+
37
+ const browse = async (p, { pushHistory = true } = {}) => {
38
+ setLoading(true);
39
+ try {
40
+ const url = '/api/browse' + (p ? `?path=${encodeURIComponent(p)}` : '');
41
+ const r = await api('GET', url);
42
+ setData(r);
43
+ select(r.path);
44
+ if (pushHistory) {
45
+ setHistory((h) => {
46
+ const head = h.stack.slice(0, h.cursor + 1);
47
+ head.push(r.path);
48
+ return { stack: head, cursor: head.length - 1 };
49
+ });
50
+ }
51
+ } catch (e) { setToast(e.message, 'error'); }
52
+ finally { setLoading(false); }
53
+ };
54
+
55
+ useEffect(() => { browse(initialPath); }, []);
56
+
57
+ const canBack = history.cursor > 0;
58
+ const canForward = history.cursor >= 0 && history.cursor < history.stack.length - 1;
59
+
60
+ const goBack = () => {
61
+ if (!canBack) return;
62
+ const idx = history.cursor - 1;
63
+ setHistory({ ...history, cursor: idx });
64
+ browse(history.stack[idx], { pushHistory: false });
65
+ };
66
+ const goForward = () => {
67
+ if (!canForward) return;
68
+ const idx = history.cursor + 1;
69
+ setHistory({ ...history, cursor: idx });
70
+ browse(history.stack[idx], { pushHistory: false });
71
+ };
72
+ const goUp = () => {
73
+ if (!data?.parent) return;
74
+ browse(data.parent);
75
+ };
76
+ const onAddressSubmit = (ev) => {
77
+ ev?.preventDefault?.();
78
+ const p = (input || '').trim();
79
+ setEditing(false);
80
+ if (p && p !== path) browse(p);
81
+ };
82
+
83
+ if (!data) {
84
+ return html`<div class="filex"><div class="filex-loading">Loading…</div></div>`;
85
+ }
86
+
87
+ // Build breadcrumb segments. Windows-style: "C:\Users\Admin\foo" →
88
+ // [C:, Users, Admin, foo] with each segment clickable.
89
+ const segments = breadcrumbSegments(data.path);
90
+
91
+ return html`
92
+ <div class="filex">
93
+ <div class="filex-toolbar">
94
+ <div class="filex-navbtns">
95
+ <button class="filex-navbtn" title="Back" disabled=${!canBack} onClick=${goBack}>
96
+ <${IconChevronLeft} />
97
+ </button>
98
+ <button class="filex-navbtn" title="Forward" disabled=${!canForward} onClick=${goForward}>
99
+ <${IconChevronRight} />
100
+ </button>
101
+ <button class="filex-navbtn" title="Up" disabled=${!data.parent} onClick=${goUp}>
102
+ <${IconChevronUp} />
103
+ </button>
104
+ </div>
105
+
106
+ ${editing ? html`
107
+ <form class="filex-address-edit" onSubmit=${onAddressSubmit}>
108
+ <input class="filex-address-input mono" value=${input}
109
+ autoFocus
110
+ onInput=${(e) => setInput(e.target.value)}
111
+ onBlur=${onAddressSubmit}
112
+ onKeyDown=${(e) => { if (e.key === 'Escape') { setInput(data.path); setEditing(false); } }}
113
+ spellcheck="false" />
114
+ </form>
115
+ ` : html`
116
+ <div class="filex-breadcrumb"
117
+ onClick=${(e) => { if (e.target === e.currentTarget) { setInput(data.path); setEditing(true); } }}>
118
+ ${segments.map((seg, i) => html`
119
+ <button key=${seg.path} class="filex-crumb"
120
+ title=${seg.path}
121
+ onClick=${() => browse(seg.path)}>
122
+ ${i === 0 && seg.label.match(/^[a-z]:\\?$/i) ? null : null}
123
+ ${seg.label}
124
+ </button>
125
+ ${i < segments.length - 1
126
+ ? html`<span class="filex-crumb-sep" aria-hidden="true">›</span>`
127
+ : null}
128
+ `)}
129
+ <button class="filex-address-edit-btn" title="Edit path"
130
+ onClick=${() => { setInput(data.path); setEditing(true); }}>
131
+ <${IconPencil} />
132
+ </button>
133
+ </div>
134
+ `}
135
+ </div>
136
+
137
+ <div class="filex-body">
138
+ <aside class="filex-side">
139
+ <div class="filex-side-label">Quick access</div>
140
+ ${(data.starts || []).map((s) => html`
141
+ <button key=${s.path} class=${`filex-side-item${path === s.path ? ' is-active' : ''}`}
142
+ onClick=${() => browse(s.path)}
143
+ title=${s.path}>
144
+ <span class="filex-side-icon">
145
+ ${s.label === 'Home' ? html`<${IconHome} />` : html`<${IconFolder} />`}
146
+ </span>
147
+ <span class="filex-side-name">${s.label}</span>
148
+ </button>`)}
149
+ </aside>
150
+
151
+ <div class="filex-main">
152
+ ${!data.exists ? html`
153
+ <div class="filex-empty">Directory not found.</div>
154
+ ` : html`
155
+ <div class="filex-list">
156
+ ${data.entries.length === 0 ? html`
157
+ <div class="filex-empty">This folder is empty.</div>
158
+ ` : data.entries.map((e) => html`
159
+ <button key=${e.path} class="filex-row"
160
+ onDblClick=${() => browse(e.path)}
161
+ onClick=${() => select(e.path)}
162
+ data-active=${path === e.path}>
163
+ <span class="filex-row-icon"><${IconFolder} /></span>
164
+ <span class="filex-row-name">${e.name}</span>
165
+ </button>`)}
166
+ </div>`}
167
+ </div>
168
+ </div>
169
+
170
+ <div class="filex-foot">
171
+ <span class="filex-foot-current mono" title=${path}>${path || ' '}</span>
172
+ </div>
173
+ </div>`;
174
+ }
175
+
176
+ // "C:\Users\Admin\foo" → [{label:'C:', path:'C:\\'}, {label:'Users', path:'C:\\Users'}, …]
177
+ // "/home/me/foo" → [{label:'/', path:'/'}, {label:'home', path:'/home'}, …]
178
+ function breadcrumbSegments(p) {
179
+ if (!p) return [];
180
+ // Windows: split on backslash. Posix: forward slash.
181
+ const isWin = /^[a-zA-Z]:[\\/]/.test(p);
182
+ const sep = isWin ? '\\' : '/';
183
+ const norm = p.replace(/[\\\/]+/g, sep);
184
+ const parts = norm.split(sep).filter(Boolean);
185
+ const segs = [];
186
+ if (isWin) {
187
+ // first part is drive like "C:"
188
+ let acc = parts[0] + sep;
189
+ segs.push({ label: parts[0], path: acc });
190
+ for (let i = 1; i < parts.length; i++) {
191
+ acc = (acc.endsWith(sep) ? acc.slice(0, -1) : acc) + sep + parts[i];
192
+ segs.push({ label: parts[i], path: acc });
193
+ }
194
+ } else {
195
+ let acc = '';
196
+ segs.push({ label: '/', path: '/' });
197
+ for (const part of parts) {
198
+ acc = acc + sep + part;
199
+ segs.push({ label: part, path: acc });
200
+ }
201
+ }
202
+ return segs;
203
+ }
@@ -1,105 +1,105 @@
1
- // Generic create/edit form rendered inside a Modal. Field shape mirrors
2
- // the createFields prop used by Picker.js so the same field-definition
3
- // objects power both the inline-create-in-popover flow and the standalone
4
- // edit flow used from Configure.
5
- //
6
- // Usage:
7
- // <${EntityFormModal}
8
- // title="Edit CLI"
9
- // fields=${cliFields}
10
- // initial=${currentValues}
11
- // onSubmit=${async (values) => { ... }}
12
- // onClose=${close} />
13
-
14
- import { html } from '../html.js';
15
- import { useState } from 'preact/hooks';
16
- import { Modal } from './Modal.js';
17
-
18
- export function EntityFormModal({
19
- title, fields, initial = {}, submitLabel = 'Save',
20
- readOnlyKeys = [],
21
- onSubmit, onClose, danger,
22
- }) {
23
- const [draft, setDraft] = useState(() => ({ ...initialFrom(fields), ...initial }));
24
- const [saving, setSaving] = useState(false);
25
-
26
- const isReadOnly = (key) => readOnlyKeys.includes(key);
27
-
28
- const submit = async (ev) => {
29
- ev?.preventDefault?.();
30
- for (const f of fields) {
31
- if (f.required && !String(draft[f.key] || '').trim()) return;
32
- }
33
- setSaving(true);
34
- try { await onSubmit?.(draft); onClose?.(); }
35
- catch { /* caller toasts; stay open */ }
36
- finally { setSaving(false); }
37
- };
38
-
39
- return html`
40
- <${Modal} title=${title} onClose=${onClose} width=${440}>
41
- <form class="entity-form" onSubmit=${submit}>
42
- ${fields.map((f) => html`
43
- <label class="entity-field" key=${f.key}>
44
- <span class="entity-field-label">${f.label}</span>
45
- ${f.type === 'select' ? html`
46
- <select class="input" value=${draft[f.key] || ''}
47
- disabled=${isReadOnly(f.key)}
48
- onChange=${(e) => {
49
- const next = { ...draft, [f.key]: e.target.value };
50
- const sideEffects = f.onChange?.(e.target.value, next);
51
- setDraft(sideEffects ? { ...next, ...sideEffects } : next);
52
- }}>
53
- ${(f.options || []).map((opt) => html`
54
- <option value=${opt.value}>${opt.label}</option>`)}
55
- </select>
56
- ` : f.type === 'iconRadio' ? html`
57
- <div class=${`icon-radio${isReadOnly(f.key) ? ' is-disabled' : ''}`}>
58
- ${(f.options || []).map((opt) => html`
59
- <button type="button" key=${opt.value}
60
- class=${`icon-radio-opt${draft[f.key] === opt.value ? ' is-active' : ''}`}
61
- disabled=${isReadOnly(f.key)}
62
- onClick=${() => {
63
- if (isReadOnly(f.key)) return;
64
- const next = { ...draft, [f.key]: opt.value };
65
- const sideEffects = f.onChange?.(opt.value, next);
66
- setDraft(sideEffects ? { ...next, ...sideEffects } : next);
67
- }}>
68
- ${opt.icon ? html`<span class="icon-radio-icon">${opt.icon}</span>` : null}
69
- <span>${opt.label}</span>
70
- </button>`)}
71
- </div>
72
- ` : f.type === 'checkbox' ? html`
73
- <span class="entity-checkbox-row">
74
- <input type="checkbox" checked=${!!draft[f.key]}
75
- disabled=${isReadOnly(f.key)}
76
- onChange=${(e) => setDraft({ ...draft, [f.key]: e.target.checked })} />
77
- ${f.hint ? html`<span class="entity-field-hint">${f.hint}</span>` : null}
78
- </span>
79
- ` : html`
80
- <input type=${f.type || 'text'}
81
- class=${`input${f.mono ? ' mono' : ''}`}
82
- placeholder=${f.placeholder || ''}
83
- value=${draft[f.key] || ''}
84
- readonly=${isReadOnly(f.key)}
85
- onInput=${(e) => setDraft({ ...draft, [f.key]: e.target.value })}
86
- autoFocus=${f.autoFocus && !isReadOnly(f.key)} />`}
87
- ${f.hint && f.type !== 'checkbox' ? html`
88
- <span class="entity-field-hint">${f.hint}</span>` : null}
89
- </label>`)}
90
- <div class="entity-form-actions">
91
- <button type="button" class="action small subtle" onClick=${onClose}>Cancel</button>
92
- <button type="submit" class=${`action small ${danger ? 'danger' : 'primary'}`}
93
- disabled=${saving}>
94
- ${saving ? 'Saving…' : submitLabel}
95
- </button>
96
- </div>
97
- </form>
98
- </${Modal}>`;
99
- }
100
-
101
- function initialFrom(fields) {
102
- const out = {};
103
- for (const f of fields) out[f.key] = f.default ?? '';
104
- return out;
105
- }
1
+ // Generic create/edit form rendered inside a Modal. Field shape mirrors
2
+ // the createFields prop used by Picker.js so the same field-definition
3
+ // objects power both the inline-create-in-popover flow and the standalone
4
+ // edit flow used from Configure.
5
+ //
6
+ // Usage:
7
+ // <${EntityFormModal}
8
+ // title="Edit CLI"
9
+ // fields=${cliFields}
10
+ // initial=${currentValues}
11
+ // onSubmit=${async (values) => { ... }}
12
+ // onClose=${close} />
13
+
14
+ import { html } from '../html.js';
15
+ import { useState } from 'preact/hooks';
16
+ import { Modal } from './Modal.js';
17
+
18
+ export function EntityFormModal({
19
+ title, fields, initial = {}, submitLabel = 'Save',
20
+ readOnlyKeys = [],
21
+ onSubmit, onClose, danger,
22
+ }) {
23
+ const [draft, setDraft] = useState(() => ({ ...initialFrom(fields), ...initial }));
24
+ const [saving, setSaving] = useState(false);
25
+
26
+ const isReadOnly = (key) => readOnlyKeys.includes(key);
27
+
28
+ const submit = async (ev) => {
29
+ ev?.preventDefault?.();
30
+ for (const f of fields) {
31
+ if (f.required && !String(draft[f.key] || '').trim()) return;
32
+ }
33
+ setSaving(true);
34
+ try { await onSubmit?.(draft); onClose?.(); }
35
+ catch { /* caller toasts; stay open */ }
36
+ finally { setSaving(false); }
37
+ };
38
+
39
+ return html`
40
+ <${Modal} title=${title} onClose=${onClose} width=${440}>
41
+ <form class="entity-form" onSubmit=${submit}>
42
+ ${fields.map((f) => html`
43
+ <label class="entity-field" key=${f.key}>
44
+ <span class="entity-field-label">${f.label}</span>
45
+ ${f.type === 'select' ? html`
46
+ <select class="input" value=${draft[f.key] || ''}
47
+ disabled=${isReadOnly(f.key)}
48
+ onChange=${(e) => {
49
+ const next = { ...draft, [f.key]: e.target.value };
50
+ const sideEffects = f.onChange?.(e.target.value, next);
51
+ setDraft(sideEffects ? { ...next, ...sideEffects } : next);
52
+ }}>
53
+ ${(f.options || []).map((opt) => html`
54
+ <option value=${opt.value}>${opt.label}</option>`)}
55
+ </select>
56
+ ` : f.type === 'iconRadio' ? html`
57
+ <div class=${`icon-radio${isReadOnly(f.key) ? ' is-disabled' : ''}`}>
58
+ ${(f.options || []).map((opt) => html`
59
+ <button type="button" key=${opt.value}
60
+ class=${`icon-radio-opt${draft[f.key] === opt.value ? ' is-active' : ''}`}
61
+ disabled=${isReadOnly(f.key)}
62
+ onClick=${() => {
63
+ if (isReadOnly(f.key)) return;
64
+ const next = { ...draft, [f.key]: opt.value };
65
+ const sideEffects = f.onChange?.(opt.value, next);
66
+ setDraft(sideEffects ? { ...next, ...sideEffects } : next);
67
+ }}>
68
+ ${opt.icon ? html`<span class="icon-radio-icon">${opt.icon}</span>` : null}
69
+ <span>${opt.label}</span>
70
+ </button>`)}
71
+ </div>
72
+ ` : f.type === 'checkbox' ? html`
73
+ <span class="entity-checkbox-row">
74
+ <input type="checkbox" checked=${!!draft[f.key]}
75
+ disabled=${isReadOnly(f.key)}
76
+ onChange=${(e) => setDraft({ ...draft, [f.key]: e.target.checked })} />
77
+ ${f.hint ? html`<span class="entity-field-hint">${f.hint}</span>` : null}
78
+ </span>
79
+ ` : html`
80
+ <input type=${f.type || 'text'}
81
+ class=${`input${f.mono ? ' mono' : ''}`}
82
+ placeholder=${f.placeholder || ''}
83
+ value=${draft[f.key] || ''}
84
+ readonly=${isReadOnly(f.key)}
85
+ onInput=${(e) => setDraft({ ...draft, [f.key]: e.target.value })}
86
+ autoFocus=${f.autoFocus && !isReadOnly(f.key)} />`}
87
+ ${f.hint && f.type !== 'checkbox' ? html`
88
+ <span class="entity-field-hint">${f.hint}</span>` : null}
89
+ </label>`)}
90
+ <div class="entity-form-actions">
91
+ <button type="button" class="action small subtle" onClick=${onClose}>Cancel</button>
92
+ <button type="submit" class=${`action small ${danger ? 'danger' : 'primary'}`}
93
+ disabled=${saving}>
94
+ ${saving ? 'Saving…' : submitLabel}
95
+ </button>
96
+ </div>
97
+ </form>
98
+ </${Modal}>`;
99
+ }
100
+
101
+ function initialFrom(fields) {
102
+ const out = {};
103
+ for (const f of fields) out[f.key] = f.default ?? '';
104
+ return out;
105
+ }