2020-04-17 19:41:47 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2019-06-29 23:57:11 +00:00
|
|
|
import { Websocket } from '@/plugins/Websocket';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2019-09-25 03:20:29 +00:00
|
|
|
import getWebsocketToken from '@/api/server/getWebsocketToken';
|
2020-04-17 19:41:47 +00:00
|
|
|
import ContentContainer from '@/components/elements/ContentContainer';
|
|
|
|
import { CSSTransition } from 'react-transition-group';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2020-07-04 22:58:14 +00:00
|
|
|
import tw from 'twin.macro';
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
export default () => {
|
2019-07-10 04:25:57 +00:00
|
|
|
const server = ServerContext.useStoreState(state => state.server.data);
|
2020-04-17 19:41:47 +00:00
|
|
|
const [ error, setError ] = useState(false);
|
|
|
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
2019-07-10 04:25:57 +00:00
|
|
|
const setServerStatus = ServerContext.useStoreActions(actions => actions.status.setServerStatus);
|
|
|
|
const { setInstance, setConnectionState } = ServerContext.useStoreActions(actions => actions.socket);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-09-28 20:09:47 +00:00
|
|
|
const updateToken = (uuid: string, socket: Websocket) => {
|
|
|
|
getWebsocketToken(uuid)
|
2019-12-22 01:31:04 +00:00
|
|
|
.then(data => socket.setToken(data.token, true))
|
2019-09-28 20:09:47 +00:00
|
|
|
.catch(error => console.error(error));
|
|
|
|
};
|
|
|
|
|
2020-04-17 19:41:47 +00:00
|
|
|
useEffect(() => {
|
|
|
|
connected && setError(false);
|
|
|
|
}, [ connected ]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
instance && instance.close();
|
|
|
|
};
|
|
|
|
}, [ instance ]);
|
|
|
|
|
2019-06-29 23:14:32 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// If there is already an instance or there is no server, just exit out of this process
|
|
|
|
// since we don't need to make a new connection.
|
|
|
|
if (instance || !server) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-25 03:20:29 +00:00
|
|
|
const socket = new Websocket();
|
2019-06-29 23:57:11 +00:00
|
|
|
|
2019-12-22 01:31:04 +00:00
|
|
|
socket.on('auth success', () => setConnectionState(true));
|
2019-06-29 23:57:11 +00:00
|
|
|
socket.on('SOCKET_CLOSE', () => setConnectionState(false));
|
2020-04-17 19:41:47 +00:00
|
|
|
socket.on('SOCKET_ERROR', () => {
|
|
|
|
setError(true);
|
|
|
|
setConnectionState(false);
|
|
|
|
});
|
2019-06-29 23:57:11 +00:00
|
|
|
socket.on('status', (status) => setServerStatus(status));
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-09-28 20:09:47 +00:00
|
|
|
socket.on('daemon error', message => {
|
|
|
|
console.warn('Got error message from daemon socket:', message);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('token expiring', () => updateToken(server.uuid, socket));
|
|
|
|
socket.on('token expired', () => updateToken(server.uuid, socket));
|
|
|
|
|
2019-09-25 03:20:29 +00:00
|
|
|
getWebsocketToken(server.uuid)
|
|
|
|
.then(data => {
|
2019-12-22 01:31:04 +00:00
|
|
|
// Connect and then set the authentication token.
|
2019-09-25 03:20:29 +00:00
|
|
|
socket.setToken(data.token).connect(data.socket);
|
2019-12-22 01:31:04 +00:00
|
|
|
|
|
|
|
// Once that is done, set the instance.
|
2019-09-25 03:20:29 +00:00
|
|
|
setInstance(socket);
|
|
|
|
})
|
2019-09-09 00:48:37 +00:00
|
|
|
.catch(error => console.error(error));
|
2019-06-29 23:57:11 +00:00
|
|
|
}, [ server ]);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2020-04-17 19:41:47 +00:00
|
|
|
return (
|
|
|
|
error ?
|
|
|
|
<CSSTransition timeout={250} in={true} appear={true} classNames={'fade'}>
|
2020-07-04 22:58:14 +00:00
|
|
|
<div css={tw`bg-red-500 py-2`}>
|
|
|
|
<ContentContainer css={tw`flex items-center justify-center`}>
|
2020-07-04 19:39:55 +00:00
|
|
|
<Spinner size={'small'}/>
|
2020-07-04 22:58:14 +00:00
|
|
|
<p css={tw`ml-2 text-sm text-red-100`}>
|
2020-07-04 22:40:41 +00:00
|
|
|
We're having some trouble connecting to your server, please wait...
|
2020-04-17 19:41:47 +00:00
|
|
|
</p>
|
|
|
|
</ContentContainer>
|
|
|
|
</div>
|
|
|
|
</CSSTransition>
|
|
|
|
:
|
|
|
|
null
|
|
|
|
);
|
2019-06-29 23:14:32 +00:00
|
|
|
};
|