Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x | import {type MouseEvent, useRef, useState} from "react";
import type {VoiceId} from "@aws-sdk/client-polly";
import {MicOff, Volume2} from "lucide-react";
import type {Phrase, Transcript, TtsVoice} from "../../amplify/utils/types.ts";
import {Languages} from "../../amplify/utils/languages.ts";
import {textToSpeech} from "../utils/text-to-speech.ts";
import {usePopup} from "./Popup.tsx";
export interface TranscriptProps {
transcript: Transcript;
isTranslating: boolean;
selectedVoices: Record<string, TtsVoice>;
ttsPlaying: boolean;
onTtsPlaying: (playing: boolean) => void;
}
function TranscriptBox(props: TranscriptProps) {
const {showPopup} = usePopup();
const showDisambiguationPopup = (e: MouseEvent, alternateDefinition: string) => {
if (alternateDefinition) {
showPopup(
e.clientX,
e.clientY,
<div>
<h3 className="card-title">
Alternative Translation
</h3>
<p className={"text-sm"}>
This could also mean:
</p>
<p>
"{alternateDefinition}"
</p>
</div>,
);
}
};
return (
<div className="flex-1 bg-base-100 rounded-lg shadow-lg p-4">
<div className="w-full h-full relative">
<div className="w-full h-full absolute top-0 left-0">
<div className="max-h-96 md:h-0 min-h-full overflow-auto">
<div className="mr-4">
{props.transcript.parts.map((part, index) => {
let className: string | undefined;
if (index === props.transcript.parts.length - 1) {
className = undefined;
} else {
className = "mb-4";
}
let showPlayIconFirst: boolean;
let showPlayIconSecond: boolean;
if (part.language === Languages.ENGLISH) {
showPlayIconFirst = false;
showPlayIconSecond = true;
} else {
showPlayIconFirst = true;
showPlayIconSecond = false;
}
const ambiguity = addAmbiguityInformation(
part.ambiguousWords,
part.translatedText,
).split("*");
const ambiguousWordMap = new Map();
part.ambiguousWords.forEach(amb => {
ambiguousWordMap.set(amb.text, amb.alternateDefinition);
});
return (
<div key={index} className={className}>
<div className="flex flex-row">
<PlayTtsButton
text={part.text}
voice={props.selectedVoices[part.language.name].id}
anyPlaying={props.ttsPlaying}
onPlaying={props.onTtsPlaying}
visible={showPlayIconFirst}
/>
<p className="mb-0" aria-label={`Transcript part ${index + 1}`}>
{part.text}
</p>
</div>
<div className="flex flex-row">
<PlayTtsButton
text={part.translatedText}
voice={props.selectedVoices[part.translatedLanguage.name].id}
anyPlaying={props.ttsPlaying}
onPlaying={props.onTtsPlaying}
visible={showPlayIconSecond}
/>
<div className="flex-wrap items-center">
{ambiguity.map(amb => {
const ambText = amb.replace("(", "").replace(")", "");
const ambAlternateDefinition: string = ambiguousWordMap.get(ambText);
return (
<span
key={ambiguity.indexOf(amb)}
className={ambiguity.indexOf(amb) % 2 == 1 && ambAlternateDefinition ? "text-accent mx-0.5 " : "text-gray-400 "}
role="textbox"
aria-label={`Translated transcript part ${index + 1}`}
onClick={e => showDisambiguationPopup(e, ambAlternateDefinition)}
>
{ambText}
</span>
);
})}
</div>
</div>
</div>
);
})}
</div>
</div>
</div>
<div className="absolute top-2 right-2 pointer-events-none">
<MicOff
className={`transition duration-300 text-red-600 ${props.ttsPlaying && props.isTranslating ? "" : "opacity-0"}`}
/>
</div>
</div>
</div>
);
}
interface PlayTtsButtonProps {
text: string;
voice: VoiceId;
anyPlaying: boolean;
onPlaying: (playing: boolean) => void;
visible: boolean;
}
function PlayTtsButton(props: PlayTtsButtonProps) {
const [isPlaying, setIsPlaying] = useState(false);
const audioRef = useRef<HTMLAudioElement | null>(null);
async function toggleTts() {
if (isPlaying) {
audioRef.current?.pause();
audioRef.current = null;
setIsPlaying(false);
props.onPlaying(false);
return;
}
if (props.anyPlaying) {
return;
}
setIsPlaying(true);
props.onPlaying(true);
const audio = await textToSpeech(props.text, props.voice);
audio.onended = () => {
audioRef.current = null;
setIsPlaying(false);
props.onPlaying(false);
};
audioRef.current = audio;
}
let className: string;
if (!props.visible) {
className = "invisible";
} else if (isPlaying) {
className = "text-green-600 cursor-pointer hover:text-green-700";
} else if (props.anyPlaying) {
className = "opacity-50 cursor-not-allowed";
} else {
className = "hover:text-gray-500 cursor-pointer";
}
return (
<Volume2
className={`mr-2 shrink-0 transition duration-200 ${className}`}
onClick={toggleTts}
/>
);
}
function addAmbiguityInformation(phrases: Phrase[], text: string): string {
let finalText = text;
for (const phrase of phrases) {
const index = text.indexOf(phrase.text);
const temp = splitAtIndex(finalText, index, false);
finalText = splitAtIndex(temp, index + phrase.text.length + 2, true);
}
return finalText;
}
function splitAtIndex(value: string, index: number, end: boolean) {
if (end) {
return value.substring(0, index) + ")*" + value.substring(index);
} else {
return value.substring(0, index) + "*(" + value.substring(index);
}
}
export default TranscriptBox;
|