-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use highlight js instead of syntax highlighter (#538)
* use highlight js instead of syntax highlighter * fix test * prettify code snippets * remove unused prop
- Loading branch information
1 parent
f2974b2
commit c17ec59
Showing
13 changed files
with
705 additions
and
724 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
frontend/src/components/organisms/chat/inputBox/speechButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useEffect, useState } from 'react'; | ||
import SpeechRecognition, { | ||
useSpeechRecognition | ||
} from 'react-speech-recognition'; | ||
|
||
import KeyboardVoiceIcon from '@mui/icons-material/KeyboardVoice'; | ||
import StopCircleIcon from '@mui/icons-material/StopCircle'; | ||
import { IconButton, Tooltip } from '@mui/material'; | ||
|
||
interface Props { | ||
onSpeech: (text: string) => void; | ||
language?: string; | ||
disabled?: boolean; | ||
} | ||
|
||
const SpeechButton = ({ onSpeech, language, disabled }: Props) => { | ||
const { transcript, browserSupportsSpeechRecognition } = | ||
useSpeechRecognition(); | ||
const [isRecording, setIsRecording] = useState(false); | ||
const [lastTranscript, setLastTranscript] = useState(''); | ||
const [timer, setTimer] = useState<NodeJS.Timeout | null>(null); | ||
|
||
useEffect(() => { | ||
if (lastTranscript.length < transcript.length) { | ||
onSpeech(transcript.slice(lastTranscript.length)); | ||
} | ||
setLastTranscript(transcript); | ||
}, [transcript]); | ||
|
||
useEffect(() => { | ||
if (isRecording) { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
setTimer( | ||
setTimeout(() => { | ||
setIsRecording(false); | ||
SpeechRecognition.stopListening(); | ||
}, 2000) // stop after 3 seconds of silence | ||
); | ||
} | ||
}, [transcript, isRecording]); | ||
|
||
if (!browserSupportsSpeechRecognition) { | ||
return null; | ||
} | ||
|
||
return isRecording ? ( | ||
<Tooltip title="Stop recording"> | ||
<span> | ||
<IconButton | ||
disabled={disabled} | ||
color="inherit" | ||
onClick={() => { | ||
setIsRecording(false); | ||
SpeechRecognition.stopListening(); | ||
}} | ||
> | ||
<StopCircleIcon /> | ||
</IconButton> | ||
</span> | ||
</Tooltip> | ||
) : ( | ||
<Tooltip title="Start recording"> | ||
<span> | ||
<IconButton | ||
disabled={disabled} | ||
color="inherit" | ||
onClick={() => { | ||
setIsRecording(true); | ||
SpeechRecognition.startListening({ | ||
continuous: true, | ||
language: language | ||
}); | ||
}} | ||
> | ||
<KeyboardVoiceIcon /> | ||
</IconButton> | ||
</span> | ||
</Tooltip> | ||
); | ||
}; | ||
export default SpeechButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.