From 907c4f8c4bda4d9856b1a79a370eda801203974f Mon Sep 17 00:00:00 2001 From: TaylorS15 Date: Wed, 22 Feb 2023 09:12:55 -0500 Subject: [PATCH] storing api key and recent source docs locally --- frontend/src/Navigation.tsx | 2 +- frontend/src/preferences/APIKeyModal.tsx | 27 +++++++- frontend/src/preferences/SelectDocsModal.tsx | 28 +++++++- frontend/src/preferences/preferenceApi.ts | 73 ++++++++++++++++++++ frontend/src/preferences/preferenceSlice.ts | 2 +- frontend/src/preferences/selectDocsApi.ts | 33 --------- 6 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 frontend/src/preferences/preferenceApi.ts delete mode 100644 frontend/src/preferences/selectDocsApi.ts diff --git a/frontend/src/Navigation.tsx b/frontend/src/Navigation.tsx index cfc7a82..4199acf 100644 --- a/frontend/src/Navigation.tsx +++ b/frontend/src/Navigation.tsx @@ -1,3 +1,4 @@ +import { useState } from 'react'; import { NavLink } from 'react-router-dom'; import Arrow1 from './assets/arrow.svg'; import Hamburger from './assets/hamburger.svg'; @@ -12,7 +13,6 @@ import { selectApiKeyStatus, selectSelectedDocsStatus, } from './preferences/preferenceSlice'; -import { useState } from 'react'; //TODO - Need to replace Chat button to open secondary nav with scrollable past chats option and new chat at top //TODO - Need to add Discord and Github links diff --git a/frontend/src/preferences/APIKeyModal.tsx b/frontend/src/preferences/APIKeyModal.tsx index b440500..5cad231 100644 --- a/frontend/src/preferences/APIKeyModal.tsx +++ b/frontend/src/preferences/APIKeyModal.tsx @@ -1,7 +1,8 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { useDispatch } from 'react-redux'; import { ActiveState } from '../models/misc'; import { setApiKey } from './preferenceSlice'; +import { getLocalApiKey, setLocalApiKey } from './preferenceApi'; export default function APIKeyModal({ modalState, @@ -20,19 +21,39 @@ export default function APIKeyModal({ if (key.length <= 1) { setIsError(true); } else { + setLocalApiKey(key); dispatch(setApiKey(key)); setModalState('INACTIVE'); - setKey(''); setIsError(false); } } function handleCancel() { - setKey(''); + async function getApiKey() { + const localKey = await getLocalApiKey(); + if (localKey) { + setKey(localKey); + } + } + + getApiKey(); setIsError(false); setModalState('INACTIVE'); } + useEffect(() => { + async function getApiKey() { + const localKey = await getLocalApiKey(); + if (localKey) { + dispatch(setApiKey(localKey)); + setKey(localKey); + setModalState('INACTIVE'); + } + } + + getApiKey(); + }, []); + return (
{ + try { + const response = await fetch( + 'https://d3dg1063dc54p9.cloudfront.net/combined.json', + ); + const data = await response.json(); + + const docs: Doc[] = []; + + data.forEach((doc: object) => { + docs.push(doc as Doc); + }); + + return docs; + } catch (error) { + console.log(error); + return null; + } +} + +export async function getLocalApiKey(): Promise { + try { + const key = localStorage.getItem('DocsGPTApiKey'); + if (key) { + return key; + } + return null; + } catch (error) { + console.log(error); + return null; + } +} + +export async function getLocalRecentDocs(): Promise { + try { + const doc = localStorage.getItem('DocsGPTRecentDocs'); + if (doc) { + return JSON.parse(doc); + } + return null; + } catch (error) { + console.log(error); + return null; + } +} + +export async function setLocalApiKey(key: string): Promise { + try { + localStorage.setItem('DocsGPTApiKey', key); + } catch (error) { + console.log(error); + } +} + +export async function setLocalRecentDocs(doc: Doc): Promise { + try { + localStorage.setItem('DocsGPTRecentDocs', JSON.stringify(doc)); + } catch (error) { + console.log(error); + } +} diff --git a/frontend/src/preferences/preferenceSlice.ts b/frontend/src/preferences/preferenceSlice.ts index 4c8a95b..f4c9808 100644 --- a/frontend/src/preferences/preferenceSlice.ts +++ b/frontend/src/preferences/preferenceSlice.ts @@ -1,5 +1,5 @@ import { createSlice } from '@reduxjs/toolkit'; -import { Doc } from './selectDocsApi'; +import { Doc } from './preferenceApi'; import store from '../store'; interface Preference { diff --git a/frontend/src/preferences/selectDocsApi.ts b/frontend/src/preferences/selectDocsApi.ts deleted file mode 100644 index fe781e1..0000000 --- a/frontend/src/preferences/selectDocsApi.ts +++ /dev/null @@ -1,33 +0,0 @@ -//Exporting Doc type from here since its the first place its used and seems needless to make an entire file for it. -export type Doc = { - name: string; - language: string; - version: string; - description: string; - fullName: string; - dat: string; - docLink: string; - model: string; -}; - -//Fetches all JSON objects from the source. We only use the objects with the "model" property in SelectDocsModal.tsx. Hopefully can clean up the source file later. -export async function getDocs(): Promise { - try { - //Fetch default source docs - const response = await fetch( - 'https://d3dg1063dc54p9.cloudfront.net/combined.json', - ); - const data = await response.json(); - - //Create array of Doc objects - const docs: Doc[] = []; - - data.forEach((doc: object) => { - docs.push(doc as Doc); - }); - - return docs; - } catch (error) { - return null; - } -}