Skip to content

Commit 82f4949

Browse files
committed
voice stuff
1 parent 6a65d5e commit 82f4949

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"react-markdown": "^10.1.0",
1717
"react-router-dom": "^6.28.1",
1818
"react-scripts": "5.0.1",
19+
"react-speech-recognition": "^4.0.0",
1920
"rehype-highlight": "^7.0.2",
2021
"rehype-katex": "^7.0.1",
2122
"rehype-raw": "^7.0.0",

client/src/components/layout/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './index.css';
33
import { Outlet } from 'react-router-dom';
44
import SideBarNav from '../main/sideBarNav';
55
import Header from '../header';
6+
import VoiceNavigator from '../main/voiceRecognition';
67

78
/**
89
* Main component represents the layout of the main page, including a sidebar and the main content area.
@@ -13,6 +14,7 @@ const Layout = () => (
1314
<div id='main' className='main'>
1415
<SideBarNav />
1516
<div id='right_main' className='right_main'>
17+
<VoiceNavigator />
1618
<Outlet />
1719
</div>
1820
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import useVoiceRecognition from '../../../hooks/useVoiceRecognition';
2+
3+
const VoiceNavigator = () => {
4+
const { transcript, listening, browserSupportsSpeechRecognition, startL, stopL } =
5+
useVoiceRecognition();
6+
7+
if (!browserSupportsSpeechRecognition) {
8+
return <p>Your browser doesn&apos;t support speech recognition.</p>;
9+
}
10+
11+
return (
12+
<div style={{ padding: '1rem', background: '#222', color: '#fff' }}>
13+
<p>Status: {listening ? '🎙️ Listening...' : '🛑 Not listening'}</p>
14+
<button onClick={startL} style={{ marginRight: '1rem' }}>
15+
Start Listening
16+
</button>
17+
<button onClick={stopL}>Stop Listening</button>
18+
19+
<div style={{ marginTop: '1rem', fontStyle: 'italic' }}>
20+
<strong>Transcript:</strong> {transcript}
21+
</div>
22+
</div>
23+
);
24+
};
25+
26+
export default VoiceNavigator;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';
2+
import { useEffect } from 'react';
3+
import { useNavigate } from 'react-router-dom';
4+
5+
const useVoiceRecognition = () => {
6+
const navigate = useNavigate();
7+
const startL = () => SpeechRecognition.startListening({ continuous: true });
8+
const stopL = () => SpeechRecognition.stopListening();
9+
10+
const commands = [
11+
{
12+
command: ['Go to home', 'Open home', 'Home page'],
13+
callback: () => navigate('/home'),
14+
},
15+
{
16+
command: [
17+
'Open chats',
18+
'Go to chats',
19+
'Chat page',
20+
'Chats',
21+
'Messages',
22+
'Go to messages',
23+
'Messages page',
24+
],
25+
callback: () => navigate('/messages'),
26+
},
27+
{
28+
command: ['Open questions', 'Go to questions', 'Questions', 'Question page'],
29+
callback: () => navigate(`/questionPage`),
30+
},
31+
{
32+
command: ['Open tags', 'Go to tags', 'Tags', 'Tags page'],
33+
callback: () => navigate(`/tags`),
34+
},
35+
{
36+
command: ['Open users', 'Go to users', 'Users', 'Users page'],
37+
callback: () => navigate(`/users`),
38+
},
39+
{
40+
command: ['Open games', 'Go to games', 'Games', 'Games page'],
41+
callback: () => navigate(`/games`),
42+
},
43+
{
44+
command: ['Open communities', 'Go to communities', 'Communities', 'Communities page'],
45+
callback: () => navigate(`/communities/all`),
46+
},
47+
{
48+
command: ['Go back', 'Navigate back'],
49+
callback: () => navigate(-1),
50+
},
51+
{
52+
command: 'Stop listening',
53+
callback: () => SpeechRecognition.stopListening(),
54+
},
55+
];
56+
57+
const { transcript, listening, browserSupportsSpeechRecognition } = useSpeechRecognition({
58+
commands,
59+
});
60+
61+
useEffect(() => {
62+
if (browserSupportsSpeechRecognition) {
63+
SpeechRecognition.startListening({ continuous: true });
64+
}
65+
}, [browserSupportsSpeechRecognition]);
66+
67+
return {
68+
transcript,
69+
listening,
70+
browserSupportsSpeechRecognition,
71+
startL,
72+
stopL,
73+
};
74+
};
75+
76+
export default useVoiceRecognition;

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
"react-hotkeys-hook": "^4.6.1",
1818
"react-icons": "^5.5.0",
1919
"react-markdown": "^10.1.0",
20+
"react-speech-recognition": "^4.0.0",
2021
"rehype-highlight": "^7.0.2",
2122
"rehype-katex": "^7.0.1",
2223
"rehype-raw": "^7.0.0",
2324
"rehype-sanitize": "^6.0.0",
2425
"remark-gfm": "^4.0.1",
2526
"remark-math": "^6.0.0"
27+
},
28+
"devDependencies": {
29+
"@types/react-speech-recognition": "^3.9.6"
2630
}
2731
}

0 commit comments

Comments
 (0)