on master help sign in
Merge pull request #223 from urbit/mino-no-singlehome
debe822Fang
src/views/Login.js+3−47
@@ -1,14 +1,10 @@
1import React, { useCallback, useState } from 'react';
1import React, { useCallback, useState } from 'react';
2−import { Just, Nothing } from 'folktale/maybe';
2+import { Nothing } from 'folktale/maybe';
3−import * as azimuth from 'azimuth-js';
4import { H4, Grid } from 'indigo-react';
3import { H4, Grid } from 'indigo-react';
5
4
6import { useHistory } from 'store/history';
5import { useHistory } from 'store/history';
7−import { useNetwork } from 'store/network';
8import { useWallet } from 'store/wallet';
6import { useWallet } from 'store/wallet';
9−import { usePointCursor } from 'store/pointCursor';
10
7
11−import * as need from 'lib/need';
12import { WALLET_TYPES } from 'lib/wallet';
8import { WALLET_TYPES } from 'lib/wallet';
13
9
14import View from 'components/View';
10import View from 'components/View';
@@ -64,11 +60,7 @@ const walletTypeToViewName = walletType => {
64export default function Login() {
60export default function Login() {
65 // globals
61 // globals
66 const { pop, push, names } = useHistory();
62 const { pop, push, names } = useHistory();
67− const { contracts } = useNetwork();
68 const { wallet, walletType } = useWallet();
63 const { wallet, walletType } = useWallet();
69− const { pointCursor, setPointCursor } = usePointCursor();
70−
71− const [deducing, setDeducing] = useState(false);
72
64
73 // inputs
65 // inputs
74 const [currentTab, setCurrentTab] = useState(
66 const [currentTab, setCurrentTab] = useState(
@@ -80,45 +72,10 @@ export default function Login() {
80 names.ACTIVATE,
72 names.ACTIVATE,
81 ]);
73 ]);
82
74
83− const goToPoints = useCallback(() => {
75+ const doContinue = useCallback(() => {
84 push(names.POINTS);
76 push(names.POINTS);
85 }, [push, names]);
77 }, [push, names]);
86
78
87− const goToPoint = useCallback(() => {
88− goToPoints();
89− push(names.POINT);
90− }, [goToPoints, push, names]);
91−
92− const doContinue = useCallback(async () => {
93− const _wallet = need.wallet(wallet);
94− const _contracts = need.contracts(contracts);
95−
96− setDeducing(true);
97−
98− let deduced = pointCursor;
99− // if no point cursor set by login logic, try to deduce it
100− if (Nothing.hasInstance(deduced)) {
101− const owned = await azimuth.azimuth.getOwnedPoints(
102− _contracts,
103− _wallet.address
104− );
105− if (owned.length === 1) {
106− deduced = Just(owned[0]);
107− }
108− }
109−
110− setDeducing(false);
111−
112− // if we have a deduced point or one in the global context,
113− // navigate to that specific point, otherwise navigate to list of points
114− if (Just.hasInstance(deduced)) {
115− setPointCursor(deduced);
116− goToPoint();
117− } else {
118− goToPoints();
119− }
120− }, [contracts, pointCursor, setPointCursor, wallet, goToPoint, goToPoints]);
121−
122 return (
79 return (
123 <View pop={pop} inset>
80 <View pop={pop} inset>
124 <Grid>
81 <Grid>
@@ -142,8 +99,7 @@ export default function Login() {
142 as={ForwardButton}
99 as={ForwardButton}
143 solid
100 solid
144 className="mt2"
101 className="mt2"
145− loading={deducing}
146− disabled={Nothing.hasInstance(wallet) || deducing}
102+ disabled={Nothing.hasInstance(wallet)}
147 onClick={doContinue}>
103 onClick={doContinue}>
148 Continue
104 Continue
149 </Grid.Item>
105 </Grid.Item>
src/views/Points.js+74−8
@@ -1,4 +1,4 @@
1−import React, { useCallback } from 'react';
1+import React, { useCallback, useEffect, useMemo } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
2import { Just, Nothing } from 'folktale/maybe';
3import { Grid, H5, H1, HelpText, LinkButton, Flex } from 'indigo-react';
3import { Grid, H5, H1, HelpText, LinkButton, Flex } from 'indigo-react';
4import { get } from 'lodash';
4import { get } from 'lodash';
@@ -79,7 +79,7 @@ function ActionButtons({ actions = [] }) {
79
79
80export default function Points() {
80export default function Points() {
81 const { wallet } = useWallet();
81 const { wallet } = useWallet();
82− const { pop, push, names } = useHistory();
82+ const { pop, push, popAndPush, names } = useHistory();
83 const { setPointCursor } = usePointCursor();
83 const { setPointCursor } = usePointCursor();
84 const { controlledPoints, getDetails } = usePointCache();
84 const { controlledPoints, getDetails } = usePointCache();
85 const isEclipticOwner = useIsEclipticOwner();
85 const isEclipticOwner = useIsEclipticOwner();
@@ -88,6 +88,77 @@ export default function Points() {
88 addRejectedPoint,
88 addRejectedPoint,
89 ] = useRejectedIncomingPointTransfers();
89 ] = useRejectedIncomingPointTransfers();
90
90
91+ const maybeOutgoingPoints = useMemo(
92+ () =>
93+ controlledPoints.chain(points =>
94+ points.matchWith({
95+ Error: () => Nothing(),
96+ Ok: c => {
97+ const points = c.value.ownedPoints.map(point =>
98+ getDetails(point).chain(details =>
99+ Just({ point: point, has: hasTransferProxy(details) })
100+ )
101+ );
102+ // if we have details for every point,
103+ // return the array of pending transfers.
104+ if (points.every(p => Just.hasInstance(p))) {
105+ const outgoing = points
106+ .filter(p => p.value.has)
107+ .map(p => p.value.point);
108+ return Just(outgoing);
109+ } else {
110+ return Nothing();
111+ }
112+ },
113+ })
114+ ),
115+ [getDetails, controlledPoints]
116+ );
117+
118+ // if we can only interact with a single point, forget about the existence
119+ // of this page and jump to the point page.
120+ // if there are any pending transfers, incoming or outgoing, stay on this
121+ // page, because those can only be completed/cancelled here.
122+ useEffect(() => {
123+ if (Nothing.hasInstance(maybeOutgoingPoints)) {
124+ return;
125+ }
126+ controlledPoints.matchWith({
127+ Nothing: () => null,
128+ Just: r => {
129+ r.value.matchWith({
130+ Error: () => null,
131+ Ok: c => {
132+ let all = [
133+ ...c.value.ownedPoints,
134+ ...c.value.votingPoints,
135+ ...c.value.managingPoints,
136+ ...c.value.spawningPoints,
137+ ];
138+ const incoming = c.value.incomingPoints.filter(
139+ p => !rejectedPoints.includes(p)
140+ );
141+ if (
142+ all.length === 1 &&
143+ incoming.length === 0 &&
144+ maybeOutgoingPoints.value.length === 0
145+ ) {
146+ setPointCursor(Just(all[0]));
147+ popAndPush(names.POINT);
148+ }
149+ },
150+ });
151+ },
152+ });
153+ }, [
154+ controlledPoints,
155+ rejectedPoints,
156+ maybeOutgoingPoints,
157+ setPointCursor,
158+ popAndPush,
159+ names,
160+ ]);
161+
91 const address = need.addressFromWallet(wallet);
162 const address = need.addressFromWallet(wallet);
92
163
93 const loading = Nothing.hasInstance(controlledPoints);
164 const loading = Nothing.hasInstance(controlledPoints);
@@ -101,12 +172,7 @@ export default function Points() {
101 const managingPoints = maybeGetResult(controlledPoints, 'managingPoints', []);
172 const managingPoints = maybeGetResult(controlledPoints, 'managingPoints', []);
102 const votingPoints = maybeGetResult(controlledPoints, 'votingPoints', []);
173 const votingPoints = maybeGetResult(controlledPoints, 'votingPoints', []);
103 const spawningPoints = maybeGetResult(controlledPoints, 'spawningPoints', []);
174 const spawningPoints = maybeGetResult(controlledPoints, 'spawningPoints', []);
104− const outgoingPoints = ownedPoints.filter(point =>
175+ const outgoingPoints = maybeOutgoingPoints.getOrElse([]);
105− getDetails(point).matchWith({
106− Nothing: () => false,
107− Just: p => hasTransferProxy(p.value),
108− })
109− );
110
176
111 const allPoints = [
177 const allPoints = [
112 ...ownedPoints.filter(p => !outgoingPoints.includes(p)),
178 ...ownedPoints.filter(p => !outgoingPoints.includes(p)),