"use client"; import { useState } from "react"; import ReactMarkdown from "react-markdown"; import rehypeRaw from "rehype-raw"; interface TranscriptViewerProps { transcriptContent: string; transcriptUrl?: string | null; } /** * Format the transcript content into a more readable format with styling */ function formatTranscript(content: string): React.ReactNode[] { if (!content.trim()) { return [

No transcript content available.

]; } // Split the transcript by lines const lines = content.split("\n"); const elements: React.ReactNode[] = []; let currentSpeaker: string | null = null; let currentMessages: string[] = []; let currentTimestamp: string | null = null; // Process each line lines.forEach((line) => { line = line.trim(); if (!line) { // Empty line, ignore return; } // Check if this is a new speaker line with or without datetime // Format 1: [29.05.2025 21:26:44] User: message // Format 2: User: message const datetimeMatch = line.match( /^\[([^\]]+)\]\s*(User|Assistant):\s*(.*)$/ ); const simpleMatch = line.match(/^(User|Assistant):\s*(.*)$/); if (datetimeMatch || simpleMatch) { // If we have accumulated messages for a previous speaker, add them if (currentSpeaker && currentMessages.length > 0) { elements.push(
{currentTimestamp && (
{currentTimestamp}
)} {currentMessages.map((msg, i) => ( // Use ReactMarkdown to render each message part ( ), }} > {msg} ))}
); currentMessages = []; } if (datetimeMatch) { // Format with datetime: [29.05.2025 21:26:44] User: message currentTimestamp = datetimeMatch[1]; currentSpeaker = datetimeMatch[2]; const messageContent = datetimeMatch[3].trim(); if (messageContent) { currentMessages.push(messageContent); } } else if (simpleMatch) { // Format without datetime: User: message currentTimestamp = null; currentSpeaker = simpleMatch[1]; const messageContent = simpleMatch[2].trim(); if (messageContent) { currentMessages.push(messageContent); } } } else if (currentSpeaker) { // This is a continuation of the current speaker's message currentMessages.push(line); } }); // Add any remaining messages if (currentSpeaker && currentMessages.length > 0) { elements.push(
{currentTimestamp && (
{currentTimestamp}
)} {currentMessages.map((msg, i) => ( // Use ReactMarkdown to render each message part (
), }} > {msg} ))}
); } return elements; } /** * Component to display a chat transcript */ export default function TranscriptViewer({ transcriptContent, transcriptUrl, }: TranscriptViewerProps) { const [showRaw, setShowRaw] = useState(false); const formattedElements = formatTranscript(transcriptContent); // Hide "View Full Raw" button in production environment const isProduction = process.env.NODE_ENV === "production"; return (

Session Transcript

{transcriptUrl && !isProduction && ( View Full Raw )}
{showRaw ? (
          {transcriptContent}
        
) : (
{formattedElements.length > 0 ? ( formattedElements ) : (

No transcript content available.

)}
)}
); }