From b70ff46c723bceb95fc18d9bc317b929a442191a Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 9 Jan 2024 15:41:16 +0100 Subject: [PATCH] review fixes --- .../[transcriptId]/correct/topicPlayer.tsx | 17 +++-- .../[transcriptId]/correct/topicWords.tsx | 64 +++++++++++-------- .../[domain]/transcripts/useParticipants.ts | 4 +- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/www/app/[domain]/transcripts/[transcriptId]/correct/topicPlayer.tsx b/www/app/[domain]/transcripts/[transcriptId]/correct/topicPlayer.tsx index 6695a994..bdd9e5ea 100644 --- a/www/app/[domain]/transcripts/[transcriptId]/correct/topicPlayer.tsx +++ b/www/app/[domain]/transcripts/[transcriptId]/correct/topicPlayer.tsx @@ -78,13 +78,16 @@ const TopicPlayer = ({ setEndTopicCallback( () => function () { - if (!topicTime) return; - if (mp3.media && mp3.media.currentTime >= topicTime.end) { - mp3.media.pause(); - setIsPlaying(false); - mp3.media.currentTime = topicTime.start; - calcShowTime(); - } + if ( + !topicTime || + !mp3.media || + !(mp3.media.currentTime >= topicTime.end) + ) + return; + mp3.media.pause(); + setIsPlaying(false); + mp3.media.currentTime = topicTime.start; + calcShowTime(); }, ); if (mp3.media) { diff --git a/www/app/[domain]/transcripts/[transcriptId]/correct/topicWords.tsx b/www/app/[domain]/transcripts/[transcriptId]/correct/topicWords.tsx index cb847f1d..441630f8 100644 --- a/www/app/[domain]/transcripts/[transcriptId]/correct/topicWords.tsx +++ b/www/app/[domain]/transcripts/[transcriptId]/correct/topicWords.tsx @@ -28,22 +28,39 @@ const topicWords = ({ }, [topicWithWords.loading]); const getStartTimeFromFirstNode = (node, offset, reverse) => { - // if the first element is a word - return node.parentElement?.dataset["start"] - ? // but after of the word (like on the blank space right of the word) - node.textContent?.length == offset - ? // if next element is a word, we need the start of it - (node.parentElement?.nextElementSibling as any)?.dataset?.["start"] || - // otherwise we get the start of the first word of the next paragraph - ( - node.parentElement?.parentElement?.nextElementSibling - ?.childNodes[1] as any - )?.dataset?.["start"] || - (reverse ? 0 : 99) - : // otherwise it's just somewhere in the word and we get the start of the word - node.parentElement?.dataset["start"] - : // otherwise selection start is on a name and we get the start of the next word - (node.parentElement?.nextElementSibling as any)?.dataset["start"]; + // Check if the current node represents a word with a start time + if (node.parentElement?.dataset["start"]) { + // Check if the position is at the end of the word + if (node.textContent?.length == offset) { + // Try to get the start time of the next word + const nextWordStartTime = + node.parentElement.nextElementSibling?.dataset["start"]; + if (nextWordStartTime) { + return nextWordStartTime; + } + + // If no next word, get start of the first word in the next paragraph + const nextParaFirstWordStartTime = + node.parentElement.parentElement.nextElementSibling?.childNodes[1] + ?.dataset["start"]; + if (nextParaFirstWordStartTime) { + return nextParaFirstWordStartTime; + } + + // Return default values based on 'reverse' flag + // If reverse is false, means the node is the last word of the topic transcript, + // so reverse should be true, and we set a high value to make sure this is not picked as the start time. + // Reverse being true never happens given how we use this function, but for consistency in case things change, + // we set a low value. + return reverse ? 0 : 9999999999999; + } else { + // Position is within the word, return start of this word + return node.parentElement.dataset["start"]; + } + } else { + // Selection is on a name, return start of the next word + return node.parentElement.nextElementSibling?.dataset["start"]; + } }; const onMouseUp = (e) => { @@ -83,7 +100,6 @@ const topicWords = ({ ? parseInt(focusNode.parentElement?.dataset["speaker"]) : undefined, ); - console.log("Unset Time : selected Speaker"); return; } @@ -107,11 +123,12 @@ const topicWords = ({ const reverse = parseFloat(anchorStart) >= parseFloat(focusEnd); if (!reverse) { - setSelectedText({ - start: parseFloat(anchorStart), - end: parseFloat(focusEnd), - }); - console.log("setting right"); + anchorStart && + focusEnd && + setSelectedText({ + start: parseFloat(anchorStart), + end: parseFloat(focusEnd), + }); } else { const anchorEnd = anchorNode.parentElement?.dataset["end"] || @@ -130,7 +147,6 @@ const topicWords = ({ start: parseFloat(focusStart), end: parseFloat(anchorEnd), }); - console.log("setting reverse"); } } selection && selection.empty(); @@ -144,7 +160,6 @@ const topicWords = ({ )?.name || `Speaker ${speakerNumber}` ); }; - if ( !topicWithWords.loading && topicWithWords.response && @@ -199,7 +214,6 @@ const topicWords = ({ } if (topicWithWords.loading || participants.loading) return ; - if (topicWithWords.error || participants.error) return

error

; return null; }; diff --git a/www/app/[domain]/transcripts/useParticipants.ts b/www/app/[domain]/transcripts/useParticipants.ts index 2d4a1fd0..d1a4b3e1 100644 --- a/www/app/[domain]/transcripts/useParticipants.ts +++ b/www/app/[domain]/transcripts/useParticipants.ts @@ -13,7 +13,7 @@ type ErrorParticipants = { type LoadingParticipants = { response: Participant[] | null; loading: true; - error: false; + error: null; }; type SuccessParticipants = { @@ -63,6 +63,8 @@ const useParticipants = (transcriptId: string): UseParticipants => { setError(error); } setErrorState(error); + setResponse(null); + setLoading(false); }); }, [transcriptId, !api, count]);