All files / src/components TranscriptModal.tsx

65.65% Statements 130/198
66.66% Branches 4/6
14.28% Functions 1/7
65.65% Lines 130/198

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 2301x 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 4x 4x 4x 4x   4x 4x       4x       4x 4x 1x   1x 1x 4x 4x 4x 4x   4x 4x       4x       4x 4x 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  
import {useState} from "react";
import {generateClient} from "aws-amplify/api";
import {jsPDF} from "jspdf";
import {LoaderCircle} from "lucide-react";
import type {Schema} from "../../amplify/data/resource.ts";
import type {BaseTranscriptPart, Font, Transcript, TranscriptComment} from "../../amplify/utils/types.ts";
import {createPdf} from "../../amplify/utils/transcript-pdf.ts";
import {Languages} from "../../amplify/utils/languages.ts";
 
const client = generateClient<Schema>();
 
export interface TranscriptModalProps {
    transcription: string;
    closeModal: () => void;
    transcript: Transcript;
    fonts: Record<string, Font>;
    onEmailSent: () => void;
}
 
function TranscriptModal(props: TranscriptModalProps) {
    const [comments, setComments] = useState<TranscriptComment[]>([]);
    const [downloadingPdf, setDownloadingPdf] = useState(false);
    const [sendingEmail, setSendingEmail] = useState(false);
 
    function addComment(index: number, partIndex: number, isTranslated: boolean) {
        const commentText = prompt("Enter your comment");
        if (commentText) {
            setComments([...comments, {
                text: commentText,
                index,
                partIndex,
                isTranslated,
            }]);
        }
    }
 
    async function generatePdf(): Promise<jsPDF> {
        const languageCode = props.transcript.lastTargetLanguageCode;
        let font: Font | undefined;
        if (languageCode === Languages.ARABIC.transcribeCode) {
            font = props.fonts.notoArabic;
        } else if (languageCode === Languages.CHINESE.transcribeCode
            || languageCode === Languages.RUSSIAN.transcribeCode
        ) {
            font = props.fonts.notoChinese;
        } else if (languageCode === Languages.JAPANESE.transcribeCode) {
            font = props.fonts.notoJapanese;
        } else if (languageCode === Languages.KOREAN.transcribeCode) {
            font = props.fonts.notoKorean;
        } else {
            font = undefined;
        }
 
        return createPdf(
            props.transcript.parts,
            comments,
            font,
        );
    }
 
    async function downloadPDF() {
        setDownloadingPdf(true);
        const pdf = await generatePdf();
        pdf.save("transcript.pdf");
        setDownloadingPdf(false);
    }
 
    async function emailTranscript() {
        setSendingEmail(true);
        try {
            const transcriptParts = props.transcript.parts.map(part => ({
                text: part.text,
                translatedText: part.translatedText,
            } as BaseTranscriptPart));
            await client.queries.emailTranscript({
                transcriptParts: JSON.stringify(transcriptParts),
                comments: JSON.stringify(comments),
                languageCode: props.transcript.lastTargetLanguageCode ?? "",
            });
            props.onEmailSent();
        } catch (error) {
            console.error("Error sending email: ", error);
            alert(error);
        }
        setSendingEmail(false);
    }
 
    return (
        <div className="modal modal-open">
            <div className="modal-box relative max-w-lg">
                <div className="flex items-start">
                    <div className="flex-shrink-0 bg-primary bg-opacity-20 p-3 rounded-full">
                        <svg
                            className="w-6 h-6"
                            fill="none"
                            viewBox="0 0 24 24"
                            strokeWidth="1.5"
                            stroke="currentColor"
                            aria-hidden="true"
                        >
                            <path
                                d="M6 22H18C19.1046 22 20 21.1046 20 20V9.82843C20 9.29799 19.7893 8.78929 19.4142 8.41421L13.5858 2.58579C13.2107 2.21071 12.702 2 12.1716 2H6C4.89543 2 4 2.89543 4 4V20C4 21.1046 4.89543 22 6 22Z"
                                stroke="currentColor"
                                strokeWidth="2"
                                strokeLinecap="round"
                                strokeLinejoin="round"
                            />
                            <path
                                d="M13 2.5V9H19"
                                stroke="currentColor"
                                strokeWidth="2"
                                strokeLinecap="round"
                                strokeLinejoin="round"
                            />
                            <path
                                d="M8 17H15"
                                stroke="currentColor"
                                strokeWidth="2"
                                strokeLinecap="round"
                                strokeLinejoin="round"
                            />
                            <path
                                d="M8 13H15"
                                stroke="currentColor"
                                strokeWidth="2"
                                strokeLinecap="round"
                                strokeLinejoin="round"
                            />
                            <path
                                d="M8 9H9"
                                stroke="currentColor"
                                strokeWidth="2"
                                strokeLinecap="round"
                                strokeLinejoin="round"
                            />
                        </svg>
                    </div>
                    <div className="ml-4">
                        <h3
                            className="text-lg font-medium"
                            id="modal-title"
                            aria-label="Review transcript header"
                        >
                            Review transcript and click to add comments
                        </h3>
                        <div className="mt-2">
                            <div className="bg-base-200 p-4 rounded-md overflow-y-auto max-h-64">
                                {props.transcript.parts.map((part, partIndex) => (
                                    <div key={partIndex} className="mb-2">
                                        {part.text.split(" ").map((word, index) => (
                                            <span
                                                key={index}
                                                className="cursor-pointer"
                                                onClick={() => addComment(index, partIndex, false)}
                                            >
                                                {word}{" "}
                                                {comments.filter(comment =>
                                                    comment.index === index
                                                    && comment.partIndex === partIndex
                                                    && !comment.isTranslated
                                                ).map((comment, commentIndex) => (
                                                    <span key={commentIndex} className="text-error italic">
                                                        [{comment.text}]{" "}
                                                    </span>
                                                ))}
                                            </span>
                                        ))}
 
                                        <div className="text-gray-400">
                                            {part.translatedText.split(" ").map((word, index) => (
                                                <span
                                                    key={index}
                                                    className="cursor-pointer"
                                                    onClick={() => addComment(index, partIndex, true)}
                                                >
                                                    {word}{" "}
                                                    {comments.filter(comment =>
                                                        comment.index === index
                                                        && comment.partIndex === partIndex
                                                        && comment.isTranslated
                                                    ).map((comment, commentIndex) => (
                                                        <span key={commentIndex} className="text-error italic">
                                                            [{comment.text}]{" "}
                                                        </span>
                                                    ))}
                                                </span>
                                            ))}
                                        </div>
                                    </div>
                                ))}
                            </div>
                        </div>
                    </div>
                </div>
                <div className="modal-action">
                    <button
                        className="btn btn-primary"
                        disabled={sendingEmail}
                        onClick={emailTranscript}
                    >
                        Email Transcript
                        {sendingEmail && (
                            <LoaderCircle className="animate-spin"/>
                        )}
                    </button>
                    <button
                        className="btn btn-primary"
                        disabled={downloadingPdf}
                        onClick={downloadPDF}
                    >
                        Download
                        {downloadingPdf && (
                            <LoaderCircle className="animate-spin"/>
                        )}
                    </button>
                    <button
                        className="btn btn-outline"
                        onClick={props.closeModal}
                    >
                        Cancel
                    </button>
                </div>
            </div>
            <div className="modal-backdrop" onClick={props.closeModal}/>
        </div>
    );
}
 
export default TranscriptModal;