real time chat loading

This commit is contained in:
Claeb101 2023-03-06 10:53:52 -05:00
parent 17b7b669e5
commit 8b47a13bc8
2 changed files with 58 additions and 20 deletions

View File

@ -40,6 +40,11 @@ const NavBar = () => {
const {data: session} = useSession() const {data: session} = useSession()
const router = useRouter() const router = useRouter()
const { toastDispatch } = useToasts() const { toastDispatch } = useToasts()
const [path, setPath] = useState("")
useEffect(() => {
if (typeof window !== 'undefined') setPath(window.location.href)
}, [path])
useEffect(() => { useEffect(() => {
const atTopCallback = () => { const atTopCallback = () => {
@ -67,7 +72,7 @@ const NavBar = () => {
session ? session ?
<OutlineButton className='mt-4 md:ml-4 md:mt-0' name="Log Out" onClick={signOut} /> <OutlineButton className='mt-4 md:ml-4 md:mt-0' name="Log Out" onClick={signOut} />
: :
<OutlineButton className='mt-4 md:ml-4 md:mt-0' name="Log In" onClick={() => signIn("google")}/> <OutlineButton className='mt-4 md:ml-4 md:mt-0' name="Log In" onClick={() => signIn("google", {callbackUrl: path})}/>
} }
</div> </div>
</nav> </nav>

View File

@ -23,11 +23,13 @@ const Session = ({ data, onClick = () => { }, ...props }) => {
} }
const Demo: NextPage<any> = ({ officers }) => { const Demo: NextPage<any> = ({ officers }) => {
const updateDelay = 2000
const { toastDispatch } = useToasts(); const { toastDispatch } = useToasts();
const [input, setInput] = useState({ const [input, setInput] = useState({
phone: '', phone: '',
}) })
const [sessions, setSessions] = useState([]) const [sessions, setSessions] = useState([])
const [loadingSummary, setLoadingSummary] = useState(false)
const [focusSession, setFocusSession] = useState(null) const [focusSession, setFocusSession] = useState(null)
const { data: session } = useSession() const { data: session } = useSession()
@ -46,33 +48,41 @@ const Demo: NextPage<any> = ({ officers }) => {
notify(toastDispatch, "", "Updated Phone: " + input.phone, ToastType.SUCCESS) notify(toastDispatch, "", "Updated Phone: " + input.phone, ToastType.SUCCESS)
} }
const popOut = async (session) => { const getSummary = async (sesh) => {
notify(toastDispatch, "", "Loading call log...", ToastType.DEFAULT) const summary = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/summary/?sessionId=' + sesh.id, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
})).json()
return summary.summary
}
const messages = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/message/?sessionId=' + session.id, { const summarize = async () => {
// setLoadingSummary(true)
notify(toastDispatch, "", "Loading summary...", ToastType.DEFAULT)
focusSession.summary = await getSummary(focusSession)
notify(toastDispatch, "", "Loaded summary!", ToastType.SUCCESS)
setFocusSession(focusSession)
// setLoadingSummary(false)
}
const updateMessages = async (sesh) => {
const messages = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/message/?sessionId=' + sesh.id, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
})).json() })).json()
const summary = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/summary/?sessionId=' + session.id, { return messages.messages
method: 'GET', }
headers: {
'Content-Type': 'application/json'
},
})).json()
session.messages = messages.messages; const popOut = async (sesh) => {
session.summary = summary.summary; setFocusSession(sesh)
console.log(session)
setFocusSession(session)
notify(toastDispatch, "", "Loaded call log!", ToastType.SUCCESS)
} }
const transfer = async () => { const transfer = async () => {
console.log(session['token'].sub)
const res = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/transfer/?sessionId=' + focusSession.id, { const res = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/transfer/?sessionId=' + focusSession.id, {
method: 'GET', method: 'GET',
headers: { headers: {
@ -87,6 +97,28 @@ const Demo: NextPage<any> = ({ officers }) => {
setFocusSession(null) setFocusSession(null)
} }
useEffect(() => {
const main = async () => {
const fetcher = async () => {
focusSession.messages = await updateMessages(focusSession)
setFocusSession(focusSession)
}
if (focusSession) {
if (!focusSession.messages) {
await fetcher();
const cycle = () => setTimeout(async () => {
if (focusSession) {
await fetcher()
cycle()
}
}, updateDelay)
cycle()
}
}
}
main()
}, [focusSession])
useEffect(() => { useEffect(() => {
const interval = setInterval(async () => { const interval = setInterval(async () => {
const fetcher = async () => { const fetcher = async () => {
@ -100,7 +132,7 @@ const Demo: NextPage<any> = ({ officers }) => {
setSessions(s.sessions) setSessions(s.sessions)
} }
await fetcher() await fetcher()
}, 2000) }, updateDelay)
return () => clearInterval(interval) return () => clearInterval(interval)
}, []) }, [])
@ -119,12 +151,13 @@ const Demo: NextPage<any> = ({ officers }) => {
<button className={`absolute right-2 top-1 w-fit text-blue transition-all ease-in-out font-bold`} onClick={closePop}>{"X"}</button> <button className={`absolute right-2 top-1 w-fit text-blue transition-all ease-in-out font-bold`} onClick={closePop}>{"X"}</button>
{focusSession ? {focusSession ?
<><div className="flex items-center "> <><div className="flex items-center ">
<h2 className="w-fit text-3xl font-extrabold">Caller: {focusSession.callerPhone} | {focusSession.startedAt}</h2> <h2 className="w-fit text-2xl font-extrabold">Caller: {focusSession.callerPhone} | {focusSession.startedAt}</h2>
<OutlineButton name="Summarize" onClick={summarize} className="ml-8" />
<OutlineButton name="Transfer" onClick={transfer} className="ml-8" /> <OutlineButton name="Transfer" onClick={transfer} className="ml-8" />
</div> </div>
<p className="my-4 text-white"><span className="text-blue font-bold">Summary: </span>{focusSession.summary}</p> <p className="my-4 text-white"><span className="text-blue font-bold">Summary: </span>{focusSession.summary}</p>
<div className=""> <div className="">
{focusSession.messages.map(msg => { {(focusSession.messages ? focusSession.messages : []).map(msg => {
return ( return (
<div key={msg.id}> <div key={msg.id}>
<p className="text-white"><span className={`${msg.role == "USER" ? "text-green-300" : "text-blue-300"} font-bold`}>{msg.role}: </span>{msg.content}</p> <p className="text-white"><span className={`${msg.role == "USER" ? "text-green-300" : "text-blue-300"} font-bold`}>{msg.role}: </span>{msg.content}</p>