File size: 920 Bytes
58490f9
 
 
 
 
1896b82
 
 
58490f9
 
 
 
 
1896b82
 
 
 
58490f9
1896b82
58490f9
1896b82
58490f9
1896b82
58490f9
 
1896b82
 
 
 
 
 
58490f9
1896b82
58490f9
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
"use client"

import { ReactNode, useEffect, useState } from "react"
import { onlyText } from "react-children-utilities"

import { useTimeout } from "@/lib/useTimeout"
import { useStore } from "./useStore"

export function Speak({
  children
}: {
  children: ReactNode
}) {
  const isSpeechSynthesisAvailable = useStore(state => state.isSpeechSynthesisAvailable)
  const lastSpokenSentence = useStore(state => state.lastSpokenSentence)
  const init = useStore(state => state.init)
  const speak = useStore(state => state.speak)

  const newMessage = onlyText(children).trim()

  useEffect(() => { init() }, [])

  const canSpeak = isSpeechSynthesisAvailable && newMessage?.length && newMessage !== lastSpokenSentence
  
  useEffect(() => {
    console.log("debug:", { canSpeak, newMessage })
    if (canSpeak) {
      console.log("speaking!")
      speak(newMessage)
    }
  }, [canSpeak, newMessage])
  
  return null
}