reifyui 0.3.0 → 0.4.1
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.
- package/README.md +59 -10
- package/dist/index.d.ts +93 -22
- package/dist/slides.cjs +737 -0
- package/dist/slides.cjs.map +1 -0
- package/dist/slides.d.ts +128 -0
- package/dist/slides.js +705 -0
- package/dist/slides.js.map +1 -0
- package/package.json +10 -3
- package/src/auth/AuthForm.jsx +187 -0
- package/src/auth/GoogleButton.jsx +80 -0
- package/src/auth/client.js +177 -0
- package/src/components/ChatMessages.jsx +194 -0
- package/src/components/CodeBlock.jsx +120 -0
- package/src/components/Composer.jsx +75 -0
- package/src/components/Dialog.jsx +312 -0
- package/src/components/TaskList.jsx +193 -0
- package/src/components/ToolSteps.jsx +84 -0
- package/src/components/icons.jsx +33 -0
- package/src/components/resizable.jsx +84 -0
- package/src/components/toolMeta.js +91 -0
- package/src/index.d.ts +234 -0
- package/src/index.js +35 -0
- package/src/sheet/SheetGrid.jsx +633 -0
- package/src/slides/Editor.jsx +246 -0
- package/src/slides/Presentation.jsx +79 -0
- package/src/slides/SlideView.jsx +97 -0
- package/src/slides/elements.jsx +178 -0
- package/src/slides/index.d.ts +128 -0
- package/src/slides/index.js +10 -0
- package/src/state/blocks.js +37 -0
- package/src/state/conversationStore.js +55 -0
- package/src/stream/responses.js +122 -0
- package/styles/slides.css +38 -0
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
// SheetGrid — the reusable AI-spreadsheet grid (UI Core).
|
|
2
|
+
//
|
|
3
|
+
// Product-agnostic: it renders a sheet JSON and calls back for edits/runs.
|
|
4
|
+
// The Sheets product (frontend-sheets) AND agentstudio studio Spaces both
|
|
5
|
+
// mount it via @ui-core, so a Space can nest a sheet-typed element with the
|
|
6
|
+
// same component. All I/O is injected — this file has no product API.
|
|
7
|
+
//
|
|
8
|
+
// Sheet shape (see backend/sheets sheet_model):
|
|
9
|
+
// { meta{title,rev}, columns[{id,name,type,options?,compute?}],
|
|
10
|
+
// rows[{id}], cells{ "<rowId>:<colId>": {value,status,error} } }
|
|
11
|
+
//
|
|
12
|
+
// Props:
|
|
13
|
+
// sheet — the sheet JSON (required)
|
|
14
|
+
// onChange(next) — persist an edited sheet (add/edit/delete col/row/cell)
|
|
15
|
+
// onRunCell(rowId, colId) — run one computed cell (optional)
|
|
16
|
+
// onRunColumn(colId) — batch-run a computed column (optional)
|
|
17
|
+
// readOnly — no editing/running (template previews / embedded read views)
|
|
18
|
+
// fetchBlobUrl(path) — async product-authed fetch -> object URL, for
|
|
19
|
+
// inline images whose url is a product path
|
|
20
|
+
// onOpenResource(ref) — open a referenced artifact (sheet/slides/…)
|
|
21
|
+
//
|
|
22
|
+
// Editing is optimistic: local edits call onChange with the next sheet; the
|
|
23
|
+
// host persists + (via realtime) rebroadcasts truth back through `sheet`.
|
|
24
|
+
import { useEffect, useRef, useState } from 'react';
|
|
25
|
+
import { createPortal } from 'react-dom';
|
|
26
|
+
|
|
27
|
+
const cellKey = (rowId, colId) => `${rowId}:${colId}`;
|
|
28
|
+
const uid = (p) => `${p}_${Math.random().toString(36).slice(2, 10)}`;
|
|
29
|
+
|
|
30
|
+
const TYPE_LABEL = {
|
|
31
|
+
text: 'Text', number: 'Number', select: 'Select', tags: 'Tags',
|
|
32
|
+
checkbox: 'Checkbox', date: 'Date', url: 'Link', resource: 'Resource', compute: 'Computed',
|
|
33
|
+
};
|
|
34
|
+
const MANUAL_TYPES = ['text', 'number', 'select', 'tags', 'checkbox', 'date', 'url', 'resource'];
|
|
35
|
+
const COMPUTE_KINDS = [
|
|
36
|
+
{ kind: 'prompt', label: 'AI prompt' },
|
|
37
|
+
{ kind: 'harness', label: 'Agent (harness)' },
|
|
38
|
+
{ kind: 'workflow', label: 'Workflow' },
|
|
39
|
+
{ kind: 'image', label: 'Generate image' },
|
|
40
|
+
];
|
|
41
|
+
const RESOURCE_KINDS = ['image', 'sheet', 'slides', 'workflow', 'graph'];
|
|
42
|
+
const RESOURCE_ICON = { image: '🖼', sheet: '▦', slides: '▤', workflow: '⚙', graph: '◉' };
|
|
43
|
+
|
|
44
|
+
// Popovers render through a body portal with FIXED positioning — inside the
|
|
45
|
+
// grid they'd be clipped by the scroll container and out-stacked by the
|
|
46
|
+
// sticky header/row-number cells. anchor is a DOMRect captured at open time;
|
|
47
|
+
// the box clamps to the viewport and flips above the anchor when the bottom
|
|
48
|
+
// would overflow. Any scroll closes it (the anchor moved).
|
|
49
|
+
function PopPortal({ anchor, width = 250, estHeight = 300, className, children, onClose }) {
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!onClose) return undefined;
|
|
52
|
+
const close = () => onClose();
|
|
53
|
+
window.addEventListener('scroll', close, true);
|
|
54
|
+
window.addEventListener('resize', close);
|
|
55
|
+
return () => { window.removeEventListener('scroll', close, true); window.removeEventListener('resize', close); };
|
|
56
|
+
}, [onClose]);
|
|
57
|
+
if (!anchor) return null;
|
|
58
|
+
const left = Math.max(8, Math.min(anchor.right - width, window.innerWidth - width - 8));
|
|
59
|
+
let top = anchor.bottom + 6;
|
|
60
|
+
if (top + estHeight > window.innerHeight - 8) top = Math.max(8, anchor.top - estHeight - 6);
|
|
61
|
+
return createPortal(
|
|
62
|
+
<div className={className} style={{ position: 'fixed', top, left, width, zIndex: 1000 }}>
|
|
63
|
+
{children}
|
|
64
|
+
</div>,
|
|
65
|
+
document.body,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// A muted rotation for select/tag pills when an option declares no color.
|
|
70
|
+
const PILL_FALLBACKS = ['#E0E7FF', '#FCE7F3', '#D1FAE5', '#FEF3C7', '#E0F2FE', '#F3E8FF', '#FFE4E6'];
|
|
71
|
+
|
|
72
|
+
function normOptions(options) {
|
|
73
|
+
return (options || []).map((o, i) => {
|
|
74
|
+
if (typeof o === 'string') return { label: o, color: PILL_FALLBACKS[i % PILL_FALLBACKS.length] };
|
|
75
|
+
return { label: o.label ?? String(o.value ?? ''), color: o.color || PILL_FALLBACKS[i % PILL_FALLBACKS.length] };
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function pillColor(label, options) {
|
|
80
|
+
const opts = normOptions(options);
|
|
81
|
+
const hit = opts.find((o) => o.label === label);
|
|
82
|
+
if (hit) return hit.color;
|
|
83
|
+
// stable fallback by hash so ad-hoc tags keep their color
|
|
84
|
+
let h = 0;
|
|
85
|
+
for (const ch of String(label)) h = (h * 31 + ch.charCodeAt(0)) >>> 0;
|
|
86
|
+
return PILL_FALLBACKS[h % PILL_FALLBACKS.length];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// An inline image whose url may be a product path (needs authed fetch).
|
|
90
|
+
function RefImage({ refVal, fetchBlobUrl }) {
|
|
91
|
+
const [src, setSrc] = useState(/^https?:\/\//i.test(refVal.url || '') ? refVal.url : null);
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
let dead = false; let obj = null;
|
|
94
|
+
if (!src && refVal.url && fetchBlobUrl) {
|
|
95
|
+
fetchBlobUrl(refVal.url).then((u) => { if (!dead) { obj = u; setSrc(u); } });
|
|
96
|
+
}
|
|
97
|
+
return () => { dead = true; if (obj) URL.revokeObjectURL(obj); };
|
|
98
|
+
}, [refVal.url]);
|
|
99
|
+
if (!src) return <span className="shg-refchip">{RESOURCE_ICON.image} {refVal.name || 'image'}</span>;
|
|
100
|
+
return <img className="shg-refimg" src={src} alt={refVal.name || ''} title={refVal.name || ''} />;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function ResourceChip({ refVal, onOpen }) {
|
|
104
|
+
const icon = RESOURCE_ICON[refVal.kind] || '◇';
|
|
105
|
+
return (
|
|
106
|
+
<button className="shg-refchip shg-refchip-btn" title={`Open ${refVal.kind}`}
|
|
107
|
+
onClick={(e) => { e.stopPropagation(); onOpen && onOpen(refVal); }}>
|
|
108
|
+
<span className="shg-refchip-ic">{icon}</span>
|
|
109
|
+
<span className="shg-refchip-name">{refVal.name || refVal.id || refVal.kind}</span>
|
|
110
|
+
</button>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// A small status dot for computed cells.
|
|
115
|
+
function StatusDot({ status }) {
|
|
116
|
+
if (!status || status === 'empty') return null;
|
|
117
|
+
return <span className={`shg-dot shg-dot-${status}`} title={status} />;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function CellValue({ cell, col, fetchBlobUrl, onOpenResource }) {
|
|
121
|
+
const v = cell?.value;
|
|
122
|
+
if (v === undefined || v === null || v === '') return <span className="shg-val-txt" />;
|
|
123
|
+
// A typed artifact ref renders the same whether hand-set (resource column)
|
|
124
|
+
// or produced by a compute kind (e.g. Generate image).
|
|
125
|
+
if (v && typeof v === 'object' && !Array.isArray(v) && v.kind) {
|
|
126
|
+
if (v.kind === 'image') return <RefImage refVal={v} fetchBlobUrl={fetchBlobUrl} />;
|
|
127
|
+
return <ResourceChip refVal={v} onOpen={onOpenResource} />;
|
|
128
|
+
}
|
|
129
|
+
switch (col.type) {
|
|
130
|
+
case 'select':
|
|
131
|
+
return <span className="shg-pill" style={{ background: pillColor(String(v), col.options) }}>{String(v)}</span>;
|
|
132
|
+
case 'tags': {
|
|
133
|
+
const tags = Array.isArray(v) ? v : String(v).split(',').map((s) => s.trim()).filter(Boolean);
|
|
134
|
+
return (
|
|
135
|
+
<span className="shg-pills">
|
|
136
|
+
{tags.map((t) => (
|
|
137
|
+
<span key={t} className="shg-pill" style={{ background: pillColor(t, col.options) }}>{t}</span>
|
|
138
|
+
))}
|
|
139
|
+
</span>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
case 'checkbox':
|
|
143
|
+
return <span className={'shg-check' + (v ? ' on' : '')}>{v ? '✓' : ''}</span>;
|
|
144
|
+
case 'url': {
|
|
145
|
+
const href = /^https?:\/\//i.test(String(v)) ? String(v) : `https://${v}`;
|
|
146
|
+
return <a className="shg-link" href={href} target="_blank" rel="noreferrer" onClick={(e) => e.stopPropagation()}>{String(v)}</a>;
|
|
147
|
+
}
|
|
148
|
+
case 'number':
|
|
149
|
+
return <span className="shg-val-txt shg-num">{String(v)}</span>;
|
|
150
|
+
default:
|
|
151
|
+
return <span className="shg-val-txt">{typeof v === 'object' ? JSON.stringify(v) : String(v)}</span>;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function SheetGrid({ sheet, onChange, onRunCell, onRunColumn, readOnly = false, fetchBlobUrl, onOpenResource, peerMarks, onActiveCell }) {
|
|
156
|
+
const columns = sheet?.columns || [];
|
|
157
|
+
const rows = sheet?.rows || [];
|
|
158
|
+
const cells = sheet?.cells || {};
|
|
159
|
+
const [editing, setEditing] = useState(null); // {rowId, colId, type}
|
|
160
|
+
const [draft, setDraft] = useState('');
|
|
161
|
+
const [menuCol, setMenuCol] = useState(null); // {id, rect} of the open column menu
|
|
162
|
+
const [sizeDraft, setSizeDraft] = useState({}); // live {col:{id:w}, row:{id:h}} during a drag
|
|
163
|
+
const [dnd, setDnd] = useState(null); // {kind:'col'|'row', id} being dragged
|
|
164
|
+
const [dropAt, setDropAt] = useState(null); // {kind, id, after} current drop slot
|
|
165
|
+
const inputRef = useRef(null);
|
|
166
|
+
const dragRef = useRef(null);
|
|
167
|
+
|
|
168
|
+
// Drag-to-resize: live via sizeDraft, persisted into the sheet on release.
|
|
169
|
+
const startResize = (e, kind, id, start) => {
|
|
170
|
+
if (readOnly) return;
|
|
171
|
+
e.preventDefault(); e.stopPropagation();
|
|
172
|
+
const origin = kind === 'col' ? e.clientX : e.clientY;
|
|
173
|
+
dragRef.current = { kind, id, start, origin, last: start };
|
|
174
|
+
const onMove = (ev) => {
|
|
175
|
+
const d = dragRef.current; if (!d) return;
|
|
176
|
+
const delta = (d.kind === 'col' ? ev.clientX : ev.clientY) - d.origin;
|
|
177
|
+
const next = Math.max(d.kind === 'col' ? 90 : 26, d.start + delta);
|
|
178
|
+
d.last = next;
|
|
179
|
+
setSizeDraft((sd) => ({ ...sd, [d.kind]: { ...(sd[d.kind] || {}), [d.id]: next } }));
|
|
180
|
+
};
|
|
181
|
+
const onUp = () => {
|
|
182
|
+
const d = dragRef.current; dragRef.current = null;
|
|
183
|
+
window.removeEventListener('mousemove', onMove);
|
|
184
|
+
window.removeEventListener('mouseup', onUp);
|
|
185
|
+
document.body.style.cursor = '';
|
|
186
|
+
if (!d) return;
|
|
187
|
+
setSizeDraft({});
|
|
188
|
+
mutate((n) => {
|
|
189
|
+
if (d.kind === 'col') { const c = n.columns.find((x) => x.id === d.id); if (c) c.width = Math.round(d.last); }
|
|
190
|
+
else { const r = n.rows.find((x) => x.id === d.id); if (r) r.height = Math.round(d.last); }
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
window.addEventListener('mousemove', onMove);
|
|
194
|
+
window.addEventListener('mouseup', onUp);
|
|
195
|
+
document.body.style.cursor = kind === 'col' ? 'col-resize' : 'row-resize';
|
|
196
|
+
};
|
|
197
|
+
const colWidth = (col) => (sizeDraft.col && sizeDraft.col[col.id]) || col.width || undefined;
|
|
198
|
+
|
|
199
|
+
// Drag-to-reorder (columns by their header, rows by their number cell).
|
|
200
|
+
const reorder = (kind, fromId, toId, after) => mutate((n) => {
|
|
201
|
+
const arr = kind === 'col' ? n.columns : n.rows;
|
|
202
|
+
const i = arr.findIndex((x) => x.id === fromId);
|
|
203
|
+
if (i < 0 || fromId === toId) return;
|
|
204
|
+
const [item] = arr.splice(i, 1);
|
|
205
|
+
let j = arr.findIndex((x) => x.id === toId);
|
|
206
|
+
if (j < 0) { arr.splice(i, 0, item); return; }
|
|
207
|
+
if (after) j += 1;
|
|
208
|
+
arr.splice(j, 0, item);
|
|
209
|
+
});
|
|
210
|
+
const dndProps = (kind, id) => (readOnly ? {} : {
|
|
211
|
+
draggable: true,
|
|
212
|
+
onDragStart: (e) => {
|
|
213
|
+
if (e.target.closest && e.target.closest('.shg-resize-col, .shg-resize-row, input, button')) { e.preventDefault(); return; }
|
|
214
|
+
e.dataTransfer.effectAllowed = 'move';
|
|
215
|
+
e.dataTransfer.setData('text/plain', `${kind}:${id}`);
|
|
216
|
+
setDnd({ kind, id });
|
|
217
|
+
},
|
|
218
|
+
onDragOver: (e) => {
|
|
219
|
+
if (!dnd || dnd.kind !== kind || dnd.id === id) return;
|
|
220
|
+
e.preventDefault();
|
|
221
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
222
|
+
const after = kind === 'col' ? e.clientX > r.x + r.width / 2 : e.clientY > r.y + r.height / 2;
|
|
223
|
+
setDropAt((d) => (d && d.id === id && d.after === after ? d : { kind, id, after }));
|
|
224
|
+
},
|
|
225
|
+
onDrop: (e) => {
|
|
226
|
+
e.preventDefault();
|
|
227
|
+
if (dnd && dnd.kind === kind && dnd.id !== id) {
|
|
228
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
229
|
+
const after = kind === 'col' ? e.clientX > r.x + r.width / 2 : e.clientY > r.y + r.height / 2;
|
|
230
|
+
reorder(kind, dnd.id, id, after);
|
|
231
|
+
}
|
|
232
|
+
setDnd(null); setDropAt(null);
|
|
233
|
+
},
|
|
234
|
+
onDragEnd: () => { setDnd(null); setDropAt(null); },
|
|
235
|
+
});
|
|
236
|
+
const dndClass = (kind, id) => {
|
|
237
|
+
let cls = '';
|
|
238
|
+
if (dnd && dnd.kind === kind && dnd.id === id) cls += ' shg-dragging';
|
|
239
|
+
if (dropAt && dropAt.kind === kind && dropAt.id === id) {
|
|
240
|
+
cls += dropAt.after ? (kind === 'col' ? ' shg-drop-after' : ' shg-drop-below')
|
|
241
|
+
: (kind === 'col' ? ' shg-drop-before' : ' shg-drop-above');
|
|
242
|
+
}
|
|
243
|
+
return cls;
|
|
244
|
+
};
|
|
245
|
+
// The number gutter is FIXED: sized by the digit count of the row total so
|
|
246
|
+
// it never stretches (an empty grid otherwise splits the table width).
|
|
247
|
+
const numW = Math.max(40, 24 + String(rows.length || 1).length * 9);
|
|
248
|
+
const numStyle = { width: numW, minWidth: numW, maxWidth: numW };
|
|
249
|
+
const rowHeight = (row) => (sizeDraft.row && sizeDraft.row[row.id]) || row.height || undefined;
|
|
250
|
+
|
|
251
|
+
useEffect(() => { if (editing && inputRef.current) inputRef.current.focus(); }, [editing]);
|
|
252
|
+
|
|
253
|
+
const mutate = (fn) => {
|
|
254
|
+
if (readOnly || !onChange) return;
|
|
255
|
+
const next = JSON.parse(JSON.stringify(sheet));
|
|
256
|
+
fn(next);
|
|
257
|
+
onChange(next);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const setCell = (rowId, colId, value) => mutate((n) => {
|
|
261
|
+
n.cells = n.cells || {};
|
|
262
|
+
n.cells[cellKey(rowId, colId)] = { value, status: 'done', updated_at: Math.floor(Date.now() / 1000) };
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const openEdit = (rowId, col, anchorEl) => {
|
|
266
|
+
if (readOnly) return;
|
|
267
|
+
if (col.type === 'compute') return; // computed cells aren't hand-edited
|
|
268
|
+
if (col.type === 'checkbox') { // click toggles directly
|
|
269
|
+
const cur = cells[cellKey(rowId, col.id)];
|
|
270
|
+
setCell(rowId, col.id, !(cur && cur.value));
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const c = cells[cellKey(rowId, col.id)];
|
|
274
|
+
const v = c?.value;
|
|
275
|
+
if (col.type === 'tags') setDraft(Array.isArray(v) ? v.join(', ') : (v || ''));
|
|
276
|
+
else if (col.type === 'resource') setDraft(v && typeof v === 'object' ? v : { kind: 'sheet', name: '', id: '', url: '' });
|
|
277
|
+
else setDraft(v == null ? '' : String(v));
|
|
278
|
+
setEditing({ rowId, colId: col.id, type: col.type, rect: anchorEl?.getBoundingClientRect?.() || null });
|
|
279
|
+
onActiveCell && onActiveCell(cellKey(rowId, col.id));
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const commitEdit = () => {
|
|
283
|
+
onActiveCell && onActiveCell(null);
|
|
284
|
+
if (!editing) return;
|
|
285
|
+
const { rowId, colId, type } = editing;
|
|
286
|
+
let value = draft;
|
|
287
|
+
if (type === 'number') value = draft === '' ? null : Number(draft);
|
|
288
|
+
else if (type === 'tags') value = draft ? draft.split(',').map((s) => s.trim()).filter(Boolean) : [];
|
|
289
|
+
setCell(rowId, colId, value);
|
|
290
|
+
setEditing(null);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const addColumn = () => mutate((n) => {
|
|
294
|
+
n.columns.push({ id: uid('col'), name: `Column ${n.columns.length + 1}`, type: 'text' });
|
|
295
|
+
});
|
|
296
|
+
const addRow = () => mutate((n) => { n.rows.push({ id: uid('row') }); });
|
|
297
|
+
const deleteColumn = (colId) => mutate((n) => {
|
|
298
|
+
n.columns = n.columns.filter((c) => c.id !== colId);
|
|
299
|
+
for (const k of Object.keys(n.cells || {})) if (k.endsWith(`:${colId}`)) delete n.cells[k];
|
|
300
|
+
});
|
|
301
|
+
const deleteRow = (rowId) => mutate((n) => {
|
|
302
|
+
n.rows = n.rows.filter((r) => r.id !== rowId);
|
|
303
|
+
for (const k of Object.keys(n.cells || {})) if (k.startsWith(`${rowId}:`)) delete n.cells[k];
|
|
304
|
+
});
|
|
305
|
+
const renameColumn = (colId, name) => mutate((n) => {
|
|
306
|
+
const c = n.columns.find((x) => x.id === colId); if (c) c.name = name;
|
|
307
|
+
});
|
|
308
|
+
const configureColumn = (colId, patch) => mutate((n) => {
|
|
309
|
+
const c = n.columns.find((x) => x.id === colId); if (!c) return;
|
|
310
|
+
Object.assign(c, patch);
|
|
311
|
+
if (patch.type && patch.type !== 'compute') delete c.compute;
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// The inline editor for the editing cell, by type.
|
|
315
|
+
const renderEditor = (col) => {
|
|
316
|
+
if (col.type === 'resource') {
|
|
317
|
+
const d = typeof draft === 'object' && draft ? draft : { kind: 'sheet' };
|
|
318
|
+
const set = (k, v) => setDraft({ ...d, [k]: v });
|
|
319
|
+
return (
|
|
320
|
+
<PopPortal anchor={editing.rect} width={240} estHeight={230}
|
|
321
|
+
className="shg-select-pop shg-ref-pop" onClose={() => setEditing(null)}>
|
|
322
|
+
<div onClick={(e) => e.stopPropagation()}>
|
|
323
|
+
<select className="shg-menu-input" value={d.kind || 'sheet'} onChange={(e) => set('kind', e.target.value)}>
|
|
324
|
+
{RESOURCE_KINDS.map((k) => <option key={k} value={k}>{k}</option>)}
|
|
325
|
+
</select>
|
|
326
|
+
<input className="shg-menu-input" placeholder="Name" value={d.name || ''} onChange={(e) => set('name', e.target.value)} />
|
|
327
|
+
<input className="shg-menu-input" placeholder="Resource id (e.g. sheet.…)" value={d.id || ''} onChange={(e) => set('id', e.target.value)} />
|
|
328
|
+
<input className="shg-menu-input" placeholder="or URL" value={d.url || ''} onChange={(e) => set('url', e.target.value)} />
|
|
329
|
+
<div className="shg-menu-actions">
|
|
330
|
+
<button className="shg-pop-clear" onClick={() => { setCell(editing.rowId, editing.colId, null); setEditing(null); }}>Clear</button>
|
|
331
|
+
<button className="shg-menu-apply" onClick={() => { setCell(editing.rowId, editing.colId, d); setEditing(null); }}>Set</button>
|
|
332
|
+
</div>
|
|
333
|
+
</div>
|
|
334
|
+
</PopPortal>
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
if (col.type === 'select') {
|
|
338
|
+
const opts = normOptions(col.options);
|
|
339
|
+
return (
|
|
340
|
+
<PopPortal anchor={editing.rect} width={190} estHeight={40 + opts.length * 30}
|
|
341
|
+
className="shg-select-pop" onClose={() => setEditing(null)}>
|
|
342
|
+
{opts.length === 0 && <div className="shg-pop-note">No options yet — add them in the column menu.</div>}
|
|
343
|
+
{opts.map((o) => (
|
|
344
|
+
<button key={o.label} className="shg-pill shg-pill-btn" style={{ background: o.color }}
|
|
345
|
+
onClick={(e) => { e.stopPropagation(); setCell(editing.rowId, editing.colId, o.label); setEditing(null); }}>
|
|
346
|
+
{o.label}
|
|
347
|
+
</button>
|
|
348
|
+
))}
|
|
349
|
+
<button className="shg-pop-clear"
|
|
350
|
+
onClick={(e) => { e.stopPropagation(); setCell(editing.rowId, editing.colId, null); setEditing(null); }}>
|
|
351
|
+
Clear
|
|
352
|
+
</button>
|
|
353
|
+
</PopPortal>
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
const inputType = col.type === 'date' ? 'date' : col.type === 'number' ? 'number' : 'text';
|
|
357
|
+
return (
|
|
358
|
+
<input ref={inputRef} className="shg-input" type={inputType} value={draft}
|
|
359
|
+
placeholder={col.type === 'tags' ? 'tag, tag, …' : ''}
|
|
360
|
+
onChange={(e) => setDraft(e.target.value)}
|
|
361
|
+
onBlur={commitEdit}
|
|
362
|
+
onKeyDown={(e) => { if (e.key === 'Enter') commitEdit(); if (e.key === 'Escape') setEditing(null); }} />
|
|
363
|
+
);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
return (
|
|
367
|
+
<div className="shg">
|
|
368
|
+
<div className="shg-scroll">
|
|
369
|
+
<table className="shg-table">
|
|
370
|
+
<thead>
|
|
371
|
+
<tr>
|
|
372
|
+
<th className="shg-corner" style={numStyle} />
|
|
373
|
+
{columns.map((col) => (
|
|
374
|
+
<th key={col.id}
|
|
375
|
+
className={'shg-col' + (col.type === 'compute' ? ' shg-col-compute' : '') + dndClass('col', col.id)}
|
|
376
|
+
style={colWidth(col) ? { width: colWidth(col), minWidth: colWidth(col), maxWidth: colWidth(col) } : undefined}
|
|
377
|
+
{...dndProps('col', col.id)}>
|
|
378
|
+
{!readOnly && (
|
|
379
|
+
<span className="shg-resize-col" onMouseDown={(e) => startResize(e, 'col', col.id, colWidth(col) || e.currentTarget.closest('th').offsetWidth)} />
|
|
380
|
+
)}
|
|
381
|
+
<div className="shg-col-h">
|
|
382
|
+
<ColumnHeader
|
|
383
|
+
col={col} columns={columns} readOnly={readOnly}
|
|
384
|
+
menuOpen={menuCol?.id === col.id}
|
|
385
|
+
menuAnchor={menuCol?.id === col.id ? menuCol.rect : null}
|
|
386
|
+
onMenu={(e) => setMenuCol(menuCol?.id === col.id ? null
|
|
387
|
+
: { id: col.id, rect: e.currentTarget.getBoundingClientRect() })}
|
|
388
|
+
onCloseMenu={() => setMenuCol(null)}
|
|
389
|
+
onRename={renameColumn}
|
|
390
|
+
onConfigure={configureColumn}
|
|
391
|
+
onDelete={() => { setMenuCol(null); deleteColumn(col.id); }}
|
|
392
|
+
onRun={col.type === 'compute' && onRunColumn ? () => onRunColumn(col.id) : null} />
|
|
393
|
+
</div>
|
|
394
|
+
</th>
|
|
395
|
+
))}
|
|
396
|
+
{!readOnly && (
|
|
397
|
+
<th className="shg-add-col"><button className="shg-add" onClick={addColumn} title="Add column">+</button></th>
|
|
398
|
+
)}
|
|
399
|
+
</tr>
|
|
400
|
+
</thead>
|
|
401
|
+
<tbody>
|
|
402
|
+
{rows.map((row, ri) => (
|
|
403
|
+
<tr key={row.id}
|
|
404
|
+
style={{
|
|
405
|
+
...(rowHeight(row) ? { height: rowHeight(row) } : null),
|
|
406
|
+
'--shg-clamp': Math.max(1, Math.floor(((rowHeight(row) || 34) - 10) / 18)),
|
|
407
|
+
}}>
|
|
408
|
+
<td className={'shg-rownum' + dndClass('row', row.id)} style={numStyle}
|
|
409
|
+
title="Drag to reorder" {...dndProps('row', row.id)}>
|
|
410
|
+
<span>{ri + 1}</span>
|
|
411
|
+
{!readOnly && <button className="shg-rowdel" onClick={() => deleteRow(row.id)} title="Delete row">×</button>}
|
|
412
|
+
{!readOnly && (
|
|
413
|
+
<span className="shg-resize-row" onMouseDown={(e) => startResize(e, 'row', row.id, rowHeight(row) || e.currentTarget.closest('tr').offsetHeight)} />
|
|
414
|
+
)}
|
|
415
|
+
</td>
|
|
416
|
+
{columns.map((col) => {
|
|
417
|
+
const c = cells[cellKey(row.id, col.id)] || {};
|
|
418
|
+
const isEditing = editing && editing.rowId === row.id && editing.colId === col.id;
|
|
419
|
+
const peer = peerMarks && peerMarks[cellKey(row.id, col.id)];
|
|
420
|
+
return (
|
|
421
|
+
<td key={col.id}
|
|
422
|
+
className={'shg-cell' + (col.type === 'compute' ? ' shg-cell-compute' : '')
|
|
423
|
+
+ (c.status && col.type === 'compute' ? ` shg-cell-${c.status}` : '')
|
|
424
|
+
+ (peer ? ' shg-cell-peer' : '')}
|
|
425
|
+
style={peer ? { boxShadow: `inset 0 0 0 2px ${peer.color}` } : undefined}
|
|
426
|
+
onClick={(e) => { if (!isEditing) openEdit(row.id, col, e.currentTarget); }}
|
|
427
|
+
title={c.error || ''}>
|
|
428
|
+
{peer && <span className="shg-peer-flag" style={{ background: peer.color }}>{peer.name}</span>}
|
|
429
|
+
{isEditing ? renderEditor(col) : (
|
|
430
|
+
<div className="shg-val"
|
|
431
|
+
style={{ maxWidth: (colWidth(col) ? colWidth(col) - 20 : 400) }}>
|
|
432
|
+
{col.type === 'compute' && <StatusDot status={c.status} />}
|
|
433
|
+
<CellValue cell={c} col={col} fetchBlobUrl={fetchBlobUrl} onOpenResource={onOpenResource} />
|
|
434
|
+
{col.type === 'compute' && !readOnly && onRunCell && (
|
|
435
|
+
<button className="shg-runcell" title="Run this cell"
|
|
436
|
+
onClick={(e) => { e.stopPropagation(); onRunCell(row.id, col.id); }}>▶</button>
|
|
437
|
+
)}
|
|
438
|
+
</div>
|
|
439
|
+
)}
|
|
440
|
+
</td>
|
|
441
|
+
);
|
|
442
|
+
})}
|
|
443
|
+
{!readOnly && <td className="shg-pad" />}
|
|
444
|
+
</tr>
|
|
445
|
+
))}
|
|
446
|
+
{!readOnly && (
|
|
447
|
+
<tr className="shg-ghost-row" onClick={addRow}>
|
|
448
|
+
<td className="shg-rownum shg-ghost-num" style={numStyle}>+</td>
|
|
449
|
+
<td className="shg-ghost-cell" colSpan={columns.length + 1}>New row</td>
|
|
450
|
+
</tr>
|
|
451
|
+
)}
|
|
452
|
+
</tbody>
|
|
453
|
+
</table>
|
|
454
|
+
</div>
|
|
455
|
+
</div>
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function ColumnHeader({ col, columns, readOnly, menuOpen, menuAnchor, onMenu, onCloseMenu, onRename, onConfigure, onDelete, onRun }) {
|
|
460
|
+
const [editing, setEditing] = useState(false);
|
|
461
|
+
const [name, setName] = useState(col.name);
|
|
462
|
+
useEffect(() => setName(col.name), [col.name]);
|
|
463
|
+
return (
|
|
464
|
+
<div className="shg-colhdr">
|
|
465
|
+
<div className="shg-colhdr-main">
|
|
466
|
+
{editing && !readOnly ? (
|
|
467
|
+
<input className="shg-colname-input" value={name} autoFocus
|
|
468
|
+
onChange={(e) => setName(e.target.value)}
|
|
469
|
+
onBlur={() => { setEditing(false); if (name.trim() && name !== col.name) onRename(col.id, name.trim()); }}
|
|
470
|
+
onKeyDown={(e) => { if (e.key === 'Enter') e.target.blur(); }} />
|
|
471
|
+
) : (
|
|
472
|
+
<span className="shg-colname" onDoubleClick={() => !readOnly && setEditing(true)} title={col.name}>{col.name}</span>
|
|
473
|
+
)}
|
|
474
|
+
<span className={'shg-coltype' + (col.type === 'compute' ? ' compute' : '')}>
|
|
475
|
+
{TYPE_LABEL[col.type] || col.type}
|
|
476
|
+
{col.type === 'compute' && col.compute?.kind ? ` · ${col.compute.kind}` : ''}
|
|
477
|
+
</span>
|
|
478
|
+
</div>
|
|
479
|
+
{!readOnly && (
|
|
480
|
+
<div className="shg-colhdr-tools">
|
|
481
|
+
{onRun && <button className="shg-colrun" onClick={onRun} title="Run this column">▶ Run</button>}
|
|
482
|
+
<button className="shg-colmenu-btn" onClick={(e) => onMenu(e)} title="Column settings">⋯</button>
|
|
483
|
+
</div>
|
|
484
|
+
)}
|
|
485
|
+
{menuOpen && !readOnly && (
|
|
486
|
+
<ColumnMenu col={col} columns={columns} anchor={menuAnchor}
|
|
487
|
+
onApply={(patch) => { onConfigure(col.id, patch); onCloseMenu(); }}
|
|
488
|
+
onDelete={onDelete} onClose={onCloseMenu} />
|
|
489
|
+
)}
|
|
490
|
+
</div>
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// The column configuration popover: type, options (select/tags), and the
|
|
495
|
+
// compute mount (prompt / harness / workflow + deps). Product-agnostic —
|
|
496
|
+
// plain controlled inputs, applied as one patch.
|
|
497
|
+
function ColumnMenu({ col, columns, anchor, onApply, onDelete, onClose }) {
|
|
498
|
+
const [type, setType] = useState(col.type || 'text');
|
|
499
|
+
const [optText, setOptText] = useState(
|
|
500
|
+
normOptions(col.options).map((o) => o.label).join('\n'));
|
|
501
|
+
const comp = col.compute || {};
|
|
502
|
+
const [kind, setKind] = useState(comp.kind || 'prompt');
|
|
503
|
+
const [prompt, setPrompt] = useState(comp.prompt || '');
|
|
504
|
+
const [harnessId, setHarnessId] = useState(comp.harness_id || '');
|
|
505
|
+
const [wfSlug, setWfSlug] = useState(comp.workflow_slug || '');
|
|
506
|
+
const [outKey, setOutKey] = useState(comp.output_key || '');
|
|
507
|
+
const [deps, setDeps] = useState(new Set(comp.deps || []));
|
|
508
|
+
const ref = useRef(null);
|
|
509
|
+
|
|
510
|
+
useEffect(() => {
|
|
511
|
+
const onDown = (e) => { if (ref.current && !ref.current.contains(e.target)) onClose(); };
|
|
512
|
+
window.addEventListener('mousedown', onDown);
|
|
513
|
+
return () => window.removeEventListener('mousedown', onDown);
|
|
514
|
+
}, [onClose]);
|
|
515
|
+
|
|
516
|
+
const others = columns.filter((c) => c.id !== col.id);
|
|
517
|
+
const toggleDep = (id) => setDeps((s) => {
|
|
518
|
+
const n = new Set(s);
|
|
519
|
+
if (n.has(id)) n.delete(id); else n.add(id);
|
|
520
|
+
return n;
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
const apply = () => {
|
|
524
|
+
const patch = { type };
|
|
525
|
+
if (type === 'select' || type === 'tags') {
|
|
526
|
+
patch.options = optText.split('\n').map((s) => s.trim()).filter(Boolean);
|
|
527
|
+
}
|
|
528
|
+
if (type === 'compute') {
|
|
529
|
+
const compute = { kind, deps: [...deps] };
|
|
530
|
+
if (kind === 'prompt') compute.prompt = prompt;
|
|
531
|
+
if (kind === 'harness') { compute.harness_id = harnessId; compute.prompt = prompt; }
|
|
532
|
+
if (kind === 'workflow') { compute.workflow_slug = wfSlug; compute.output_key = outKey; }
|
|
533
|
+
patch.compute = compute;
|
|
534
|
+
}
|
|
535
|
+
onApply(patch);
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
return (
|
|
539
|
+
<PopPortal anchor={anchor} width={250} estHeight={340} className="shg-menu" onClose={onClose}>
|
|
540
|
+
<div ref={ref} onClick={(e) => e.stopPropagation()}>
|
|
541
|
+
<label className="shg-menu-lbl">Type
|
|
542
|
+
<select className="shg-menu-input" value={type} onChange={(e) => setType(e.target.value)}>
|
|
543
|
+
{MANUAL_TYPES.map((t) => <option key={t} value={t}>{TYPE_LABEL[t]}</option>)}
|
|
544
|
+
<option value="compute">Computed</option>
|
|
545
|
+
</select>
|
|
546
|
+
</label>
|
|
547
|
+
|
|
548
|
+
{(type === 'select' || type === 'tags') && (
|
|
549
|
+
<label className="shg-menu-lbl">Options (one per line)
|
|
550
|
+
<textarea className="shg-menu-input" rows={3} value={optText}
|
|
551
|
+
onChange={(e) => setOptText(e.target.value)} placeholder={'Todo\nDoing\nDone'} />
|
|
552
|
+
</label>
|
|
553
|
+
)}
|
|
554
|
+
|
|
555
|
+
{type === 'compute' && (
|
|
556
|
+
<>
|
|
557
|
+
<label className="shg-menu-lbl">Runs
|
|
558
|
+
<select className="shg-menu-input" value={kind} onChange={(e) => setKind(e.target.value)}>
|
|
559
|
+
{COMPUTE_KINDS.map((k) => <option key={k.kind} value={k.kind}>{k.label}</option>)}
|
|
560
|
+
</select>
|
|
561
|
+
</label>
|
|
562
|
+
{(kind === 'prompt' || kind === 'harness') && (
|
|
563
|
+
<label className="shg-menu-lbl">Prompt — reference columns as {'{{Name}}'}
|
|
564
|
+
<textarea className="shg-menu-input" rows={3} value={prompt}
|
|
565
|
+
onChange={(e) => setPrompt(e.target.value)}
|
|
566
|
+
placeholder="Summarize {{Company}} in one line" />
|
|
567
|
+
</label>
|
|
568
|
+
)}
|
|
569
|
+
{kind === 'harness' && (
|
|
570
|
+
<label className="shg-menu-lbl">Agent id
|
|
571
|
+
<input className="shg-menu-input" value={harnessId} onChange={(e) => setHarnessId(e.target.value)} placeholder="chrn_…" />
|
|
572
|
+
</label>
|
|
573
|
+
)}
|
|
574
|
+
{kind === 'workflow' && (
|
|
575
|
+
<>
|
|
576
|
+
<label className="shg-menu-lbl">Workflow id
|
|
577
|
+
<input className="shg-menu-input" value={wfSlug} onChange={(e) => setWfSlug(e.target.value)} placeholder="my-workflow" />
|
|
578
|
+
</label>
|
|
579
|
+
<label className="shg-menu-lbl">Output key (optional)
|
|
580
|
+
<input className="shg-menu-input" value={outKey} onChange={(e) => setOutKey(e.target.value)} placeholder="summary" />
|
|
581
|
+
</label>
|
|
582
|
+
</>
|
|
583
|
+
)}
|
|
584
|
+
{others.length > 0 && (
|
|
585
|
+
<div className="shg-menu-lbl">Reads columns
|
|
586
|
+
<div className="shg-menu-deps">
|
|
587
|
+
{others.map((c) => (
|
|
588
|
+
<label key={c.id} className="shg-menu-dep">
|
|
589
|
+
<input type="checkbox" checked={deps.has(c.id)} onChange={() => toggleDep(c.id)} /> {c.name}
|
|
590
|
+
</label>
|
|
591
|
+
))}
|
|
592
|
+
</div>
|
|
593
|
+
</div>
|
|
594
|
+
)}
|
|
595
|
+
</>
|
|
596
|
+
)}
|
|
597
|
+
|
|
598
|
+
<div className="shg-menu-actions">
|
|
599
|
+
<button className="shg-menu-del" onClick={onDelete}>Delete column</button>
|
|
600
|
+
<button className="shg-menu-apply" onClick={apply}>Apply</button>
|
|
601
|
+
</div>
|
|
602
|
+
</div>
|
|
603
|
+
</PopPortal>
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// ── Export helpers (CSV / TSV / array-of-arrays) ─────────────────────────────
|
|
608
|
+
export function sheetToDelimited(sheet, sep = ',') {
|
|
609
|
+
const cols = sheet?.columns || [];
|
|
610
|
+
const rows = sheet?.rows || [];
|
|
611
|
+
const cells = sheet?.cells || {};
|
|
612
|
+
const esc = (v) => {
|
|
613
|
+
const s = v == null ? '' : (Array.isArray(v) ? v.join(', ') : (typeof v === 'object' ? JSON.stringify(v) : String(v)));
|
|
614
|
+
if (sep === ',' && /[",\n]/.test(s)) return `"${s.replace(/"/g, '""')}"`;
|
|
615
|
+
return s;
|
|
616
|
+
};
|
|
617
|
+
const header = cols.map((c) => esc(c.name)).join(sep);
|
|
618
|
+
const body = rows.map((r) => cols.map((c) => {
|
|
619
|
+
const cell = cells[cellKey(r.id, c.id)];
|
|
620
|
+
return esc(cell ? cell.value : '');
|
|
621
|
+
}).join(sep));
|
|
622
|
+
return [header, ...body].join('\n');
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** Rows as an array-of-arrays (header first) — feed to an xlsx writer. */
|
|
626
|
+
export function sheetToAoA(sheet) {
|
|
627
|
+
const cols = sheet?.columns || [];
|
|
628
|
+
const rows = sheet?.rows || [];
|
|
629
|
+
const cells = sheet?.cells || {};
|
|
630
|
+
const val = (v) => (v == null ? '' : (Array.isArray(v) ? v.join(', ') : (typeof v === 'object' ? JSON.stringify(v) : v)));
|
|
631
|
+
return [cols.map((c) => c.name),
|
|
632
|
+
...rows.map((r) => cols.map((c) => val(cells[cellKey(r.id, c.id)]?.value)))];
|
|
633
|
+
}
|