Save chat history locally (#267)

This commit is contained in:
ravikiranp123 2023-06-06 21:41:36 +05:30 committed by GitHub
parent 98c409176d
commit 6ca4d95de4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import { useState } from "react"; import { useEffect, useState } from "react";
import { useSupabase } from "@/app/supabase-provider"; import { useSupabase } from "@/app/supabase-provider";
import { useBrainConfig } from "@/lib/context/BrainConfigProvider/hooks/useBrainConfig"; import { useBrainConfig } from "@/lib/context/BrainConfigProvider/hooks/useBrainConfig";
@ -17,6 +17,16 @@ export const useQuestion = () => {
redirect("/login"); redirect("/login");
} }
useEffect(()=>{
// Check if history exists in local storage. If it does, fetch it and set it as history
(async ()=>{
const localHistory = await localStorage.getItem('history')
if(localHistory){
setHistory(JSON.parse(localHistory))
}
})()
}, [])
const askQuestion = async () => { const askQuestion = async () => {
setHistory((hist) => [...hist, ["user", question]]); setHistory((hist) => [...hist, ["user", question]]);
setIsPending(true); setIsPending(true);
@ -29,15 +39,22 @@ export const useQuestion = () => {
max_tokens: maxTokens, max_tokens: maxTokens,
}); });
setHistory(response.data.history); setHistory(response.data.history);
localStorage.setItem('history', JSON.stringify(response.data.history))
setQuestion(""); setQuestion("");
setIsPending(false); setIsPending(false);
}; };
const resetHistory = () => {
localStorage.setItem('history', JSON.stringify([]))
setHistory([])
}
return { return {
isPending, isPending,
history, history,
question, question,
setQuestion, setQuestion,
resetHistory,
askQuestion, askQuestion,
}; };
}; };

View File

@ -1,6 +1,6 @@
"use client"; "use client";
import Link from "next/link"; import Link from "next/link";
import { MdMic, MdMicOff, MdSettings } from "react-icons/md"; import { MdAutorenew, MdMic, MdMicOff, MdSettings } from "react-icons/md";
import Button from "../components/ui/Button"; import Button from "../components/ui/Button";
import Card from "../components/ui/Card"; import Card from "../components/ui/Card";
import PageHeading from "../components/ui/PageHeading"; import PageHeading from "../components/ui/PageHeading";
@ -9,7 +9,7 @@ import { useQuestion } from "./hooks/useQuestion";
import { useSpeech } from "./hooks/useSpeech"; import { useSpeech } from "./hooks/useSpeech";
export default function ChatPage() { export default function ChatPage() {
const { history, isPending, question, askQuestion, setQuestion } = const { history, isPending, question, askQuestion, setQuestion, resetHistory } =
useQuestion(); useQuestion();
const { isListening, speechSupported, startListening } = useSpeech(); const { isListening, speechSupported, startListening } = useSpeech();
@ -48,6 +48,16 @@ export default function ChatPage() {
<Button type="submit" isLoading={isPending}> <Button type="submit" isLoading={isPending}>
{isPending ? "Thinking..." : "Chat"} {isPending ? "Thinking..." : "Chat"}
</Button> </Button>
{/* Reset Button */}
<Button
className="px-3"
variant={"tertiary"}
type="button"
onClick={resetHistory}
disabled={isPending}
>
<MdAutorenew className="text-2xl" />
</Button>
{/* Mic Button */} {/* Mic Button */}
<Button <Button
className="px-3" className="px-3"