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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 18x 17x 11x 17x 6x 6x 6x 6x 6x 6x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 306x 306x 306x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 22x 22x 19x 22x 22x 22x 22x 22x 22x 18x 3x 3x 3x 3x 15x 15x 18x 22x 18x 22x 22x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x | import {useEffect, useState} from "react";
import type {VoiceId} from "@aws-sdk/client-polly";
import type {Font, Language, Transcript, TtsVoice} from "../../amplify/utils/types.ts";
import {Languages} from "../../amplify/utils/languages.ts";
import {PlayIcon, StopIcon} from "../assets/icons";
import {useDialog} from "../hooks/use-dialog.ts";
import Alert from "./Alert.tsx";
import TranscriptModal from "./TranscriptModal.tsx";
export interface ControlsProps {
isLoggedIn: boolean;
isLoading: boolean;
isTranslating: boolean;
onToggleTranslation: () => void;
targetLanguage: Language | null;
onChangeTargetLanguage: (language: Language) => void;
selectedVoices: Record<string, TtsVoice>;
onChangeVoice: (voiceId: VoiceId) => void;
transcript: Transcript;
onClearTranscript: () => void;
/**
* For test mocking purposes.
*/
fonts?: Record<string, Font>;
}
function Controls(props: ControlsProps) {
return (
<div className="w-full md:w-72 p-2 bg-base-100 rounded-lg shadow-lg">
<div className="max-h-40 md:h-0 min-h-full p-4 overflow-auto">
<ToggleTranslationButton
isLoggedIn={props.isLoggedIn}
isLoading={props.isLoading}
isTranslating={props.isTranslating}
onToggleTranslation={props.onToggleTranslation}
targetLanguage={props.targetLanguage}
/>
<LanguageSelector
isTranslating={props.isTranslating}
targetLanguage={props.targetLanguage}
onChangeTargetLanguage={props.onChangeTargetLanguage}
/>
<VoiceSelector
targetLanguage={props.targetLanguage}
selectedVoices={props.selectedVoices}
onChangeVoice={props.onChangeVoice}
/>
<ReviewButton
transcript={props.transcript}
fonts={props.fonts}
/>
<ClearTranscriptButton
transcript={props.transcript}
onClearTranscript={props.onClearTranscript}
/>
</div>
</div>
);
}
interface ToggleTranslationButtonProps {
isLoggedIn: boolean;
isLoading: boolean;
isTranslating: boolean;
onToggleTranslation: () => void;
targetLanguage: Language | null;
}
function ToggleTranslationButton(props: ToggleTranslationButtonProps) {
return (
<div className="flex justify-center mb-6">
<button
className={`btn w-full ${props.isTranslating ? "btn-error" : "btn-success"}`}
aria-label="Toggle translation button"
disabled={!props.isLoggedIn || props.isLoading || props.targetLanguage === null}
onClick={props.onToggleTranslation}
>
{(() => {
if (!props.isLoggedIn) {
return "Please login";
} else if (props.isLoading) {
return "Loading...";
} else if (props.targetLanguage === null) {
return "Select a language";
} else if (props.isTranslating) {
return (
<>
<StopIcon className="mr-2 h-5 w-5"/>
Stop Translation
</>
);
} else {
return (
<>
<PlayIcon className="mr-2 h-5 w-5"/>
Start Translation
</>
);
}
})()}
</button>
</div>
);
}
interface LanguageSelectorProps {
isTranslating: boolean;
targetLanguage: Language | null;
onChangeTargetLanguage: (language: Language) => void;
}
function LanguageSelector(props: LanguageSelectorProps) {
const languagesNoEnglish = Languages.ALL.filter(language => language !== Languages.ENGLISH);
return (
<div className="space-y-2 mb-6">
<div>
<h3 className="font-bold text-lg">
Target Language
</h3>
</div>
<select
className="select select-bordered w-full"
aria-label="Select language"
defaultValue="default"
disabled={props.isTranslating}
onChange={e => props.onChangeTargetLanguage(languagesNoEnglish[parseInt(e.target.value)])}
>
<option disabled hidden value="default">
Select a language
</option>
{languagesNoEnglish.map((language, index) =>
<option key={index} value={index}>
{language.name}
</option>
)}
</select>
<p aria-label="Selected language status">
The currently selected language is: {props.targetLanguage?.name ?? "none"}
</p>
</div>
);
}
interface VoiceSelectorProps {
targetLanguage: Language | null;
selectedVoices: Record<string, TtsVoice | undefined>;
onChangeVoice: (voiceId: VoiceId) => void;
}
function VoiceSelector(props: VoiceSelectorProps) {
const selectedVoice = props.selectedVoices[props.targetLanguage?.name ?? ""];
const voices = props.targetLanguage?.ttsVoices ?? [];
return (
<div className="space-y-2 mb-6">
<div>
<h3 className="font-bold text-lg">
Voice
</h3>
</div>
<select
className="select select-bordered w-full"
aria-label="Select voice"
disabled={props.targetLanguage === null}
value={selectedVoice?.id}
onChange={e => props.onChangeVoice(e.target.value as VoiceId)}
>
{voices.map((voice, index) =>
<option key={index} value={voice.id}>
{voice.name ?? voice.id}
</option>
)}
</select>
<p aria-label="Selected voice status">
The currently selected voice is: {(selectedVoice?.name ?? selectedVoice?.id) ?? "none"}
</p>
</div>
);
}
interface ReviewButtonProps {
transcript: Transcript;
/**
* For test mocking purposes.
*/
fonts?: Record<string, Font>;
}
function ReviewButton(props: ReviewButtonProps) {
const transcript = props.transcript.parts.map(part =>
part.text + "\n" + part.translatedText
).join("\n\n");
const [isModalOpen, setIsModalOpen] = useState(false);
const [fontsLoaded, setFontsLoaded] = useState(false);
const [fonts, setFonts] = useState<Record<string, Font>>({});
const [showEmailedAlert, setShowEmailedAlert] = useState(false);
async function loadFonts() {
if (props.fonts !== undefined) {
setFonts(props.fonts);
setFontsLoaded(true);
return;
}
const {notoArabic} = await import("../../amplify/fonts/noto-arabic-normal");
const {notoChinese} = await import("../../amplify/fonts/noto-chinese-normal");
const {notoJapanese} = await import("../../amplify/fonts/noto-japanese-normal");
const {notoKorean} = await import("../../amplify/fonts/noto-korean-normal");
const newFonts = {
notoArabic,
notoChinese,
notoJapanese,
notoKorean,
};
setFonts(newFonts);
setFontsLoaded(true);
}
useEffect(() => {
// noinspection JSIgnoredPromiseFromCall
loadFonts();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
function openModal() {
setIsModalOpen(true);
}
function closeModal() {
setIsModalOpen(false);
}
return (
<>
<button
className="btn btn-primary w-full mb-6"
aria-label="Review button"
disabled={!fontsLoaded || props.transcript.parts.length === 0}
onClick={openModal}
>
{fontsLoaded ? "Review" : "Loading..."}
</button>
{isModalOpen && (
<TranscriptModal
transcription={transcript}
closeModal={closeModal}
transcript={props.transcript}
fonts={fonts}
onEmailSent={() => setShowEmailedAlert(true)}
/>
)}
<Alert
message="Email sent!"
isVisible={showEmailedAlert}
onDismiss={() => setShowEmailedAlert(false)}
autoDismissTime={3000}
/>
</>
);
}
interface ClearTranscriptButtonProps {
transcript: Transcript;
onClearTranscript: () => void;
}
function ClearTranscriptButton(props: ClearTranscriptButtonProps) {
const {dialogRef, openDialog} = useDialog();
function clearTranscript() {
props.onClearTranscript();
}
return (
<>
<button
className="btn btn-error w-full"
aria-label="Clear transcript button"
disabled={props.transcript.parts.length === 0}
onClick={openDialog}
>
Clear transcript
</button>
<dialog ref={dialogRef} className="modal">
<div className="modal-box p-4">
<div className="flex w-full flex-col">
<h1 className="font-bold text-xl mb-4">
Are you sure you want to clear the transcript?
</h1>
<form method="dialog">
<div className="grid grid-cols-2 gap-4">
<button
className="btn btn-error"
onClick={clearTranscript}
>
Clear transcript
</button>
<button className="btn btn-primary">
Cancel
</button>
</div>
</form>
</div>
</div>
</dialog>
</>
);
}
export default Controls;
|