blob: 983a33d3761678f8cd7e188e0ec323b926bafc50 (
plain)
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
|
import { useEffect, useState } from 'react';
import { hasExpired } from '../../shared/account-expiry';
import { AuthFailedError, ErrorStateCause } from '../../shared/daemon-rpc-types';
import Connect from '../components/Connect';
import { useHistory } from '../lib/history';
import { RoutePath } from '../lib/routes';
import { useSelector } from '../redux/store';
import ExpiredAccountErrorView from './ExpiredAccountErrorView';
type ExpiryData = { show: false } | { show: true; expiry: string | undefined };
export default function MainView() {
const history = useHistory();
const accountExpiry = useSelector((state) => state.account.expiry);
const accountHasExpired = accountExpiry !== undefined && hasExpired(accountExpiry);
const isNewAccount = useSelector(
(state) => state.account.status.type === 'ok' && state.account.status.method === 'new_account',
);
const tunnelState = useSelector((state) => state.connection.status);
const [showAccountExpired, setShowAccountExpired] = useState<ExpiryData>(() =>
isNewAccount || accountHasExpired ? { show: true, expiry: accountExpiry } : { show: false },
);
useEffect(() => {
if (
(!showAccountExpired.show || showAccountExpired.expiry !== accountExpiry) &&
(accountHasExpired ||
(tunnelState.state === 'error' &&
tunnelState.details.cause === ErrorStateCause.authFailed &&
tunnelState.details.authFailedError === AuthFailedError.expiredAccount))
) {
setShowAccountExpired({ show: true, expiry: accountExpiry });
} else if (
showAccountExpired.show &&
accountExpiry !== showAccountExpired.expiry &&
!accountHasExpired
) {
history.push(RoutePath.timeAdded);
}
}, [showAccountExpired, accountHasExpired, tunnelState.state]);
if (showAccountExpired.show) {
return <ExpiredAccountErrorView />;
} else {
return <Connect />;
}
}
|