humus-old/client/ui/connection-status.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-04-14 16:14:03 -05:00
// 3rd party.
import classnames from 'classnames';
import React, {useEffect, useState} from 'react';
// 2nd party.
import {compose} from '@avocado/core';
import contempo from 'contempo';
const decorate = compose(
2019-09-22 21:25:43 -05:00
contempo(require('./connection-status.raw.scss').default),
2019-04-14 16:14:03 -05:00
);
const ConnectionStatusComponent = ({socket}) => {
// Start true even if not really connected to prevent initial "interrupted"
// state.
const [isConnected, setIsConnected] = useState(true);
const [dots, setDots] = useState(0);
// Juggle connected state.
useEffect(() => {
const onConnect = () => {
setIsConnected(true);
};
socket.on('connect', onConnect);
const onDisconnect = () => {
setIsConnected(false);
};
socket.on('disconnect', onDisconnect);
return () => {
socket.off('connect', onConnect);
socket.off('disconnect', onDisconnect);
};
}, []);
// Make the dots dance.
useEffect(() => {
if (isConnected) {
return;
}
2019-04-20 21:36:05 -05:00
const handle = setTimeout(() => {
2019-04-14 16:14:03 -05:00
setDots((dots + 1) % 39);
}, 50);
2019-04-20 21:36:05 -05:00
return () => {
clearTimeout(handle);
};
2019-04-14 16:14:03 -05:00
}, [dots, isConnected]);
const renderedDots = Array(dots + 1).fill('.').join('');
return <div className={classnames(
'connection-status',
!isConnected ? 'interrupted' : '',
'unselectable',
)}>
<div className="message">
<p>:[ Connection interrupted ]:</p>
<p className="dots">{renderedDots}</p>
</div>
</div>;
}
export default decorate(ConnectionStatusComponent);