Files
call-recorder/ui/src/routes/Main.svelte
T
haukeandClaude Sonnet 5 fa55dcb7bc fix #14740: upload a transcript only once
A call once uploaded can no longer be sent again: claim_upload refuses a
call that already carries a CRM transcript id ("Bereits hochgeladen"),
checked under the same lock that already serializes uploads per call, so a
concurrent second upload can never slip through between the check and the
one upload it allows. upload_transcript runs this check before any HTTP
request, so a rejected re-upload never reaches the backend.

The call detail view drops "Erneut hochladen" and its confirmation dialog:
once a call has uploaded_at/crm_transcript_id, only "Hochgeladen am …" is
shown, no button.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-06 15:40:31 +01:00

578 lines
15 KiB
Svelte

<script lang="ts">
import { onMount } from 'svelte'
import {
discardCall,
errorText,
getCall,
getRecordingState,
listCalls,
mergeSpeakers,
onBackendLoginError,
onCallEnded,
onCallUpdated,
onPromptChanged,
onRecordingState,
renameCall,
renameSpeaker,
retryCall,
showRecorder,
startManualRecording,
transcribeCall,
unmergeSpeaker,
uploadTranscript,
type CallDetail,
type CallId,
type CallListItem,
type RecordingState,
} from '../lib/api'
import CallList from '../lib/CallList.svelte'
import { formatDateTime, formatDetailTime, formatMinutes } from '../lib/format'
import StatusPill from '../lib/StatusPill.svelte'
import TranscriptView from '../lib/TranscriptView.svelte'
import UpdateBanner from '../lib/UpdateBanner.svelte'
let calls = $state<CallListItem[]>([])
let loaded = $state(false)
let listError = $state('')
let recordingState = $state<RecordingState>({ state: 'idle' })
let recordBusy = $state(false)
let recordError = $state('')
let selectedId = $state<CallId | null>(null)
let detail = $state<CallDetail | null>(null)
let detailError = $state('')
let actionError = $state('')
let busy = $state(false)
// Uploads run per call and may outlive the selection (the login can
// take a while); their errors are kept per call too.
let uploading = $state<CallId[]>([])
let uploadErrors = $state<Record<CallId, string>>({})
let editingTitle = $state(false)
let titleDraft = $state('')
let savingTitle = $state(false)
let titleError = $state('')
// Answers can arrive out of order; only the newest request is applied.
let listRequest = 0
let detailRequest = 0
async function refreshList() {
const request = ++listRequest
try {
const next = await listCalls()
if (request !== listRequest) return
const before = calls.find((c) => c.id === selectedId)
const after = next.find((c) => c.id === selectedId)
const firstLoad = !loaded
calls = next
loaded = true
listError = ''
if (firstLoad && selectedId === null && next.length > 0) {
select(next[0].id)
} else if (selectedId !== null && !after) {
clearSelection() // discarded or deleted elsewhere
} else if (JSON.stringify(before) !== JSON.stringify(after)) {
refreshDetail()
}
} catch (err) {
listError = errorText(err)
}
}
async function refreshDetail() {
const id = selectedId
if (id === null) return
const request = ++detailRequest
try {
const next = await getCall(id)
if (request === detailRequest && id === selectedId) {
detail = next
detailError = ''
}
} catch (err) {
if (request === detailRequest && id === selectedId) detailError = errorText(err)
}
}
function select(id: CallId) {
if (id === selectedId) return
selectedId = id
detail = null
detailError = ''
actionError = ''
editingTitle = false
titleError = ''
refreshDetail()
}
function clearSelection() {
selectedId = null
detail = null
detailError = ''
actionError = ''
editingTitle = false
titleError = ''
}
onMount(() => {
const unlisten = [
onCallUpdated((id) => {
refreshList()
if (id === selectedId) refreshDetail()
}),
onCallEnded(refreshList),
// Creating the key failed; the login window stays open for another try.
onBackendLoginError((msg) => {
for (const id of uploading) uploadErrors[id] = msg
}),
onPromptChanged(refreshList),
onRecordingState((state) => {
recordingState = state
refreshList()
}),
]
refreshList()
getRecordingState()
.then((state) => (recordingState = state))
.catch((err) => (recordError = errorText(err)))
return () => unlisten.forEach((u) => u.then((off) => off()))
})
/** The record button: starts a manual recording, or shows the floating
* recorder window if one already runs (same as the tray entry). Errors
* (e.g. "Es läuft bereits eine Aufnahme", device errors) show inline in
* the sidebar header. */
async function recordClick() {
recordError = ''
if (recordingState.state === 'recording') {
try {
await showRecorder()
} catch (err) {
recordError = errorText(err)
}
return
}
recordBusy = true
try {
await startManualRecording()
} catch (err) {
recordError = errorText(err)
} finally {
recordBusy = false
}
}
/** Runs a command on the shown call; its error is shown inline. */
async function run(action: (id: CallId) => Promise<void>, done?: () => void) {
if (!detail) return
const id = detail.call.id
busy = true
actionError = ''
try {
await action(id)
done?.()
} catch (err) {
if (id === selectedId) actionError = errorText(err)
} finally {
busy = false
}
}
const transcribe = () => run(transcribeCall)
const retry = () => run(retryCall)
const discard = () =>
run(discardCall, () => {
clearSelection()
refreshList()
})
/** Uploads the shown call's transcript (logging in first if needed). A
* call already uploaded has no button any more: the CRM never gets a
* duplicate. */
async function upload() {
if (!detail) return
const id = detail.call.id
if (uploading.includes(id)) return
uploading = [...uploading, id]
delete uploadErrors[id]
try {
await uploadTranscript(id)
delete uploadErrors[id]
} catch (err) {
uploadErrors[id] = errorText(err)
} finally {
uploading = uploading.filter((u) => u !== id)
}
}
async function rename(speakerKey: string, name: string) {
if (!detail) return
await renameSpeaker(detail.call.id, speakerKey, name)
await refreshDetail()
}
async function merge(fromKey: string, intoKey: string) {
if (!detail) return
await mergeSpeakers(detail.call.id, fromKey, intoKey)
await refreshDetail()
}
async function unmerge(fromKey: string) {
if (!detail) return
await unmergeSpeaker(detail.call.id, fromKey)
await refreshDetail()
}
function startTitleEdit() {
titleDraft = detail?.call.title ?? ''
titleError = ''
editingTitle = true
}
/** Enter saves the title (empty restores the default), Esc cancels. */
async function titleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
editingTitle = false
titleError = ''
} else if (event.key === 'Enter' && detail && !savingTitle) {
savingTitle = true
titleError = ''
try {
await renameCall(detail.call.id, titleDraft.trim() || null)
editingTitle = false
await Promise.all([refreshList(), refreshDetail()])
} catch (err) {
titleError = errorText(err)
} finally {
savingTitle = false
}
}
}
function focus(node: HTMLInputElement) {
node.focus()
node.select()
}
/** "Microsoft Teams · heute 10:14 · 32 min" (no duration while recording). */
function defaultTitle(call: CallListItem): string {
const parts = [call.app, formatDetailTime(call.started_at)]
if (call.status !== 'recording') parts.push(formatMinutes(call.duration_secs))
return parts.join(' · ')
}
// Also while an upload started elsewhere (e.g. before switching views)
// still runs in the app.
let isUploading = $derived(
detail !== null && (uploading.includes(detail.call.id) || detail.uploading),
)
// A failed start (e.g. missing key) also marks the call failed; its
// error is then shown once, in the failure box.
let shownActionError = $derived(actionError !== detail?.call.error ? actionError : '')
</script>
<main class="main">
<UpdateBanner />
<aside class="sidebar">
<header class="sidebar-header">
<h1>Gespräche</h1>
<div class="header-actions">
<button
class="btn small record-btn"
class:primary={recordingState.state !== 'recording'}
class:recording={recordingState.state === 'recording'}
disabled={recordBusy}
title="Aufnahme manuell starten"
onclick={recordClick}
>
{recordingState.state === 'recording' ? 'Aufnahme läuft' : '● Aufnahme'}
</button>
<a class="gear" href="#/settings" title="Einstellungen" aria-label="Einstellungen">⚙</a>
</div>
</header>
{#if recordError}
<p class="error record-error">{recordError}</p>
{/if}
<div class="list">
{#if listError}
<p class="error pad">{listError}</p>
{/if}
{#if loaded}
<CallList {calls} {selectedId} onselect={select} />
{/if}
</div>
</aside>
<section class="detail">
{#if detail}
{@const call = detail.call}
<header class="detail-header">
<div class="heading">
{#if editingTitle}
<input
class="title-input"
aria-label="Titel des Gesprächs"
placeholder={defaultTitle(call)}
disabled={savingTitle}
bind:value={titleDraft}
onkeydown={titleKeydown}
use:focus
/>
{:else}
<h1>
{call.title ?? defaultTitle(call)}
<button class="edit-title" title="Titel ändern" onclick={startTitleEdit}>✎</button>
</h1>
{/if}
<StatusPill status={call.status} interrupted={call.interrupted} />
</div>
{#if editingTitle}
<p class="muted subline">Enter speichert, Esc bricht ab. Leer = Standardtitel.</p>
{:else if call.title}
<p class="muted subline">{defaultTitle(call)}</p>
{/if}
{#if titleError}
<p class="error">{titleError}</p>
{/if}
<div class="toolbar">
{#if call.status === 'done' && call.uploaded_at}
<span class="muted uploaded">
Hochgeladen am {formatDateTime(call.uploaded_at)} (ID {call.crm_transcript_id})
</span>
{/if}
<span class="actions">
{#if call.status === 'awaiting_decision'}
<button class="btn primary" disabled={busy} onclick={transcribe}>Transkribieren</button>
<button class="btn danger" disabled={busy} onclick={discard}>Verwerfen</button>
{:else if call.status === 'failed'}
<button class="btn primary" disabled={busy} onclick={retry}>Erneut versuchen</button>
<button class="btn danger" disabled={busy} onclick={discard}>Verwerfen</button>
{:else if call.status === 'done' && !call.uploaded_at}
<button
class="btn primary"
disabled={!call.transcribed_at || isUploading}
title={call.transcribed_at ? 'Transkript ins CRM hochladen' : 'Noch kein Transkript'}
onclick={upload}
>
{isUploading ? 'wird hochgeladen…' : 'Transkript hochladen'}
</button>
{/if}
</span>
</div>
{#if shownActionError}
<p class="error">{shownActionError}</p>
{/if}
{#if uploadErrors[call.id]}
<p class="error">{uploadErrors[call.id]}</p>
{/if}
</header>
<div class="body">
{#if call.status === 'recording'}
<p class="state rec">● Aufnahme läuft</p>
{:else if call.status === 'processing'}
<p class="state">wird verarbeitet…</p>
{:else if call.status === 'awaiting_decision'}
<p class="state">
{call.interrupted
? 'Noch nicht transkribiert. Die Aufnahme wurde unterbrochen.'
: 'Noch nicht transkribiert.'}
</p>
{:else if call.status === 'failed'}
<div class="failure">
<strong>Verarbeitung fehlgeschlagen</strong>
<div>{call.error ?? 'Unbekannter Fehler'}</div>
</div>
{:else}
<TranscriptView
segments={detail.segments}
merges={detail.merges}
onrename={rename}
onmerge={merge}
onunmerge={unmerge}
/>
{/if}
</div>
{:else if detailError}
<p class="error pad">{detailError}</p>
{:else if loaded && selectedId === null && calls.length > 0}
<p class="muted pad">Gespräch links auswählen.</p>
{/if}
</section>
</main>
<style>
:global(body:has(.main)) {
overflow: hidden;
}
.main {
display: grid;
grid-template-columns: minmax(260px, 30%) 1fr;
/* The update banner spans the first row; it is empty without one. */
grid-template-rows: auto minmax(0, 1fr);
height: 100vh;
}
.sidebar,
.detail {
grid-row: 2;
}
.sidebar {
display: flex;
flex-direction: column;
min-height: 0;
background: var(--surface);
border-right: 1px solid var(--border);
}
.sidebar-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 14px 12px 10px 16px;
border-bottom: 1px solid var(--border);
}
.sidebar-header h1 {
font-size: 16px;
}
.header-actions {
display: flex;
align-items: center;
gap: 6px;
}
.record-btn {
white-space: nowrap;
}
.record-btn.recording {
background: var(--rec-soft);
border-color: var(--rec);
color: var(--rec);
}
.record-error {
margin: 0;
padding: 8px 16px 0;
}
.gear {
color: var(--muted);
font-size: 18px;
line-height: 1;
padding: 4px 6px;
border-radius: 6px;
text-decoration: none;
}
.gear:hover {
color: var(--primary);
background: var(--primary-soft);
}
.list {
flex: 1;
overflow-y: auto;
}
.detail {
min-width: 0;
overflow-y: auto;
padding: 18px 24px 28px;
}
.pad {
padding: 16px;
}
.detail-header {
margin-bottom: 16px;
}
.heading {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 6px 12px;
}
.heading h1 {
font-size: 18px;
overflow-wrap: anywhere;
}
.edit-title {
border: none;
background: none;
color: var(--muted);
cursor: pointer;
font-size: 14px;
padding: 0 4px;
vertical-align: middle;
}
.edit-title:hover {
color: var(--primary);
}
.title-input {
flex: 1 1 260px;
max-width: 520px;
font-size: 16px;
font-weight: 600;
}
.subline {
margin: 4px 0 0;
font-size: 12px;
}
.toolbar {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 10px 16px;
margin-top: 12px;
}
.actions {
display: inline-flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
margin-left: auto;
}
.uploaded {
font-size: 12px;
}
.detail-header .error {
margin: 10px 0 0;
}
.state {
color: var(--muted);
}
.state.rec {
color: var(--rec);
font-weight: 600;
}
.failure {
padding: 10px 12px;
border-radius: 8px;
background: var(--rec-soft);
color: var(--rec);
}
</style>