|
| 1 | +import React, { useState, useCallback, useEffect } from 'react'; |
| 2 | + |
| 3 | +import { getProcessId } from '../lib/utils/element'; |
| 4 | + |
| 5 | +function generateDefaultName() { |
| 6 | + const now = new Date(); |
| 7 | + const pad = (n, len = 2) => String(n).padStart(len, '0'); |
| 8 | + return `recording-${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * RecorderBar — toolbar for starting/stopping API response recordings |
| 13 | + * from the demo UI. Communicates with the server's recorder endpoints. |
| 14 | + * |
| 15 | + * @param {Object} props |
| 16 | + * @param {Object} [props.injector] — modeler injector for selection tracking |
| 17 | + */ |
| 18 | +export default function RecorderBar({ injector }) { |
| 19 | + const [ recording, setRecording ] = useState(false); |
| 20 | + const [ name, setName ] = useState(() => generateDefaultName()); |
| 21 | + const [ elementId, setElementId ] = useState(''); |
| 22 | + const [ processId, setProcessId ] = useState(''); |
| 23 | + const [ description, setDescription ] = useState(''); |
| 24 | + const [ message, setMessage ] = useState(null); |
| 25 | + |
| 26 | + // Track the currently selected element |
| 27 | + useEffect(() => { |
| 28 | + if (!injector || recording) return; |
| 29 | + |
| 30 | + const selection = injector.get('selection'); |
| 31 | + const eventBus = injector.get('eventBus'); |
| 32 | + |
| 33 | + const update = () => { |
| 34 | + const selected = selection.get(); |
| 35 | + |
| 36 | + if (selected.length === 1) { |
| 37 | + setElementId(selected[0].id); |
| 38 | + |
| 39 | + const pid = getProcessId(selected[0]); |
| 40 | + if (pid) { |
| 41 | + setProcessId(pid); |
| 42 | + } |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + update(); |
| 47 | + |
| 48 | + eventBus.on('selection.changed', update); |
| 49 | + |
| 50 | + return () => { |
| 51 | + eventBus.off('selection.changed', update); |
| 52 | + }; |
| 53 | + }, [ injector, recording ]); |
| 54 | + |
| 55 | + // Auto-fill process ID from the first process in the diagram |
| 56 | + useEffect(() => { |
| 57 | + if (!injector || processId || recording) return; |
| 58 | + |
| 59 | + try { |
| 60 | + const canvas = injector.get('canvas'); |
| 61 | + const rootElement = canvas.getRootElement(); |
| 62 | + |
| 63 | + if (rootElement?.businessObject?.id) { |
| 64 | + setProcessId(rootElement.businessObject.id); |
| 65 | + } |
| 66 | + } catch (e) { |
| 67 | + |
| 68 | + // ignore |
| 69 | + } |
| 70 | + }, [ injector, processId, recording ]); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + fetch('/api/recorder/status') |
| 74 | + .then(res => res.json()) |
| 75 | + .then(({ recording, name, elementId }) => { |
| 76 | + setRecording(recording); |
| 77 | + |
| 78 | + if (recording) { |
| 79 | + setName(name || ''); |
| 80 | + setElementId(elementId || ''); |
| 81 | + } |
| 82 | + }) |
| 83 | + .catch(() => {}); |
| 84 | + }, []); |
| 85 | + |
| 86 | + const start = useCallback(async () => { |
| 87 | + if (!name.trim() || !elementId.trim()) { |
| 88 | + setMessage('Name and element ID are required'); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const response = await fetch('/api/recorder/start', { |
| 93 | + method: 'POST', |
| 94 | + headers: { 'Content-Type': 'application/json' }, |
| 95 | + body: JSON.stringify({ |
| 96 | + name: name.trim(), |
| 97 | + elementId: elementId.trim(), |
| 98 | + processId: processId.trim() || undefined |
| 99 | + }) |
| 100 | + }); |
| 101 | + |
| 102 | + const result = await response.json(); |
| 103 | + |
| 104 | + if (result.success) { |
| 105 | + setRecording(true); |
| 106 | + setMessage(null); |
| 107 | + } else { |
| 108 | + setMessage(result.error || 'Failed to start recording'); |
| 109 | + } |
| 110 | + }, [ name, elementId, processId ]); |
| 111 | + |
| 112 | + const save = useCallback(async () => { |
| 113 | + const response = await fetch('/api/recorder/save', { |
| 114 | + method: 'POST', |
| 115 | + headers: { 'Content-Type': 'application/json' }, |
| 116 | + body: JSON.stringify({ |
| 117 | + description: description.trim() || undefined |
| 118 | + }) |
| 119 | + }); |
| 120 | + |
| 121 | + const result = await response.json(); |
| 122 | + |
| 123 | + if (result.success) { |
| 124 | + setRecording(false); |
| 125 | + setMessage(`Saved to ${result.filePath}`); |
| 126 | + setName(generateDefaultName()); |
| 127 | + setDescription(''); |
| 128 | + } else { |
| 129 | + setMessage(result.error || 'Failed to save recording'); |
| 130 | + } |
| 131 | + }, [ description ]); |
| 132 | + |
| 133 | + const discard = useCallback(async () => { |
| 134 | + await fetch('/api/recorder/discard', { method: 'POST' }); |
| 135 | + setRecording(false); |
| 136 | + setMessage('Recording discarded'); |
| 137 | + setName(generateDefaultName()); |
| 138 | + setDescription(''); |
| 139 | + }, []); |
| 140 | + |
| 141 | + return ( |
| 142 | + <div className="recorder-bar"> |
| 143 | + <span className="recorder-bar__icon">{ recording ? '⏺' : '📼' }</span> |
| 144 | + |
| 145 | + { !recording ? ( |
| 146 | + <div className="recorder-bar__form"> |
| 147 | + <span className="recorder-bar__input-wrapper"> |
| 148 | + <input |
| 149 | + className="recorder-bar__input" |
| 150 | + type="text" |
| 151 | + placeholder="Scenario name" |
| 152 | + value={ name } |
| 153 | + onChange={ e => setName(e.target.value) } |
| 154 | + /> |
| 155 | + { name && ( |
| 156 | + <button |
| 157 | + className="recorder-bar__clear" |
| 158 | + onClick={ () => setName('') } |
| 159 | + title="Clear name" |
| 160 | + > |
| 161 | + × |
| 162 | + </button> |
| 163 | + ) } |
| 164 | + </span> |
| 165 | + <input |
| 166 | + className="recorder-bar__input" |
| 167 | + type="text" |
| 168 | + placeholder="Element ID" |
| 169 | + value={ elementId } |
| 170 | + onChange={ e => setElementId(e.target.value) } |
| 171 | + /> |
| 172 | + <input |
| 173 | + className="recorder-bar__input recorder-bar__input--narrow" |
| 174 | + type="text" |
| 175 | + placeholder="Process ID" |
| 176 | + value={ processId } |
| 177 | + readOnly |
| 178 | + /> |
| 179 | + <button className="recorder-bar__button recorder-bar__button--start" onClick={ start }> |
| 180 | + Start recording |
| 181 | + </button> |
| 182 | + </div> |
| 183 | + ) : ( |
| 184 | + <div className="recorder-bar__form"> |
| 185 | + <span className="recorder-bar__status"> |
| 186 | + Recording: <strong>{ name }</strong> ({ elementId }) |
| 187 | + </span> |
| 188 | + <input |
| 189 | + className="recorder-bar__input" |
| 190 | + type="text" |
| 191 | + placeholder="Description (optional)" |
| 192 | + value={ description } |
| 193 | + onChange={ e => setDescription(e.target.value) } |
| 194 | + /> |
| 195 | + <button className="recorder-bar__button recorder-bar__button--save" onClick={ save }> |
| 196 | + Save |
| 197 | + </button> |
| 198 | + <button className="recorder-bar__button recorder-bar__button--discard" onClick={ discard }> |
| 199 | + Discard |
| 200 | + </button> |
| 201 | + </div> |
| 202 | + ) } |
| 203 | + |
| 204 | + { message && ( |
| 205 | + <span className="recorder-bar__message">{ message }</span> |
| 206 | + ) } |
| 207 | + </div> |
| 208 | + ); |
| 209 | +} |
0 commit comments