transcription UI

This commit is contained in:
Igor Loskutov
2026-01-13 17:29:38 -05:00
parent 7ca9cad937
commit 807b954340
2 changed files with 227 additions and 124 deletions

View File

@@ -1,11 +1,10 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import ScrollToBottom from "../../scrollToBottom"; import ScrollToBottom from "../../scrollToBottom";
import { Topic } from "../../webSocketTypes"; import { Topic } from "../../webSocketTypes";
import useParticipants from "../../useParticipants"; import { Box, Flex, Text } from "@chakra-ui/react";
import { Box, Flex, Text, Accordion } from "@chakra-ui/react"; import { formatTime } from "../../../../lib/time";
import { TopicItem } from "./TopicItem"; import { getTopicColor } from "../../../../lib/topicColors";
import { TranscriptStatus } from "../../../../lib/transcript"; import { TranscriptStatus } from "../../../../lib/transcript";
import { featureEnabled } from "../../../../lib/features"; import { featureEnabled } from "../../../../lib/features";
type TopicListProps = { type TopicListProps = {
@@ -18,6 +17,7 @@ type TopicListProps = {
transcriptId: string; transcriptId: string;
status: TranscriptStatus | null; status: TranscriptStatus | null;
currentTranscriptText: any; currentTranscriptText: any;
onTopicClick?: (topicId: string) => void;
}; };
export function TopicList({ export function TopicList({
@@ -27,30 +27,13 @@ export function TopicList({
transcriptId, transcriptId,
status, status,
currentTranscriptText, currentTranscriptText,
onTopicClick,
}: TopicListProps) { }: TopicListProps) {
const [activeTopic, setActiveTopic] = useActiveTopic; const [activeTopic, setActiveTopic] = useActiveTopic;
const [hoveredTopicId, setHoveredTopicId] = useState<string | null>(null);
const [autoscrollEnabled, setAutoscrollEnabled] = useState<boolean>(true); const [autoscrollEnabled, setAutoscrollEnabled] = useState<boolean>(true);
const participants = useParticipants(transcriptId);
const scrollToTopic = () => { const toggleScroll = (element: HTMLElement) => {
const topicDiv = document.getElementById(`topic-${activeTopic?.id}`);
setTimeout(() => {
topicDiv?.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
});
}, 200);
};
useEffect(() => {
if (activeTopic && autoscroll) scrollToTopic();
}, [activeTopic, autoscroll]);
// scroll top is not rounded, heights are, so exact match won't work.
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#determine_if_an_element_has_been_totally_scrolled
const toggleScroll = (element) => {
const bottom = const bottom =
Math.abs( Math.abs(
element.scrollHeight - element.clientHeight - element.scrollTop, element.scrollHeight - element.clientHeight - element.scrollTop,
@@ -61,14 +44,19 @@ export function TopicList({
setAutoscrollEnabled(true); setAutoscrollEnabled(true);
} }
}; };
const handleScroll = (e) => {
toggleScroll(e.target); const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
toggleScroll(e.target as HTMLElement);
};
const scrollToBottom = () => {
const topicsDiv = document.getElementById("topics-scroll-div");
if (topicsDiv) topicsDiv.scrollTop = topicsDiv.scrollHeight;
}; };
useEffect(() => { useEffect(() => {
if (autoscroll) { if (autoscroll) {
const topicsDiv = document.getElementById("scroll-div"); const topicsDiv = document.getElementById("topics-scroll-div");
topicsDiv && toggleScroll(topicsDiv); topicsDiv && toggleScroll(topicsDiv);
} }
}, [activeTopic, autoscroll]); }, [activeTopic, autoscroll]);
@@ -77,37 +65,41 @@ export function TopicList({
if (autoscroll && autoscrollEnabled) scrollToBottom(); if (autoscroll && autoscrollEnabled) scrollToBottom();
}, [topics.length, currentTranscriptText]); }, [topics.length, currentTranscriptText]);
const scrollToBottom = () => {
const topicsDiv = document.getElementById("scroll-div");
if (topicsDiv) topicsDiv.scrollTop = topicsDiv.scrollHeight;
};
const getSpeakerName = (speakerNumber: number) => {
if (!participants.response) return;
return (
participants.response.find(
(participant) => participant.speaker == speakerNumber,
)?.name || `Speaker ${speakerNumber}`
);
};
const requireLogin = featureEnabled("requireLogin");
useEffect(() => { useEffect(() => {
if (autoscroll) { if (autoscroll) {
setActiveTopic(topics[topics.length - 1]); setActiveTopic(topics[topics.length - 1]);
} }
}, [topics, autoscroll]); }, [topics, autoscroll]);
const handleTopicClick = (topic: Topic) => {
setActiveTopic(topic);
if (onTopicClick) {
onTopicClick(topic.id);
}
};
const handleTopicMouseEnter = (topic: Topic) => {
setHoveredTopicId(topic.id);
// If already active, toggle off when mousing over
if (activeTopic?.id === topic.id) {
setActiveTopic(null);
} else {
setActiveTopic(topic);
}
};
const handleTopicMouseLeave = () => {
setHoveredTopicId(null);
};
const requireLogin = featureEnabled("requireLogin");
return ( return (
<Flex <Flex
position={"relative"} position="relative"
w={"100%"} w="full"
h={"95%"} h="200px"
flexDirection={"column"} flexDirection="column"
justify={"center"}
align={"center"}
flexShrink={0} flexShrink={0}
> >
{autoscroll && ( {autoscroll && (
@@ -118,45 +110,71 @@ export function TopicList({
)} )}
<Box <Box
id="scroll-div" id="topics-scroll-div"
overflowY={"auto"} overflowY="auto"
h={"100%"} h="full"
onScroll={handleScroll} onScroll={handleScroll}
width="full" width="full"
> >
{topics.length > 0 && ( {topics.length > 0 && (
<Accordion.Root <Flex direction="column" gap={1} p={2}>
multiple={false} {topics.map((topic, index) => {
collapsible={true} const color = getTopicColor(index);
value={activeTopic ? [activeTopic.id] : []} const isActive = activeTopic?.id === topic.id;
onValueChange={(details) => { const isHovered = hoveredTopicId === topic.id;
const selectedTopicId = details.value[0];
const selectedTopic = selectedTopicId return (
? topics.find((t) => t.id === selectedTopicId) <Flex
: null;
setActiveTopic(selectedTopic || null);
}}
>
{topics.map((topic) => (
<TopicItem
key={topic.id} key={topic.id}
topic={topic} id={`topic-${topic.id}`}
isActive={activeTopic?.id === topic.id} gap={2}
getSpeakerName={getSpeakerName} align="center"
py={1}
px={2}
cursor="pointer"
bg={isActive || isHovered ? "gray.100" : "transparent"}
_hover={{ bg: "gray.50" }}
onClick={() => handleTopicClick(topic)}
onMouseEnter={() => handleTopicMouseEnter(topic)}
onMouseLeave={handleTopicMouseLeave}
>
{/* Color indicator */}
<Box
w="12px"
h="12px"
borderRadius="full"
bg={color}
flexShrink={0}
/> />
))}
</Accordion.Root> {/* Topic title */}
<Text
flex={1}
fontSize="sm"
fontWeight={isActive ? "semibold" : "normal"}
>
{topic.title}
</Text>
{/* Timestamp */}
<Text as="span" color="gray.500" fontSize="xs" flexShrink={0}>
{formatTime(topic.timestamp)}
</Text>
</Flex>
);
})}
</Flex>
)} )}
{status == "recording" && ( {status == "recording" && (
<Box textAlign={"center"}> <Box textAlign="center">
<Text>{currentTranscriptText}</Text> <Text>{currentTranscriptText}</Text>
</Box> </Box>
)} )}
{(status == "recording" || status == "idle") && {(status == "recording" || status == "idle") &&
currentTranscriptText.length == 0 && currentTranscriptText.length == 0 &&
topics.length == 0 && ( topics.length == 0 && (
<Box textAlign={"center"} color="gray"> <Box textAlign="center" color="gray">
<Text> <Text>
Full discussion transcript will appear here after you start Full discussion transcript will appear here after you start
recording. recording.
@@ -167,7 +185,7 @@ export function TopicList({
</Box> </Box>
)} )}
{status == "processing" && ( {status == "processing" && (
<Box textAlign={"center"} color="gray"> <Box textAlign="center" color="gray">
<Text>We are processing the recording, please wait.</Text> <Text>We are processing the recording, please wait.</Text>
{!requireLogin && ( {!requireLogin && (
<span> <span>
@@ -177,12 +195,12 @@ export function TopicList({
</Box> </Box>
)} )}
{status == "ended" && topics.length == 0 && ( {status == "ended" && topics.length == 0 && (
<Box textAlign={"center"} color="gray"> <Box textAlign="center" color="gray">
<Text>Recording has ended without topics being found.</Text> <Text>Recording has ended without topics being found.</Text>
</Box> </Box>
)} )}
{status == "error" && ( {status == "error" && (
<Box textAlign={"center"} color="gray"> <Box textAlign="center" color="gray">
<Text>There was an error processing your recording</Text> <Text>There was an error processing your recording</Text>
</Box> </Box>
)} )}

View File

@@ -3,7 +3,9 @@ import Modal from "../modal";
import useTopics from "../useTopics"; import useTopics from "../useTopics";
import useWaveform from "../useWaveform"; import useWaveform from "../useWaveform";
import useMp3 from "../useMp3"; import useMp3 from "../useMp3";
import useParticipants from "../useParticipants";
import { TopicList } from "./_components/TopicList"; import { TopicList } from "./_components/TopicList";
import { TranscriptWithGutter } from "./_components/TranscriptWithGutter";
import { Topic } from "../webSocketTypes"; import { Topic } from "../webSocketTypes";
import React, { useEffect, useState, use } from "react"; import React, { useEffect, useState, use } from "react";
import FinalSummary from "./finalSummary"; import FinalSummary from "./finalSummary";
@@ -45,14 +47,91 @@ export default function TranscriptDetails(details: TranscriptDetails) {
const mp3 = useMp3(transcriptId, waiting); const mp3 = useMp3(transcriptId, waiting);
const topics = useTopics(transcriptId); const topics = useTopics(transcriptId);
const participants = useParticipants(transcriptId);
const waveform = useWaveform( const waveform = useWaveform(
transcriptId, transcriptId,
waiting || mp3.audioDeleted === true, waiting || mp3.audioDeleted === true,
); );
const useActiveTopic = useState<Topic | null>(null); const useActiveTopic = useState<Topic | null>(null);
const [activeTopic, setActiveTopic] = useActiveTopic;
const [finalSummaryElement, setFinalSummaryElement] = const [finalSummaryElement, setFinalSummaryElement] =
useState<HTMLDivElement | null>(null); useState<HTMLDivElement | null>(null);
// IntersectionObserver for active topic detection based on scroll position
useEffect(() => {
if (!topics.topics || topics.topics.length === 0) return;
const observer = new IntersectionObserver(
(entries) => {
// Find the most visible segment
let mostVisibleEntry: IntersectionObserverEntry | null = null;
let maxRatio = 0;
entries.forEach((entry) => {
if (entry.isIntersecting && entry.intersectionRatio > maxRatio) {
maxRatio = entry.intersectionRatio;
mostVisibleEntry = entry;
}
});
if (mostVisibleEntry) {
// Extract topicId from segment id (format: "segment-{topicId}-{idx}")
const segmentId = mostVisibleEntry.target.id;
const match = segmentId.match(/^segment-([^-]+)-/);
if (match) {
const topicId = match[1];
const topic = topics.topics?.find((t) => t.id === topicId);
if (topic && activeTopic?.id !== topic.id) {
setActiveTopic(topic);
}
}
}
},
{
threshold: [0, 0.25, 0.5, 0.75, 1],
rootMargin: "-20% 0px -20% 0px",
},
);
// Observe all segment elements
const segments = document.querySelectorAll('[id^="segment-"]');
segments.forEach((segment) => observer.observe(segment));
return () => observer.disconnect();
}, [topics.topics, activeTopic?.id, setActiveTopic]);
// Scroll handlers for bidirectional navigation
const handleTopicClick = (topicId: string) => {
// Scroll to first segment of this topic in transcript
const firstSegment = document.querySelector(`[id^="segment-${topicId}-"]`);
if (firstSegment) {
firstSegment.scrollIntoView({
behavior: "smooth",
block: "center",
});
}
};
const handleGutterClick = (topicId: string) => {
// Scroll to topic in list
const topicChip = document.getElementById(`topic-${topicId}`);
if (topicChip) {
topicChip.scrollIntoView({
behavior: "smooth",
block: "center",
});
}
};
const getSpeakerName = (speakerNumber: number) => {
if (!participants.response) return `Speaker ${speakerNumber}`;
return (
participants.response.find(
(participant) => participant.speaker == speakerNumber,
)?.name || `Speaker ${speakerNumber}`
);
};
useEffect(() => { useEffect(() => {
if (!waiting || !transcript.data) return; if (!waiting || !transcript.data) return;
@@ -150,22 +229,17 @@ export default function TranscriptDetails(details: TranscriptDetails) {
)} )}
</> </>
)} )}
<Grid <Flex
templateColumns={{ base: "minmax(0, 1fr)", md: "repeat(2, 1fr)" }} direction="column"
templateRows={{
base: "auto minmax(0, 1fr) minmax(0, 1fr)",
md: "auto minmax(0, 1fr)",
}}
gap={4} gap={4}
gridRowGap={2}
padding={4} padding={4}
paddingBottom={0} paddingBottom={0}
background="gray.bg" background="gray.bg"
border={"2px solid"} border="2px solid"
borderColor={"gray.bg"} borderColor="gray.bg"
borderRadius={8} borderRadius={8}
> >
<GridItem colSpan={{ base: 1, md: 2 }}> {/* Title */}
<Flex direction="column" gap={0}> <Flex direction="column" gap={0}>
<Flex alignItems="center" gap={2}> <Flex alignItems="center" gap={2}>
<TranscriptTitle <TranscriptTitle
@@ -186,7 +260,8 @@ export default function TranscriptDetails(details: TranscriptDetails) {
</Text> </Text>
)} )}
</Flex> </Flex>
</GridItem>
{/* Topics List (top section - fixed height, scrollable) */}
<TopicList <TopicList
topics={topics.topics || []} topics={topics.topics || []}
useActiveTopic={useActiveTopic} useActiveTopic={useActiveTopic}
@@ -194,9 +269,20 @@ export default function TranscriptDetails(details: TranscriptDetails) {
transcriptId={transcriptId} transcriptId={transcriptId}
status={transcript.data?.status || null} status={transcript.data?.status || null}
currentTranscriptText="" currentTranscriptText=""
onTopicClick={handleTopicClick}
/> />
{/* Transcript with colored gutter (bottom section - normal document flow) */}
{topics.topics && topics.topics.length > 0 && (
<TranscriptWithGutter
topics={topics.topics}
getSpeakerName={getSpeakerName}
onGutterClick={handleGutterClick}
/>
)}
{/* Final Summary (at bottom) */}
{transcript.data && topics.topics ? ( {transcript.data && topics.topics ? (
<>
<FinalSummary <FinalSummary
transcript={transcript.data} transcript={transcript.data}
topics={topics.topics} topics={topics.topics}
@@ -205,10 +291,9 @@ export default function TranscriptDetails(details: TranscriptDetails) {
}} }}
finalSummaryRef={setFinalSummaryElement} finalSummaryRef={setFinalSummaryElement}
/> />
</>
) : ( ) : (
<Flex justify={"center"} alignItems={"center"} h={"100%"}> <Flex justify="center" alignItems="center" h="200px">
<div className="flex flex-col h-full justify-center content-center"> <Flex direction="column" h="full" justify="center" align="center">
{transcript?.data?.status == "processing" ? ( {transcript?.data?.status == "processing" ? (
<Text>Loading Transcript</Text> <Text>Loading Transcript</Text>
) : ( ) : (
@@ -217,10 +302,10 @@ export default function TranscriptDetails(details: TranscriptDetails) {
back later back later
</Text> </Text>
)} )}
</div> </Flex>
</Flex> </Flex>
)} )}
</Grid> </Flex>
</Grid> </Grid>
</> </>
); );