From cd873274302fe8365fb437d8294092bb16dd4e5c Mon Sep 17 00:00:00 2001 From: Lynchee <125778696+lynchee-owo@users.noreply.github.com> Date: Sat, 29 Jul 2023 02:33:27 -0700 Subject: [PATCH] created three routes (#216) --- client/web/package-lock.json | 61 ++++ client/web/package.json | 1 + client/web/src/App.jsx | 266 ++++++------------ client/web/src/components/CallView/index.jsx | 11 +- client/web/src/components/Languages/index.jsx | 2 +- .../web/src/components/MediaDevices/index.jsx | 2 +- client/web/src/components/Models/index.jsx | 2 +- client/web/src/components/TextView/index.js | 10 +- client/web/src/pages/Conversation.jsx | 87 ++++++ client/web/src/pages/Home.jsx | 70 +++++ client/web/src/pages/Settings.jsx | 84 ++++++ client/web/src/pages/styles.css | 3 + 12 files changed, 410 insertions(+), 189 deletions(-) create mode 100644 client/web/src/pages/Conversation.jsx create mode 100644 client/web/src/pages/Home.jsx create mode 100644 client/web/src/pages/Settings.jsx create mode 100644 client/web/src/pages/styles.css diff --git a/client/web/package-lock.json b/client/web/package-lock.json index 34f17731f..4ee5646ce 100644 --- a/client/web/package-lock.json +++ b/client/web/package-lock.json @@ -16,6 +16,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-icons": "^4.10.1", + "react-router-dom": "^6.14.2", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" } @@ -6253,6 +6254,14 @@ "integrity": "sha512-K0aGNn1TjalKj+65D7ycc1//H9roAQ51GJVk5ZJQFb2teECGmzd86bYDC0aYdbRf7gtovescq4Zt6FR0tgXiHQ==", "peer": true }, + "node_modules/@remix-run/router": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.7.2.tgz", + "integrity": "sha512-7Lcn7IqGMV+vizMPoEl5F0XDshcdDYtMI6uJLQdQz5CfZAwy3vvGKYSUk789qndt5dEC4HfSjviSYlSoHGL2+A==", + "engines": { + "node": ">=14" + } + }, "node_modules/@rollup/plugin-babel": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz", @@ -21035,6 +21044,36 @@ "node": ">=0.10.0" } }, + "node_modules/react-router": { + "version": "6.14.2", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.14.2.tgz", + "integrity": "sha512-09Zss2dE2z+T1D03IheqAFtK4UzQyX8nFPWx6jkwdYzGLXd5ie06A6ezS2fO6zJfEb/SpG6UocN2O1hfD+2urQ==", + "dependencies": { + "@remix-run/router": "1.7.2" + }, + "engines": { + "node": ">=14" + }, + "peerDependencies": { + "react": ">=16.8" + } + }, + "node_modules/react-router-dom": { + "version": "6.14.2", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.14.2.tgz", + "integrity": "sha512-5pWX0jdKR48XFZBuJqHosX3AAHjRAzygouMTyimnBPOLdY3WjzUSKhus2FVMihUFWzeLebDgr4r8UeQFAct7Bg==", + "dependencies": { + "@remix-run/router": "1.7.2", + "react-router": "6.14.2" + }, + "engines": { + "node": ">=14" + }, + "peerDependencies": { + "react": ">=16.8", + "react-dom": ">=16.8" + } + }, "node_modules/react-scripts": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", @@ -29356,6 +29395,11 @@ "integrity": "sha512-K0aGNn1TjalKj+65D7ycc1//H9roAQ51GJVk5ZJQFb2teECGmzd86bYDC0aYdbRf7gtovescq4Zt6FR0tgXiHQ==", "peer": true }, + "@remix-run/router": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.7.2.tgz", + "integrity": "sha512-7Lcn7IqGMV+vizMPoEl5F0XDshcdDYtMI6uJLQdQz5CfZAwy3vvGKYSUk789qndt5dEC4HfSjviSYlSoHGL2+A==" + }, "@rollup/plugin-babel": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz", @@ -40199,6 +40243,23 @@ "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.11.0.tgz", "integrity": "sha512-F27qZr8uUqwhWZboondsPx8tnC3Ct3SxZA3V5WyEvujRyyNv0VYPhoBg1gZ8/MV5tubQp76Trw8lTv9hzRBa+A==" }, + "react-router": { + "version": "6.14.2", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.14.2.tgz", + "integrity": "sha512-09Zss2dE2z+T1D03IheqAFtK4UzQyX8nFPWx6jkwdYzGLXd5ie06A6ezS2fO6zJfEb/SpG6UocN2O1hfD+2urQ==", + "requires": { + "@remix-run/router": "1.7.2" + } + }, + "react-router-dom": { + "version": "6.14.2", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.14.2.tgz", + "integrity": "sha512-5pWX0jdKR48XFZBuJqHosX3AAHjRAzygouMTyimnBPOLdY3WjzUSKhus2FVMihUFWzeLebDgr4r8UeQFAct7Bg==", + "requires": { + "@remix-run/router": "1.7.2", + "react-router": "6.14.2" + } + }, "react-scripts": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", diff --git a/client/web/package.json b/client/web/package.json index 3666b4bba..46b624061 100644 --- a/client/web/package.json +++ b/client/web/package.json @@ -11,6 +11,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-icons": "^4.10.1", + "react-router-dom": "^6.14.2", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" }, diff --git a/client/web/src/App.jsx b/client/web/src/App.jsx index 588079d58..63b0599aa 100644 --- a/client/web/src/App.jsx +++ b/client/web/src/App.jsx @@ -5,55 +5,47 @@ */ import React, { useState, useRef, useEffect } from 'react'; +import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; import './App.css'; -import {isIP} from 'is-ip'; // Components import Header from './components/Header'; import Footer from './components/Footer'; -import MobileWarning from './components/MobileWarning'; -import MediaDevices from './components/MediaDevices'; -import TextView from './components/TextView'; -import CallView from './components/CallView'; -import Button from './components/Common/Button'; -import Characters from './components/Characters'; -import { sendTokenToServer, signInWithGoogle } from './components/Auth/SignIn'; -import Models from './components/Models'; -import Languages from './components/Languages'; +import { signInWithGoogle } from './components/Auth/SignIn'; + +// Pages +import Settings from './pages/Settings'; +import Conversation from './pages/Conversation'; +import Home from './pages/Home'; + +// utils +import auth from './utils/firebase'; // Custom hooks import useWebsocket from './hooks/useWebsocket'; import useMediaRecorder from './hooks/useMediaRecorder'; import useSpeechRecognition from './hooks/useSpeechRecognition'; -// utils -import auth from './utils/firebase'; - const App = () => { - const isMobile = window.innerWidth <= 768; - const [headerText, setHeaderText] = useState("Choose your partner"); + const [preferredLanguage, setPreferredLanguage] = useState("English"); const [selectedDevice, setSelectedDevice] = useState(""); - const [characterConfirmed, setCharacterConfirmed] = useState(false); - const [isCallView, setIsCallView] = useState(false); - const [isPlaying, setIsPlaying] = useState(false); - const [selectedCharacter, setSelectedCharacter] = useState(null); - const [characterGroups, setCharacterGroups] = useState([]); - const [textAreaValue, setTextAreaValue] = useState(''); - const [messageInput, setMessageInput] = useState(''); const [selectedModel, setSelectedModel] = useState("gpt-3.5-turbo-16k"); + const [useSearch, setUseSearch] = useState(false); const [user, setUser] = useState(null); + const isLoggedIn = useRef(false); const [token, setToken] = useState(""); - const [useSearch, setUseSearch] = useState(false); - const [preferredLanguage, setPreferredLanguage] = useState("English"); - + const [isPlaying, setIsPlaying] = useState(false); + const [selectedCharacter, setSelectedCharacter] = useState(null); + const [messageInput, setMessageInput] = useState(''); + const [isCallView, setIsCallView] = useState(false); + const [textAreaValue, setTextAreaValue] = useState(''); const audioPlayer = useRef(null); const callActive = useRef(false); const audioSent = useRef(false); const shouldPlayAudio = useRef(false); const audioQueue = useRef([]); const isConnected = useRef(false); - const isLoggedIn = useRef(false); - + useEffect(() => { auth.onAuthStateChanged(async user => { @@ -68,28 +60,14 @@ const App = () => { }) }, []) - useEffect(() => { - // Get host - const scheme = window.location.protocol; - var currentHost = window.location.host; - var parts = currentHost.split(':'); - var hostname = parts[0]; - // Local deployment uses 8000 port by default. - var newPort = '8000'; - - if (!(hostname === 'localhost' || isIP(hostname))) { - hostname = 'api.' + hostname; - newPort = window.location.protocol === "https:" ? 443 : 80; + const stopAudioPlayback = () => { + if (audioPlayer.current) { + audioPlayer.current.pause(); + shouldPlayAudio.current = false; } - var newHost = hostname + ':' + newPort + '/characters'; - const url = scheme + '//' + newHost; - - // Get characters - fetch(url) - .then(response => response.json()) - .then(data => setCharacterGroups(data)) - .catch(err => console.error(err)); - }, []) + audioQueue.current = []; + setIsPlaying(false); + } // Helper functions const handleSocketOnOpen = async (event) => { @@ -115,7 +93,6 @@ const App = () => { setTextAreaValue(prevState => prevState + "\n\n"); } else if (message.startsWith('Select')) { - // setCharacterGroups(createCharacterGroups(message)); } else { setTextAreaValue(prevState => prevState + `${event.data}`); @@ -134,15 +111,6 @@ const App = () => { } } - const stopAudioPlayback = () => { - if (audioPlayer.current) { - audioPlayer.current.pause(); - shouldPlayAudio.current = false; - } - audioQueue.current = []; - setIsPlaying(false); - } - // Use custom hooks const { socketRef, send, connectSocket, closeSocket } = useWebsocket(token, handleSocketOnOpen,handleSocketOnMessage, selectedModel, preferredLanguage, selectedCharacter); const { isRecording, connectMicrophone, startRecording, stopRecording, closeMediaRecorder } = useMediaRecorder(isConnected, audioSent, callActive, send, closeSocket); @@ -170,50 +138,6 @@ const App = () => { } } - const handleTalkClick = () => { - connect(); - - // Show loading animation - - const interval = setInterval(() => { - if (isConnected.current && selectedCharacter) { - setCharacterConfirmed(true); - - // display callview - setIsCallView(true); - const greeting = { - "English": "Hi, my friend, what brings you here today?", - "Spanish": "Hola, mi amigo, ¿qué te trae por aquí hoy?" - } - setHeaderText(greeting[preferredLanguage]); - - clearInterval(interval); // Stop checking - // Hide loading animation - } - }, 500); // Check every 0.5 second - } - - const handleTextClick = () => { - connect(); - - // Show loading animation - - const interval = setInterval(() => { - if (isConnected.current && selectedCharacter) { - setCharacterConfirmed(true); - - // display textview - setIsCallView(false); - setHeaderText(""); - - shouldPlayAudio.current = true; - - // Hide loading animation - clearInterval(interval); // Stop checking - } - }, 500); // Check every 0.5 second - } - const handleStopCall = () => { stopRecording(); stopListening(); @@ -240,9 +164,8 @@ const App = () => { // reset everything to initial states setSelectedCharacter(null); - setCharacterConfirmed(false); + // setCharacterConfirmed(false); setIsCallView(false); - setHeaderText("Choose your partner"); setTextAreaValue(""); setSelectedModel("gpt-3.5-turbo-16k"); setPreferredLanguage("English"); @@ -254,90 +177,67 @@ const App = () => { } return ( -
- Please wear headphone 🎧 - { isConnected.current && characterConfirmed && isRecording ? - (Recording) : null - } -
- -{headerText}
- -