on faa39fb help sign in
bridge/src/views/Login.js
108 lines 2.9 kB history raw
1import { Nothing, Just } from 'folktale/maybe';
2import React, { useState, useEffect } 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 * as need from '../lib/need';
14import { ROUTE_NAMES } from '../lib/routeNames';
16import { useHistory } from '../store/history';
17import { useNetwork } from '../store/network';
18import { useWallet } from '../store/wallet';
19import { usePointCursor } from '../store/pointCursor';
21const TABS = {
22 TICKET: Symbol('TICKET'),
23 MNEMONIC: Symbol('MNEMONIC'),
24 ADVANCED: Symbol('ADVANCED'),
25};
27export default function Login() {
28 // globals
29 const history = useHistory();
30 const { contracts } = useNetwork();
31 const { wallet, setUrbitWallet } = useWallet();
32 const { pointCursor, setPointCursor } = usePointCursor();
34 // inputs
35 const [currentTab, setCurrentTab] = useState(TABS.TICKET);
37 useEffect(() => {
38 // on-mount
39 setUrbitWallet(Nothing());
40 setPointCursor(Nothing());
42 // on-unmount
43 return () => {};
44 }, []);
46 const doContinue = async () => {
47 const realWallet = need.wallet(wallet);
48 const realContracts = need.contracts(contracts);
50 // if no point cursor set by login logic, try to deduce it
51 let deduced = Nothing();
52 if (Nothing.hasInstance(pointCursor)) {
53 const owned = await azimuth.azimuth.getOwnedPoints(
54 realContracts,
55 realWallet.address
56 );
57 if (owned.length === 1) {
58 deduced = Just(owned[0]);
59 } else if (owned.length === 0) {
60 const canOwn = await azimuth.azimuth.getTransferringFor(
61 realContracts,
62 realWallet.address
63 );
64 if (canOwn.length === 1) {
65 deduced = Just(canOwn[0]);
66 }
67 }
68 }
70 // if we have a deduced point or one in the global context,
71 // navigate to that specific point, otherwise navigate to list of points
72 if (Just.hasInstance(deduced)) {
73 setPointCursor(deduced);
74 history.popAndPush(ROUTE_NAMES.POINT);
75 } else if (Just.hasInstance(pointCursor)) {
76 history.popAndPush(ROUTE_NAMES.POINT);
77 } else {
78 history.popAndPush(ROUTE_NAMES.POINTS);
79 }
80 };
82 const tabViews = {
83 [TABS.TICKET]: Ticket,
84 [TABS.MNEMONIC]: Mnemonic,
85 [TABS.ADVANCED]: Advanced,
86 };
88 const tabOptions = [
89 { title: 'Ticket', value: TABS.TICKET },
90 { title: 'Mnemonic', value: TABS.MNEMONIC },
91 { title: 'Advanced', value: TABS.ADVANCED },
92 ];
94 return (
95 <View>
96 <H1>Login</H1>
98 <Tabs
99 tabViews={tabViews}
100 tabOptions={tabOptions}
101 currentTab={currentTab}
102 onTabChange={setCurrentTab}
103 //
104 loginCompleted={doContinue}
105 />
106 </View>
107 );