on adbcda8 help sign in
bridge/src/views/Login.js
106 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, setUrbitWallet } = useWallet();
45 const { pointCursor, setPointCursor } = usePointCursor();
47 // inputs
48 const [currentTab, setCurrentTab] = useState(TABS.TICKET);
50 // on-mount
51 useLifecycle(() => {
52 setUrbitWallet(Nothing());
53 setPointCursor(Nothing());
54 });
56 const doContinue = async () => {
57 const _wallet = need.wallet(wallet);
58 const _contracts = need.contracts(contracts);
60 // if no point cursor set by login logic, try to deduce it
61 let deduced = Nothing();
62 if (Nothing.hasInstance(pointCursor)) {
63 const owned = await azimuth.azimuth.getOwnedPoints(
64 _contracts,
65 _wallet.address
66 );
67 if (owned.length === 1) {
68 deduced = Just(owned[0]);
69 } else if (owned.length === 0) {
70 const canOwn = await azimuth.azimuth.getTransferringFor(
71 _contracts,
72 _wallet.address
73 );
74 if (canOwn.length === 1) {
75 deduced = Just(canOwn[0]);
76 }
77 }
78 }
80 // if we have a deduced point or one in the global context,
81 // navigate to that specific point, otherwise navigate to list of points
82 if (Just.hasInstance(deduced)) {
83 setPointCursor(deduced);
84 history.popAndPush(ROUTE_NAMES.POINT);
85 } else if (Just.hasInstance(pointCursor)) {
86 history.popAndPush(ROUTE_NAMES.POINT);
87 } else {
88 history.popAndPush(ROUTE_NAMES.POINTS);
89 }
90 };
92 return (
93 <View>
94 <H1>Login</H1>
96 <Tabs
97 tabViews={tabViews}
98 tabOptions={tabOptions}
99 currentTab={currentTab}
100 onTabChange={setCurrentTab}
101 //
102 loginCompleted={doContinue}
103 />
104 </View>
105 );