show alert when disconnected

This commit is contained in:
Roland Osborne 2022-06-28 23:44:56 -07:00
parent fb345a2a9a
commit edf2b7b1d4
4 changed files with 37 additions and 7 deletions

View File

@ -1,6 +1,6 @@
import { Avatar, Space, Image, Modal, Form, Input, Button } from 'antd'; import { Avatar, Space, Tooltip, Image, Modal, Form, Input, Button } from 'antd';
import React, { useState } from 'react' import React, { useState } from 'react'
import { IdentityWrapper, IdentityDropdown, MenuWrapper } from './Identity.styled'; import { IdentityWrapper, IdentityDropdown, MenuWrapper, AlertIcon } from './Identity.styled';
import { ExclamationCircleOutlined, RightOutlined, EditOutlined, UserOutlined, LockOutlined } from '@ant-design/icons'; import { ExclamationCircleOutlined, RightOutlined, EditOutlined, UserOutlined, LockOutlined } from '@ant-design/icons';
import { useIdentity } from './useIdentity.hook'; import { useIdentity } from './useIdentity.hook';
import { Menu, Dropdown } from 'antd'; import { Menu, Dropdown } from 'antd';
@ -34,6 +34,17 @@ export function Identity() {
</MenuWrapper> </MenuWrapper>
); );
const Disconnected = () => {
if(state.disconnected) {
return (
<Tooltip placement="right" title="Disconnected">
<AlertIcon />
</Tooltip>
)
}
return <></>;
}
return ( return (
<IdentityWrapper> <IdentityWrapper>
<IdentityDropdown overlay={menu} overlayStyle={{ minWidth: 0 }} trigger={['click']} placement="rightTop"> <IdentityDropdown overlay={menu} overlayStyle={{ minWidth: 0 }} trigger={['click']} placement="rightTop">
@ -43,7 +54,10 @@ export function Identity() {
</div> </div>
<div class="username"> <div class="username">
<span class="name">{ state.name }</span> <span class="name">{ state.name }</span>
<span class="handle">{ state.handle }</span> <div class="handle">
<Disconnected />
<span>{ state.handle }</span>
</div>
</div> </div>
<RightOutlined /> <RightOutlined />
</div> </div>

View File

@ -1,5 +1,6 @@
import { Menu, Button, Dropdown } from 'antd'; import { Menu, Button, Dropdown } from 'antd';
import styled from 'styled-components'; import styled from 'styled-components';
import { ExclamationCircleOutlined } from '@ant-design/icons';
export const IdentityWrapper = styled.div` export const IdentityWrapper = styled.div`
border-bottom: 1px solid #8fbea7; border-bottom: 1px solid #8fbea7;
@ -35,7 +36,7 @@ export const IdentityWrapper = styled.div`
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
align-items: center; align-items: flex-end;
padding-right: 8px; padding-right: 8px;
} }
@ -61,6 +62,12 @@ export const IdentityWrapper = styled.div`
color: #444444; color: #444444;
font-weight: bold; font-weight: bold;
} }
`;
export const AlertIcon = styled(ExclamationCircleOutlined)`
color: red;
margin-right: 6px;
`; `;
export const MenuWrapper = styled(Menu)` export const MenuWrapper = styled(Menu)`

View File

@ -22,6 +22,7 @@ export function useIdentity() {
confirmStatus: null, confirmStatus: null,
busy: false, busy: false,
showLogin: false, showLogin: false,
disconnected: false,
}); });
const navigate = useNavigate(); const navigate = useNavigate();
@ -107,8 +108,11 @@ export function useIdentity() {
}; };
useEffect(() => { useEffect(() => {
if (app.state && app.state.access != 'user') { if (app.state) {
navigate('/'); if (app.state.access != 'user') {
navigate('/');
}
updateState({ disconnected: app.state.disconnected });
} }
}, [app]); }, [app]);

View File

@ -129,12 +129,14 @@ export function useAppContext() {
try { try {
let rev = JSON.parse(ev.data); let rev = JSON.parse(ev.data);
setAppRevision(rev); setAppRevision(rev);
updateState({ disconnected: false });
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
} }
} }
ws.current.onclose = (e) => { ws.current.onclose = (e) => {
updateState({ disconnected: true });
console.log(e) console.log(e)
setTimeout(() => { setTimeout(() => {
if (ws.current != null) { if (ws.current != null) {
@ -143,7 +145,9 @@ export function useAppContext() {
ws.current.onopen = () => {} ws.current.onopen = () => {}
ws.current.onerror = () => {} ws.current.onerror = () => {}
setWebsocket(token); setWebsocket(token);
delay.current += 1; if (delay.current < 60) {
delay.current += 1;
}
} }
}, delay.current * 1000) }, delay.current * 1000)
} }
@ -151,6 +155,7 @@ export function useAppContext() {
ws.current.send(JSON.stringify({ AppToken: token })) ws.current.send(JSON.stringify({ AppToken: token }))
} }
ws.current.error = (e) => { ws.current.error = (e) => {
updateState({ disconnected: true });
console.log(e) console.log(e)
} }
} }