-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
71 lines (62 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import io from 'Socket.IO-client';
import { useAppContext } from '../shared/context';
import logger from '../shared/logger';
import { initalizeSession } from '../shared/oauth';
import { getCustomUISDK } from '../shared/custom_ui_sdk';
import { handleSocketCommunication } from '../shared/socket';
import ContactList from '../components/ContactList';
import Dialer from '../components/Dialer';
import FollowUp from '../components/FollowUp';
import Login from '../components/Login';
const log = logger('Core ✨');
export const getServerSideProps = async ({ req, res, query }) => {
log.info('Checking session details based on query parameters');
const session = await initalizeSession(req, res, query.userId);
return session.auth
? { props: { auth: true, session } }
: { props: { auth: false } };
};
const Home = ({ auth, session }) => {
const router = useRouter();
const context = useAppContext();
const socket = io();
useEffect(() => {
if (auth) {
// Update the context variables once the session is initialized
log.info('Setting user ID to ', router.query.userId);
context.setUser(session);
// Initialize Custom UI SDK and Socket communications
(async () => {
const sdk = await getCustomUISDK();
await fetch('/api/socket', {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
handleSocketCommunication(socket, context, sdk);
})();
}
}, [router]);
// Not logged-in? Login again
if (auth === false) {
return <Login />;
}
switch (context.callerState) {
case 'listening': {
return <ContactList {...context} />;
}
case 'ringing':
case 'connected': {
return <Dialer {...context} />;
}
case 'disconnected': {
return <FollowUp {...context} />;
}
default:
return <ContactList {...context} />;
}
};
export default Home;