From 5e5f13b6648435ef33fa4bda985fc61a14b59a7d Mon Sep 17 00:00:00 2001 From: TaylorS15 Date: Tue, 14 Feb 2023 21:43:14 -0500 Subject: [PATCH] major changes state management now handled with redux responsiveness uses custom hook (hooks.ts) --- frontend/src/App.tsx | 59 ++---------- frontend/src/components/APIKeyModal.tsx | 41 ++++----- frontend/src/components/About.tsx | 19 ++-- .../Conversation.tsx} | 17 ++-- .../{Navigation => }/Navigation.tsx | 91 +++++++------------ .../src/components/Navigation/PastChat.tsx | 1 - frontend/src/hooks.ts | 22 +++++ frontend/src/index.css | 1 + frontend/src/main.tsx | 6 +- frontend/src/store.ts | 48 ++++++++++ 10 files changed, 156 insertions(+), 149 deletions(-) rename frontend/src/components/{DocsGPT/DocsGPT.tsx => Conversation/Conversation.tsx} (51%) rename frontend/src/components/{Navigation => }/Navigation.tsx (68%) delete mode 100644 frontend/src/components/Navigation/PastChat.tsx create mode 100644 frontend/src/hooks.ts create mode 100644 frontend/src/store.ts diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index c0a3a9c0..d6467e2f 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,62 +1,17 @@ -import { useEffect, useState } from 'react'; import { Routes, Route } from 'react-router-dom'; -import Navigation from './components/Navigation/Navigation'; -import DocsGPT from './components/DocsGPT/DocsGPT'; +import Navigation from './components/Navigation'; +import Conversation from './components/Conversation/Conversation'; import APIKeyModal from './components/APIKeyModal'; import About from './components/About'; export default function App() { - //Currently using primitive state management. Will most likely be replaced with Redux. - const [isMobile, setIsMobile] = useState(true); - const [isMenuOpen, setIsMenuOpen] = useState(true); - const [isApiModalOpen, setIsApiModalOpen] = useState(false); - const [apiKey, setApiKey] = useState(''); - - const handleResize = () => { - if (window.innerWidth > 768 && isMobile) { - setIsMobile(false); - } else { - setIsMobile(true); - setIsMenuOpen(false); - } - }; - - useEffect(() => { - window.addEventListener('resize', handleResize); - handleResize(); - - return () => { - window.removeEventListener('resize', handleResize); - }; - }, []); - return ( -
- - +
+ + - } - /> - } - /> + } /> + } />
); diff --git a/frontend/src/components/APIKeyModal.tsx b/frontend/src/components/APIKeyModal.tsx index 505f2494..07a8f061 100644 --- a/frontend/src/components/APIKeyModal.tsx +++ b/frontend/src/components/APIKeyModal.tsx @@ -1,30 +1,29 @@ import { useState } from 'react'; - -export default function APIKeyModal({ - isApiModalOpen, - setIsApiModalOpen, - apiKey, +import { useDispatch, useSelector } from 'react-redux'; +import { setApiKey, -}: { - isApiModalOpen: boolean; - setIsApiModalOpen: React.Dispatch>; - apiKey: string; - setApiKey: React.Dispatch>; -}) { - //TODO - Add form validation + toggleApiKeyModal, + selectIsApiKeyModalOpen, +} from '../store'; + +export default function APIKeyModal({}) { + //TODO - Add form validation? //TODO - Connect to backend //TODO - Add link to OpenAI API Key page + const dispatch = useDispatch(); + const isApiModalOpen = useSelector(selectIsApiKeyModalOpen); + const [key, setKey] = useState(''); const [formError, setFormError] = useState(false); - const handleResetKey = () => { - if (!apiKey) { + function handleSubmit() { + if (key.length < 1) { setFormError(true); - } else { - setFormError(false); - setIsApiModalOpen(false); + return; } - }; + dispatch(setApiKey(key)); + dispatch(toggleApiKeyModal()); + } return (
setApiKey(e.target.value)} + onChange={(e) => setKey(e.target.value)} />
{formError && (

Please enter a valid API key

)} @@ -38,7 +40,7 @@ function MobileNavigation({ <> @@ -71,7 +73,7 @@ function MobileNavigation({
setIsApiModalOpen(true)} + onClick={() => dispatch(toggleApiKeyModal())} > info

Reset Key

@@ -82,15 +84,10 @@ function MobileNavigation({ ); } -function DesktopNavigation({ - isMenuOpen, - setIsMenuOpen, - setIsApiModalOpen, -}: { - isMenuOpen: boolean; - setIsMenuOpen: React.Dispatch>; - setIsApiModalOpen: React.Dispatch>; -}) { +function DesktopNavigation() { + const dispatch = useDispatch(); + const isMenuOpen = useSelector(selectIsMenuOpen); + return (