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 { toastDispatch } = useToasts();
const [input, setInput] = useState({
phone: '',
})
const [sessions, setSessions] = useState([])
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 popOut = async (session) => {
notify(toastDispatch, "", "Loading call log...", ToastType.DEFAULT)
const messages = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/message/?sessionId=' + session.id, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
})).json()
const summary = await (await fetch(process.env.NEXT_PUBLIC_API_URL + '/session/summary/?sessionId=' + session.id, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
})).json()
session.messages = messages.messages;
session.summary = summary.summary;
console.log(session)
setFocusSession(session)
notify(toastDispatch, "", "Loaded call log!", ToastType.SUCCESS)
}
const transfer = async () => {
console.log(session['token'].sub)
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 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()
}, 2000)
return () => clearInterval(interval)
}, [])
return (
{
session ?
<>
{focusSession ?
<>
Caller: {focusSession.callerPhone} | {focusSession.startedAt}
Summary: {focusSession.summary}
{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;