All files / components/TextToSpeech index.tsx

67.64% Statements 23/34
60.71% Branches 17/28
85.71% Functions 6/7
67.64% Lines 23/34

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            1x                             20x     20x 20x 9x     20x 1x 1x 1x                                     20x               20x   1x         20x 10x       1x   9x       20x 10x       1x       9x         20x 10x     10x                                                                                                                        
import React, { useCallback, useEffect, useState } from "react";
import ReactGA from "react-ga4";
import { useSpeechSynthesis } from "./hooks/useSpeechSynthesis";
import type { TextToSpeechProps } from "./types";
import * as style from "./index.module.scss";
 
const TextToSpeech: React.FC<TextToSpeechProps> = ({
  text,
  lang = "auto",
  ariaLabel = "記事読み上げ",
}) => {
  const {
    status,
    rate,
    isSupported,
    error,
    speak,
    pause,
    resume,
    stop,
    setRate,
  } = useSpeechSynthesis();
 
  // SSR対応: クライアント側でのみisSupported を評価
  const [mounted, setMounted] = useState(false);
  useEffect(() => {
    setMounted(true);
  }, []);
 
  const handlePlayPause = useCallback(() => {
    if (status === "idle" || status === "loading") {
      speak(text, lang === "auto" ? undefined : `${lang}-JP`);
      ReactGA.event({
        category: "TextToSpeech",
        action: "Start",
      });
    } else Eif (status === "playing") {
      pause();
      ReactGA.event({
        category: "TextToSpeech",
        action: "Pause",
      });
    } else if (status === "paused") {
      resume();
      ReactGA.event({
        category: "TextToSpeech",
        action: "Resume",
      });
    }
  }, [status, text, lang, speak, pause, resume]);
 
  const handleStop = useCallback(() => {
    stop();
    ReactGA.event({
      category: "TextToSpeech",
      action: "Stop",
    });
  }, [stop]);
 
  const handleRateChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      setRate(parseFloat(e.target.value));
    },
    [setRate],
  );
 
  const getPlayPauseIcon = () => {
    switch (status) {
      case "loading":
        return "⏳";
      case "playing":
        return "⏸";
      default:
        return "▶";
    }
  };
 
  const getPlayPauseLabel = () => {
    switch (status) {
      case "loading":
        return "読み込み中...";
      case "playing":
        return "一時停止";
      case "paused":
        return "再開";
      default:
        return "読み上げ開始";
    }
  };
 
  // SSRとブラウザ非対応時は何も表示しない
  if (!mounted || !isSupported) {
    return null;
  }
 
  return (
    <div
      className={style["text-to-speech"]}
      role="region"
      aria-label={ariaLabel}
      data-testid="text-to-speech"
    >
      <div className={style.controls}>
        <button
          type="button"
          className={style["play-button"]}
          onClick={handlePlayPause}
          disabled={status === "loading"}
          aria-label={getPlayPauseLabel()}
          data-testid="play-pause-button"
        >
          <span aria-hidden="true">{getPlayPauseIcon()}</span>
        </button>
 
        {(status === "playing" || status === "paused") && (
          <button
            type="button"
            className={style["stop-button"]}
            onClick={handleStop}
            aria-label="停止"
            data-testid="stop-button"
          >
            <span aria-hidden="true">⏹</span>
          </button>
        )}
 
        <div className={style["rate-control"]}>
          <label htmlFor="speech-rate" className={style["rate-label"]}>
            速度: {rate.toFixed(1)}x
          </label>
          <input
            id="speech-rate"
            type="range"
            min="0.5"
            max="2.0"
            step="0.1"
            value={rate}
            onChange={handleRateChange}
            className={style["rate-slider"]}
            aria-label="読み上げ速度"
            data-testid="rate-slider"
          />
        </div>
      </div>
 
      {error && (
        <div className={style.error} role="alert" data-testid="error-message">
          {error}
        </div>
      )}
    </div>
  );
};
 
export default TextToSpeech;