on 04765bb help sign in
bridge/src/views/Login.js
105 lines 2.8 kB history raw
1import { Nothing, Just } from 'folktale/maybe';
2import React, { useState } from 'react';
3import * as azimuth from 'azimuth-js';
5import { H1 } from 'indigo-react';
6import View from 'components/View';
7import Tabs from 'components/Tabs';
9import Ticket from './Login/Ticket';
10import Mnemonic from './Login/Mnemonic';
11import Advanced from './Login/Advanced';
13import useLifecycle from 'lib/useLifecycle';
14import * as need from 'lib/need';
15import { ROUTE_NAMES } from 'lib/routeNames';
17import { useHistory } from 'store/history';
18import { useNetwork } from 'store/network';
19import { useWallet } from 'store/wallet';
20import { usePointCursor } from 'store/pointCursor';
22const TABS = {
23 TICKET: Symbol('TICKET'),
24 MNEMONIC: Symbol('MNEMONIC'),
25 ADVANCED: Symbol('ADVANCED'),
26};
28const tabViews = {
29 [TABS.TICKET]: Ticket,
30 [TABS.MNEMONIC]: Mnemonic,
31 [TABS.ADVANCED]: Advanced,
32};
34const tabOptions = [
35 { title: 'Ticket', value: TABS.TICKET },
36 { title: 'Mnemonic', value: TABS.MNEMONIC },
37 { title: 'Advanced', value: TABS.ADVANCED },
38];
40export default function Login() {
41 // globals
42 const history = useHistory();
43 const { contracts } = useNetwork();
44 const { wallet, resetWallet } = useWallet();
45 const { pointCursor, setPointCursor } = usePointCursor();
47 // inputs
48 const [currentTab, setCurrentTab] = useState(TABS.TICKET);
50 // on-mount
51 useLifecycle(() => {
52 resetWallet();
53 });
55 const doContinue = async () => {
56 const _wallet = need.wallet(wallet);
57 const _contracts = need.contracts(contracts);
59 // if no point cursor set by login logic, try to deduce it
60 let deduced = Nothing();
61 if (Nothing.hasInstance(pointCursor)) {
62 const owned = await azimuth.azimuth.getOwnedPoints(
63 _contracts,
64 _wallet.address
65 );
66 if (owned.length === 1) {
67 deduced = Just(owned[0]);
68 } else if (owned.length === 0) {
69 const canOwn = await azimuth.azimuth.getTransferringFor(
70 _contracts,
71 _wallet.address
72 );
73 if (canOwn.length === 1) {
74 deduced = Just(canOwn[0]);
75 }
76 }
77 }
79 // if we have a deduced point or one in the global context,
80 // navigate to that specific point, otherwise navigate to list of points
81 if (Just.hasInstance(deduced)) {
82 setPointCursor(deduced);
83 history.popAndPush(ROUTE_NAMES.POINT);
84 } else if (Just.hasInstance(pointCursor)) {
85 history.popAndPush(ROUTE_NAMES.POINT);
86 } else {
87 history.popAndPush(ROUTE_NAMES.POINTS);
88 }
89 };
91 return (
92 <View>
93 <H1>Login</H1>
95 <Tabs
96 tabViews={tabViews}
97 tabOptions={tabOptions}
98 currentTab={currentTab}
99 onTabChange={setCurrentTab}
100 //
101 loginCompleted={doContinue}
102 />
103 </View>
104 );