refactor: simplify TipModal — remove Expertenblick and redundant header
Strips all insight/agent state, fetchInsight() SSE function, audio playback logic, and the insightWrapper JSX block that called /api/agent/* routes. Also removes the matchHeader/groupBadge and kickoffBlock from the modal (info already visible on the match card). Cleans all corresponding CSS. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { useState, useRef } from 'react';
|
||||
import { Sparkles, ChevronDown, ChevronUp, Loader2, Volume2, Square } from 'lucide-react';
|
||||
import { useState } from 'react';
|
||||
import { Match, api } from '../api/client';
|
||||
import styles from './TipModal.module.css';
|
||||
|
||||
@@ -17,53 +16,6 @@ function getTendency(home: number, away: number): Tendency {
|
||||
return 'draw';
|
||||
}
|
||||
|
||||
// Streaming-Fetch für /api/agent/insight
|
||||
async function fetchInsight(
|
||||
homeTeam: string,
|
||||
awayTeam: string,
|
||||
stage: string,
|
||||
group: string | null,
|
||||
onChunk: (text: string) => void,
|
||||
onDone: () => void,
|
||||
onError: () => void
|
||||
) {
|
||||
try {
|
||||
const res = await fetch('/api/agent/insight', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ homeTeam, awayTeam, stage, group }),
|
||||
});
|
||||
if (!res.ok) { onError(); return; }
|
||||
|
||||
const reader = res.body?.getReader();
|
||||
if (!reader) { onError(); return; }
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop() ?? '';
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data: ')) continue;
|
||||
const payload = line.slice(6).trim();
|
||||
if (payload === '[DONE]') { onDone(); return; }
|
||||
try {
|
||||
const parsed = JSON.parse(payload);
|
||||
if (parsed.text) onChunk(parsed.text);
|
||||
if (parsed.error) { onError(); return; }
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
onDone();
|
||||
} catch {
|
||||
onError();
|
||||
}
|
||||
}
|
||||
|
||||
export default function TipModal({ match, onClose, onSaved }: Props) {
|
||||
const existing = match.userTip;
|
||||
const [home, setHome] = useState(existing?.home ?? 0);
|
||||
@@ -71,78 +23,6 @@ export default function TipModal({ match, onClose, onSaved }: Props) {
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// Expertenblick State
|
||||
const [insightOpen, setInsightOpen] = useState(false);
|
||||
const [insightText, setInsightText] = useState('');
|
||||
const [insightLoading, setInsightLoading] = useState(false);
|
||||
const [insightError, setInsightError] = useState(false);
|
||||
const insightFetched = useRef(false);
|
||||
|
||||
// Audio State
|
||||
const [audioLoading, setAudioLoading] = useState(false);
|
||||
const [audioPlaying, setAudioPlaying] = useState(false);
|
||||
const [audioError, setAudioError] = useState(false);
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
|
||||
async function handlePlayAudio() {
|
||||
// Stop wenn gerade läuft
|
||||
if (audioPlaying && audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current.currentTime = 0;
|
||||
setAudioPlaying(false);
|
||||
return;
|
||||
}
|
||||
setAudioError(false);
|
||||
setAudioLoading(true);
|
||||
try {
|
||||
const res = await fetch('/api/agent/insight-audio', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ dialogText: insightText }),
|
||||
});
|
||||
if (!res.ok) throw new Error('Audio-Generierung fehlgeschlagen');
|
||||
const blob = await res.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const audio = new Audio(url);
|
||||
audioRef.current = audio;
|
||||
audio.onended = () => {
|
||||
setAudioPlaying(false);
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
audio.onerror = () => {
|
||||
setAudioPlaying(false);
|
||||
setAudioError(true);
|
||||
};
|
||||
setAudioLoading(false);
|
||||
setAudioPlaying(true);
|
||||
await audio.play();
|
||||
} catch {
|
||||
setAudioLoading(false);
|
||||
setAudioError(true);
|
||||
}
|
||||
}
|
||||
|
||||
function handleToggleInsight() {
|
||||
const opening = !insightOpen;
|
||||
setInsightOpen(opening);
|
||||
// Nur einmal laden
|
||||
if (opening && !insightFetched.current) {
|
||||
insightFetched.current = true;
|
||||
setInsightLoading(true);
|
||||
setInsightText('');
|
||||
setInsightError(false);
|
||||
fetchInsight(
|
||||
match.homeTeam.name,
|
||||
match.awayTeam.name,
|
||||
match.stage,
|
||||
match.group,
|
||||
(chunk) => setInsightText((t) => t + chunk),
|
||||
() => setInsightLoading(false),
|
||||
() => { setInsightLoading(false); setInsightError(true); }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const tendency = getTendency(home, away);
|
||||
const tendencyLabel =
|
||||
tendency === 'home' ? match.homeTeam.shortName || match.homeTeam.name :
|
||||
@@ -172,15 +52,6 @@ export default function TipModal({ match, onClose, onSaved }: Props) {
|
||||
{/* Drag handle */}
|
||||
<div className={styles.handle} />
|
||||
|
||||
{/* Match info header */}
|
||||
<div className={styles.matchHeader}>
|
||||
{match.group && (
|
||||
<span className={styles.groupBadge}>
|
||||
{match.group.replace('GROUP_', 'Gruppe ')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Teams mit Flaggen */}
|
||||
<div className={styles.teamsRow}>
|
||||
<div className={styles.teamBlock}>
|
||||
@@ -193,21 +64,7 @@ export default function TipModal({ match, onClose, onSaved }: Props) {
|
||||
<span className={styles.teamName}>{match.homeTeam.name}</span>
|
||||
</div>
|
||||
|
||||
<div className={styles.vsBlock}>
|
||||
<div className={styles.kickoffBlock}>
|
||||
<span className={styles.kickoffDate}>
|
||||
{new Date(match.utcDate).toLocaleString('de-DE', {
|
||||
weekday: 'short', day: 'numeric', month: 'short',
|
||||
timeZone: 'Europe/Berlin'
|
||||
})}
|
||||
</span>
|
||||
<span className={styles.kickoffTime}>
|
||||
{new Date(match.utcDate).toLocaleString('de-DE', {
|
||||
hour: '2-digit', minute: '2-digit', timeZone: 'Europe/Berlin'
|
||||
})} Uhr
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className={styles.vsBlock} />
|
||||
|
||||
<div className={styles.teamBlock}>
|
||||
<div className={styles.flagLarge}>
|
||||
@@ -240,91 +97,6 @@ export default function TipModal({ match, onClose, onSaved }: Props) {
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Expertenblick */}
|
||||
<div className={styles.insightWrapper}>
|
||||
<div className={styles.insightToggleRow}>
|
||||
<button className={styles.insightToggle} onClick={handleToggleInsight}>
|
||||
<Sparkles size={14} className={styles.insightIcon} />
|
||||
<span>Expertenblick</span>
|
||||
{insightOpen
|
||||
? <ChevronUp size={14} className={styles.insightChevron} />
|
||||
: <ChevronDown size={14} className={styles.insightChevron} />
|
||||
}
|
||||
</button>
|
||||
|
||||
{/* Play-Button – nur wenn Dialog fertig geladen */}
|
||||
{insightOpen && !insightLoading && !insightError && insightText && (
|
||||
<button
|
||||
className={styles.audioBtn}
|
||||
onClick={handlePlayAudio}
|
||||
disabled={audioLoading}
|
||||
title={audioPlaying ? 'Stop' : 'Dialog anhören'}
|
||||
>
|
||||
{audioLoading
|
||||
? <Loader2 size={13} className={styles.insightSpinner} />
|
||||
: audioPlaying
|
||||
? <Square size={13} />
|
||||
: <Volume2 size={13} />
|
||||
}
|
||||
<span>{audioPlaying ? 'Stop' : 'Anhören'}</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{insightOpen && (
|
||||
<div className={styles.insightPanel}>
|
||||
{insightLoading && insightText === '' ? (
|
||||
<div className={styles.insightLoading}>
|
||||
<Loader2 size={15} className={styles.insightSpinner} />
|
||||
<span>Analyse läuft…</span>
|
||||
</div>
|
||||
) : insightError ? (
|
||||
<div className={styles.insightErrorMsg}>
|
||||
Einschätzung gerade nicht verfügbar.
|
||||
</div>
|
||||
) : (
|
||||
<div className={styles.insightText}>
|
||||
{insightText.split('\n').filter(l => l.trim()).map((line, i) => {
|
||||
// Dialog-Format: **Delling:** "..." oder **Netzer:** "..."
|
||||
const dialogMatch = line.match(/^\*\*(Delling|Netzer):\*\*\s*["„]?(.+?)[""]?\s*$/);
|
||||
if (dialogMatch) {
|
||||
const speaker = dialogMatch[1] as 'Delling' | 'Netzer';
|
||||
return (
|
||||
<div key={i} className={`${styles.dialogLine} ${styles[`speaker${speaker}`]}`}>
|
||||
<span className={styles.dialogSpeaker}>{speaker}</span>
|
||||
<span className={styles.dialogText}>„{dialogMatch[2].replace(/^["„]|[""]$/g, '')}"</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
// Fallback: **Label:** Value (alter Stil)
|
||||
const labelMatch = line.match(/^\*\*(.+?):\*\*\s*(.*)$/);
|
||||
if (labelMatch) {
|
||||
const valueParts = labelMatch[2].split(/(\*\*.+?\*\*)/g).map((part, j) =>
|
||||
part.startsWith('**') && part.endsWith('**')
|
||||
? <strong key={j}>{part.slice(2, -2)}</strong>
|
||||
: part
|
||||
);
|
||||
return (
|
||||
<div key={i} className={styles.insightLine}>
|
||||
<span className={styles.insightLabel}>{labelMatch[1]}</span>
|
||||
<span className={styles.insightValue}>{valueParts}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return <div key={i} className={styles.insightLine}>{line}</div>;
|
||||
})}
|
||||
{insightLoading && <span className={styles.insightCursor} />}
|
||||
{audioError && (
|
||||
<div className={styles.insightErrorMsg} style={{ marginTop: '0.5rem' }}>
|
||||
Audio nicht verfügbar.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{error && <div className={styles.error}>{error}</div>}
|
||||
|
||||
{/* CTA */}
|
||||
|
||||
Reference in New Issue
Block a user