Show the bound script source alongside order results

This commit is contained in:
2026-11-17 23:21:30 +01:00
parent 10f18dee76
commit ff25c38251
5 changed files with 118 additions and 28 deletions
+69 -24
View File
@@ -366,6 +366,48 @@ function Operations({ tenantId, open, customerName }: { tenantId: string; open:
{list.data && <PageButtons pages={pages} next={list.data.next_cursor} change={setPages}/>}
</section>;
}
function OrderScript({ tenantId, operation, names }: { tenantId: string; operation: RecordValue; names: RecordValue[] }) {
const operationId = objectId(operation, 'operation');
const planId = objectId(operation, 'plan');
const planHash = stringField(operation, 'plan_hash');
const scripts = useResource(async signal => {
const plan = record(await request(tenantPath(tenantId, '/operation-plans/' + encodeURIComponent(planId)), { signal }));
const stored = record(plan.plan_payload);
if (plan.operation_id !== operationId || plan.plan_hash !== planHash ||
plan.plan_id !== planId || stored.plan_id !== planId || stored.tenant_id !== tenantId) {
throw new Error('Der Skriptinhalt konnte diesem Auftrag nicht zugeordnet werden.');
}
await verifyScriptPlan(plan);
const jobs = scriptPlanJobs(plan);
if (!jobs.length) throw new Error('Für diesen Auftrag ist kein Skriptinhalt gespeichert.');
const variants = new Map<string, typeof jobs>();
for (const job of jobs) {
const key = JSON.stringify([job.scriptHash, job.interpreter]);
const group = variants.get(key);
if (group) group.push(job);
else variants.set(key, [job]);
}
return [...variants.values()];
}, [tenantId, operationId, planId, planHash]);
const variants = scripts.error ? [] : scripts.data ?? [];
return <article className="order-script" aria-label="Skriptinhalt">
<h3>Skriptinhalt</h3>
<ErrorBox error={scripts.error}/>
{!scripts.data && !scripts.error && <p>Skriptinhalt wird geladen …</p>}
{variants.map(group => {
const script = group[0]!;
return <div key={script.scriptHash + '/' + script.interpreter}>
{variants.length > 1 && <h4>{group.map(job => {
const target = names.find(item => item.asset_id === job.assetId && item.agent_id === job.agentId);
return target ? deviceName(target) : job.assetId;
}).join(', ')}</h4>}
<p className="muted">{script.revision ? 'Version ' + script.revision.version : 'Einmaliges Skript'} · {script.interpreter}</p>
<pre className="script-body" aria-label="Gespeicherter Skripttext" tabIndex={0}>{outputText(new TextEncoder().encode(script.script.replace(/\r\n/g, '\n'))).text}</pre>
</div>;
})}
</article>;
}
function OperationDetail({ tenantId, id, auth, openPlan, customerName }: {
tenantId: string; id: string; auth: Session; openPlan: (id: string) => void; customerName?: string
}) {
@@ -385,29 +427,32 @@ function OperationDetail({ tenantId, id, auth, openPlan, customerName }: {
<Fact label="Status"><Status value={statusOf(data)}/></Fact><Fact label="Eingereicht">{when(data.created_at)}</Fact>
<Fact label="Antragsteller"><Requester id={data.requester} ownId={auth.principal.id}/></Fact>
<Fact label="Anlass">{display(data.reason, 'Kein Anlass angegeben')}</Fact></dl>
{singleJob ? <JobDetail key={objectId(singleJob, 'job')} tenantId={tenantId}
id={objectId(singleJob, 'job')} hostname={name(singleJob)} auth={auth}/>
: <div className="order-results">
<h3>Geräte und Ergebnisse</h3>
{jobs.length ? <ul className="job-list">{jobs.map(job => {
const resultId = objectId(job, 'job');
const hostname = name(job);
const expanded = jobId === resultId;
const action = expanded ? 'Ergebnis schließen' : 'Ergebnis anzeigen';
return <li key={resultId}>
<div className="job-row">
<strong>{hostname}</strong>
<Status value={job.state}/>
<button type="button" data-job-id={resultId} aria-expanded={expanded}
aria-label={action + ' für ' + hostname}
onClick={() => setJobId(expanded ? null : resultId)}>{action}</button>
</div>
{expanded && <JobDetail tenantId={tenantId} id={resultId} hostname={hostname} auth={auth}/>}
</li>;
})}</ul>
: <><p>{names.map(deviceName).join(', ')}</p><p className="muted">{data.approval_state === 'pending_approval'
? 'Nach der Genehmigung erscheint hier der Fortschritt je Gerät.' : 'Für diesen Auftrag liegen keine Ausführungen vor.'}</p></>}
</div>}
<div className="order-execution">
{singleJob ? <JobDetail key={objectId(singleJob, 'job')} tenantId={tenantId}
id={objectId(singleJob, 'job')} hostname={name(singleJob)} auth={auth}/>
: <div className="order-results">
<h3>Geräte und Ergebnisse</h3>
{jobs.length ? <ul className="job-list">{jobs.map(job => {
const resultId = objectId(job, 'job');
const hostname = name(job);
const expanded = jobId === resultId;
const action = expanded ? 'Ergebnis schließen' : 'Ergebnis anzeigen';
return <li key={resultId}>
<div className="job-row">
<strong>{hostname}</strong>
<Status value={job.state}/>
<button type="button" data-job-id={resultId} aria-expanded={expanded}
aria-label={action + ' für ' + hostname}
onClick={() => setJobId(expanded ? null : resultId)}>{action}</button>
</div>
{expanded && <JobDetail tenantId={tenantId} id={resultId} hostname={hostname} auth={auth}/>}
</li>;
})}</ul>
: <><p>{names.map(deviceName).join(', ')}</p><p className="muted">{data.approval_state === 'pending_approval'
? 'Nach der Genehmigung erscheint hier der Fortschritt je Gerät.' : 'Für diesen Auftrag liegen keine Ausführungen vor.'}</p></>}
</div>}
{data.action === 'run_script' && <OrderScript tenantId={tenantId} operation={data} names={names}/>}
</div>
{allowed(auth, 'plans:approve') && <button className="quiet" onClick={() => openPlan(objectId(data,'plan'))}>
{data.approval_state === 'pending_approval' ? 'Antrag prüfen' : 'Genehmigungsdetail öffnen'}
</button>}
@@ -660,7 +705,7 @@ function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
{route.page === 'auftraege' && (
route.tab === 'neu' ? <>
<a href="#auftraege" className="back-link">← Zu den Aufträgen</a>
<OrderComposer tenantId={customer} customerName={customerName} auth={scoped} initialAssetIds={selected} onSubmitted={afterSubmission} />
<OrderComposer tenantId={customer} customerName={customerName} auth={scoped} initialAssetIds={selected} onSubmitted={afterSubmission} onDraft={showDraft} />
<details className="advanced-order"><summary>Weitere Wartungsaktionen</summary>
<p>Inventur, Dienstverwaltung und weitere Wartungsaufträge planen.</p>
<a className="quiet" href="#auftraege/wartung">Wartungsauftrag planen</a>