on 513ddcb help sign in
bridge/src/views/Login.js
107 lines 2.8 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 (Just.hasInstance(deduced)) {
71 setPointCursor(deduced);
72 history.popAndPush(ROUTE_NAMES.POINT_HOME);
73 } else if (Just.hasInstance(pointCursor)) {
74 history.popAndPush(ROUTE_NAMES.POINT_HOME);
75 } else {
76 //TODO to new overview (maybe should be merged into point home?)
77 history.popAndPush(ROUTE_NAMES.POINTS);
78 }
79 };
81 const tabViews = {
82 [TABS.TICKET]: Ticket,
83 [TABS.MNEMONIC]: Mnemonic,
84 [TABS.ADVANCED]: Advanced,
85 };
87 const tabOptions = [
88 { title: 'Ticket', value: TABS.TICKET },
89 { title: 'Mnemonic', value: TABS.MNEMONIC },
90 { title: 'Advanced', value: TABS.ADVANCED },
91 ];
93 return (
94 <View>
95 <H1>Login</H1>
97 <Tabs
98 tabViews={tabViews}
99 tabOptions={tabOptions}
100 currentTab={currentTab}
101 onTabChange={setCurrentTab}
102 //
103 loginCompleted={doContinue}
104 />
105 </View>
106 );