From 744e744084a2ea1674eec528adcadf98d0a1a736 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Oct 2021 10:43:10 -0500 Subject: don't try to connect if session is bad, fix spacing --- src/web/src/components/App.js | 427 +++++++++++++++++++++--------------------- 1 file changed, 218 insertions(+), 209 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index f8813820..e717b8c0 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -16,225 +16,234 @@ import System from './System/System'; import LoginForm from './LoginForm'; import { - Sidebar, - Segment, - Menu, - Icon, - Modal, - Header, - Button + Sidebar, + Segment, + Menu, + Icon, + Modal, + Header, + Button, + Loader } from 'semantic-ui-react'; import Rooms from './Rooms/Rooms'; import ErrorSegment from './Shared/ErrorSegment'; const initialState = { - login: { - initialized: false, - pending: false, - error: undefined - }, - applicationState: {}, - error: false, + login: { + pending: false, + error: undefined + }, + applicationState: {}, + initialized: false, + error: false, }; class App extends Component { - state = initialState; - - componentDidMount = () => { - this.init(); - }; - - init = async () => { - const { login } = this.state; - - try { - const securityEnabled = await session.getSecurityEnabled(); - - if (!securityEnabled) { - console.debug('application security is not enabled, per api call') - session.enablePassthrough() - } - - await session.check(); - - const appHub = createApplicationHubConnection(); - - appHub.on('state', (state) => { - console.debug(state) - this.setState({ applicationState: state }); - }); - - appHub.onreconnecting(() => this.setState({ error: true })); - appHub.onclose(() => this.setState({ error: true })); - appHub.onreconnected(() => this.setState({ error: false })); - - appHub.start(); - - this.setState({ - login: { - ...login, - initialized: true - }, - error: false, - }); - } catch (err) { - console.error(err) - this.setState({ error: true }, () => setTimeout(() => this.init(), 1000)) - } + state = initialState; + + componentDidMount = () => { + this.init(); + }; + + init = async () => { + try { + const securityEnabled = await session.getSecurityEnabled(); + + if (!securityEnabled) { + console.debug('application security is not enabled, per api call') + session.enablePassthrough() + } + + const sessionOk = await session.check(); + + if (!sessionOk) { + return; + } + + const appHub = createApplicationHubConnection(); + + appHub.on('state', (state) => { + this.setState({ applicationState: state }); + }); + + appHub.onreconnecting(() => this.setState({ error: true })); + appHub.onclose(() => this.setState({ error: true })); + appHub.onreconnected(() => this.setState({ error: false })); + + await appHub.start(); + + this.setState({ + error: false, + }); + } catch (err) { + console.error(err) + this.setState({ error: true }, () => setTimeout(() => this.init(), 1000)) + } finally { + this.setState({ initialized: true }); + } + } + + login = (username, password, rememberMe) => { + this.setState({ login: { ...this.state.login, pending: true, error: undefined }}, async () => { + try { + await session.login({ username, password, rememberMe }); + window.location.reload(); + } catch (error) { + this.setState({ login: { ...this.state.login, pending: false, error }}); + } + }); + }; + + logout = () => { + session.logout(); + this.setState({ ...initialState, login: { ...initialState.login, initialized: true }}); + }; + + withTokenCheck = (component) => { + session.check(); // async, runs in the background + return { ...component }; + }; + + render = () => { + const { login, applicationState = {}, error, initialized } = this.state; + const { version = {}, server } = applicationState; + const { isUpdateAvailable, current, latest } = version; + + if (!initialized) { + return ; + } + + if (error) { + return ; + } + + if (!session.isLoggedIn() && !session.isPassthroughEnabled()) { + return ( + + ); } - login = (username, password, rememberMe) => { - this.setState({ login: { ...this.state.login, pending: true, error: undefined }}, async () => { - try { - await session.login({ username, password, rememberMe }); - window.location.reload(); - } catch (error) { - this.setState({ login: { ...this.state.login, pending: false, error }}); - } - }); - }; - - logout = () => { - session.logout(); - this.setState({ ...initialState, login: { ...initialState.login, initialized: true }}); - }; - - withTokenCheck = (component) => { - session.check(); // async, runs in the background - return { ...component }; - }; - - render = () => { - const { login, applicationState = {}, error } = this.state; - const { version = {}, server } = applicationState; - const { isUpdateAvailable, current, latest } = version; - - return ( - <> - {error ? : !session.isLoggedIn() && !session.isPassthroughEnabled() ? - : - login.initialized && - - - - Search - - - - - Downloads - - - - - Uploads - - - - - Rooms - - - - - Chat - - - - - Users - - - - - Browse - - - - {server?.isConnected && disconnect()} - > - Connected - } - {(!server?.isConnected) && connect()} - > - - - - Disconnected - } - {isUpdateAvailable && - - - New Version! - } - centered - closeIcon - size='mini' - > - New Version! - -

You are currently running version {current} while version {latest} is available.

-
- - - -
} - - - System - - - {session.isLoggedIn() && - Log Out - - } - centered - size='mini' - header={
} - content='Are you sure you want to log out?' - actions={['Cancel', { key: 'done', content: 'Log Out', negative: true, onClick: session.logout }]} - />} -
-
- - - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> - - -
- } - - ) - }; + return ( + + + + + Search + + + + + Downloads + + + + + Uploads + + + + + Rooms + + + + + Chat + + + + + Users + + + + + Browse + + + + {server?.isConnected && disconnect()} + > + Connected + } + {(!server?.isConnected) && connect()} + > + + + + Disconnected + } + {isUpdateAvailable && + + + New Version! + } + centered + closeIcon + size='mini' + > + New Version! + +

You are currently running version {current} while version {latest} is available.

+
+ + + +
} + + + System + + + {session.isLoggedIn() && + Log Out + + } + centered + size='mini' + header={
} + content='Are you sure you want to log out?' + actions={['Cancel', { key: 'done', content: 'Log Out', negative: true, onClick: session.logout }]} + />} +
+
+ + + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + this.withTokenCheck()}/> + + +
+ ) + }; }; export default App; -- cgit v1.2.3