import type { NextPage } from "next";
import { Layout } from "@/components/layout";
import Image from "next/image";
import { useState, useEffect, useLayoutEffect, useRef } from "react";
import Header, { notify, ToastType } from "@/components/header";
import { Footer } from "@/components/footer";
import React from "react";
import { handleInputChange } from "@/lib/handleInputChange";
import { InputField } from "@/components/InputField";
import OutlineButton from "@/components/OutlineButton";
import { useToasts } from "@/components/ToastProvider";
import { useSession } from 'next-auth/react'
import Background from '@/components/Background';
const Session = ({ data, onClick = () => { }, ...props }) => {
return (
Caller Phone: {data.callerPhone}
Started At: {data.startedAt}
);
}
const Demo: NextPage = ({ officers }) => {
const updateDelay = 2000
const { toastDispatch } = useToasts();
const [input, setInput] = useState({
phone: '',
})
const [sessions, setSessions] = useState([])
const [loadingSummary, setLoadingSummary] = useState(false)
const [focusSession, setFocusSession] = useState(null)
const { data: session } = useSession()
const submit = async () => {
const operator = await fetch(process.env.NEXT_PUBLIC_API_URL + '/operator/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${session['token'].sub}`
},
body: JSON.stringify({
phone: input.phone
})
})
notify(toastDispatch, "", "Updated Phone: " + input.phone, ToastType.SUCCESS)
}
const getSummary = async (sesh) => {
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 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',
headers: {
'Content-Type': 'application/json'
},
})).json()
return messages.messages
}
const popOut = async (sesh) => {
setFocusSession(sesh)
}
const transfer = async () => {
const res = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/transfer/?sessionId=' + focusSession.id, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${session['token'].sub}`
},
})).json()
await closePop()
}
const closePop = async () => {
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(() => {
const interval = setInterval(async () => {
const fetcher = async () => {
const s = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session?open=true', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})).json()
setSessions(s.sessions)
}
await fetcher()
}, updateDelay)
return () => clearInterval(interval)
}, [])
return (
{
session ?
<>
{focusSession ?
<>
Caller: {focusSession.callerPhone} | {focusSession.startedAt}
Summary: {focusSession.summary}
{(focusSession.messages ? focusSession.messages : []).map(msg => {
return (
{msg.role}: {msg.content}
);
})}
>
: null
}
Hey {session.user.name.split(' ')[0]}!
Can you give us your number real quick?
handleInputChange(e, input, setInput)} className="w-full" />
Active Calls
{sessions.map(session =>
popOut(session)} />
)}
>
: null
}
);
};
export default Demo;