Port of the vanilla refine-bar + output-actions block that every note
page had (see public/components/{encounter,dictation,soap,sickvisit,
wellvisit,hospital,chart}.html at commit be14578).
One shared OutputActions component renders under every generated
note's body with:
📋 Copy → navigator.clipboard
🔊 Read → POST /api/text-to-speech (audio/mpeg → Audio playback
with stop-on-second-click)
☁️ Export → POST /api/nextcloud/export (uses each page's exportLabel
as the filename prefix, exportType in the type field)
✏️ Refine → POST /api/refine — free-text instructions textarea,
sourceContext carries the original transcript so the
model can reference the source when following orders
📏 Shorter → POST /api/shorten
Busy/success/error state shown inline per-component — no native alerts.
Refine result replaces the existing output via onUpdate, matching
vanilla setOutputText() behavior.
Wired into all 7 note pages (Encounter, Dictation, SOAP, Sick Visit,
Well Visit, Hospital Course, Chart Review) with per-page exportLabel
/ exportType matching the vanilla data-label values.
177 lines
7.4 KiB
TypeScript
177 lines
7.4 KiB
TypeScript
// ============================================================
|
|
// DICTATION — voice dictation → HPI via /api/generate-hpi-dictation
|
|
//
|
|
// Minimum-viable port: demographics + transcript textarea + generate.
|
|
// The vanilla version also has MediaRecorder-based audio capture,
|
|
// transcription upload, save/load popover, refine, shorten, and
|
|
// Nextcloud export. Those each land in follow-up commits — this
|
|
// first pass proves the generate-HPI wire protocol works from React.
|
|
// ============================================================
|
|
|
|
import { useState } from 'react';
|
|
import { useMutation } from '@tanstack/react-query';
|
|
import { api, ApiError } from '@/lib/api';
|
|
import type { HpiOk } from '@/shared/types';
|
|
import { HpiEncounterRequestSchema, type HpiEncounterRequest } from '@/shared/schemas';
|
|
import Recorder from '@/components/Recorder';
|
|
import EncounterToolbar from '@/components/EncounterToolbar';
|
|
import OutputActions from '@/components/OutputActions';
|
|
|
|
type Setting = 'outpatient' | 'inpatient';
|
|
const TYPE = 'dictation' as const;
|
|
|
|
export default function Dictation() {
|
|
const [label, setLabel] = useState('');
|
|
const [patientAge, setPatientAge] = useState('');
|
|
const [patientGender, setPatientGender] = useState('');
|
|
const [setting, setSetting] = useState<Setting>('outpatient');
|
|
const [transcript, setTranscript] = useState('');
|
|
const [interim, setInterim] = useState('');
|
|
const [result, setResult] = useState<string | null>(null);
|
|
const [validationError, setValidationError] = useState<string | null>(null);
|
|
const [recError, setRecError] = useState<string | null>(null);
|
|
|
|
const generate = useMutation<HpiOk, Error, HpiEncounterRequest>({
|
|
mutationFn: (body) => api.post<HpiOk>('/api/generate-hpi-dictation', body),
|
|
onSuccess: (data) => setResult(data.hpi),
|
|
onError: () => setResult(null),
|
|
});
|
|
|
|
function submit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setValidationError(null);
|
|
const body: HpiEncounterRequest = { transcript: (interim || transcript).trim(), patientAge, patientGender, setting };
|
|
const parsed = HpiEncounterRequestSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
setValidationError(parsed.error.issues.map((i: { message: string }) => i.message).join(', '));
|
|
return;
|
|
}
|
|
setResult(null);
|
|
generate.mutate(parsed.data);
|
|
}
|
|
|
|
function clear() {
|
|
setTranscript('');
|
|
setInterim('');
|
|
setResult(null);
|
|
setValidationError(null);
|
|
}
|
|
|
|
const input = 'w-full rounded-md border border-input bg-background px-3 py-2 text-sm';
|
|
const displayedTranscript = interim || transcript;
|
|
|
|
return (
|
|
<div className="max-w-4xl mx-auto p-6 space-y-4">
|
|
<header>
|
|
<h1 className="text-2xl font-semibold">Voice Dictation → HPI</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
Dictate your narrative → AI restructures into polished HPI.
|
|
</p>
|
|
</header>
|
|
|
|
<EncounterToolbar
|
|
type={TYPE}
|
|
label={label} setLabel={setLabel}
|
|
transcript={transcript} generatedNote={result || ''}
|
|
partialData={{ age: patientAge, gender: patientGender, setting }}
|
|
onLoad={(enc) => {
|
|
setTranscript(enc.transcript || '');
|
|
setInterim('');
|
|
setResult(enc.generated_note || null);
|
|
try {
|
|
const pd = enc.partial_data ? JSON.parse(enc.partial_data) : null;
|
|
if (pd?.age) setPatientAge(pd.age);
|
|
if (pd?.gender) setPatientGender(pd.gender);
|
|
if (pd?.setting) setSetting(pd.setting);
|
|
} catch { /* ignore */ }
|
|
setLabel(enc.label || '');
|
|
}}
|
|
onClear={() => { clear(); setPatientAge(''); setPatientGender(''); setSetting('outpatient'); }}
|
|
/>
|
|
|
|
<form onSubmit={submit} className="space-y-4">
|
|
<div className="grid grid-cols-3 gap-3">
|
|
<label className="flex flex-col gap-1">
|
|
<span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Age</span>
|
|
<input className={input} placeholder="e.g. 8 months" value={patientAge} onChange={(e) => setPatientAge(e.target.value)} />
|
|
</label>
|
|
<label className="flex flex-col gap-1">
|
|
<span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Gender</span>
|
|
<select className={input} value={patientGender} onChange={(e) => setPatientGender(e.target.value)}>
|
|
<option value="">Select</option>
|
|
<option>Male</option>
|
|
<option>Female</option>
|
|
</select>
|
|
</label>
|
|
<label className="flex flex-col gap-1">
|
|
<span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Setting</span>
|
|
<select className={input} value={setting} onChange={(e) => setSetting(e.target.value as Setting)}>
|
|
<option value="outpatient">Outpatient</option>
|
|
<option value="inpatient">Inpatient / Floors</option>
|
|
</select>
|
|
</label>
|
|
</div>
|
|
|
|
<Recorder
|
|
module="dictation"
|
|
onTranscript={(text, meta) => {
|
|
setTranscript((prev) => (meta.appended ? (prev ? prev + ' ' + text : text) : text));
|
|
setInterim('');
|
|
setRecError(null);
|
|
}}
|
|
onInterim={(t) => setInterim(t ? (transcript ? transcript + ' ' + t : t) : '')}
|
|
onError={(msg) => setRecError(msg)}
|
|
/>
|
|
{recError && <div className="text-sm text-destructive">{recError}</div>}
|
|
|
|
<label className="flex flex-col gap-1">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
|
|
Transcript / dictation
|
|
</span>
|
|
<button type="button" onClick={clear} className="text-xs text-muted-foreground underline">
|
|
Clear
|
|
</button>
|
|
</div>
|
|
<textarea
|
|
className={input + ' min-h-[200px] font-mono text-sm'}
|
|
placeholder="Click Start recording, or type / paste your dictation here."
|
|
value={displayedTranscript}
|
|
onChange={(e) => { setTranscript(e.target.value); setInterim(''); }}
|
|
/>
|
|
</label>
|
|
|
|
{validationError && <div className="text-sm text-destructive">{validationError}</div>}
|
|
{generate.error && <div className="text-sm text-destructive">{(generate.error as ApiError).message}</div>}
|
|
|
|
<div className="flex gap-2">
|
|
<button
|
|
type="submit"
|
|
disabled={generate.isPending || !displayedTranscript.trim()}
|
|
className="rounded-md bg-primary text-primary-foreground px-4 py-2 text-sm font-medium disabled:opacity-50"
|
|
>
|
|
{generate.isPending ? 'Generating…' : 'Generate HPI'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
{result && (
|
|
<section className="rounded-lg border border-border bg-card">
|
|
<header className="px-4 py-2 border-b border-border bg-muted/40">
|
|
<h2 className="text-sm font-semibold">Generated HPI</h2>
|
|
</header>
|
|
<div className="p-4 whitespace-pre-wrap text-sm">{result}</div>
|
|
<div className="px-4 pb-4">
|
|
<OutputActions
|
|
text={result}
|
|
onUpdate={setResult}
|
|
sourceContext={displayedTranscript}
|
|
exportLabel="hpi-dictation"
|
|
exportType="hpi"
|
|
/>
|
|
</div>
|
|
</section>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|