on master help sign in
Merge pull request #223 from urbit/mino-no-singlehome

Better point "deduction" in Points view

commitdebe8223d41c842c37dd430e21f088ea0bc4bd57 authorFang <Fang-@users.noreply.github.com> dateThu 8 Aug 2019, 09:12 (7 years ago) parent864190513d83c1
2 files changed+77−55
src/views/Login.js+3−47 src/views/Points.js+74−8
src/views/Login.js+3−47
@@ -1,14 +1,10 @@
11import React, { useCallback, useState } from 'react';
2−import { Just, Nothing } from 'folktale/maybe';
3−import * as azimuth from 'azimuth-js';
2+import { Nothing } from 'folktale/maybe';
43import { H4, Grid } from 'indigo-react';
54
65import { useHistory } from 'store/history';
7−import { useNetwork } from 'store/network';
86import { useWallet } from 'store/wallet';
9−import { usePointCursor } from 'store/pointCursor';
107
11−import * as need from 'lib/need';
128import { WALLET_TYPES } from 'lib/wallet';
139
1410import View from 'components/View';
@@ -64,11 +60,7 @@ const walletTypeToViewName = walletType => {
6460export default function Login() {
6561 // globals
6662 const { pop, push, names } = useHistory();
67− const { contracts } = useNetwork();
6863 const { wallet, walletType } = useWallet();
69− const { pointCursor, setPointCursor } = usePointCursor();
70−
71− const [deducing, setDeducing] = useState(false);
7264
7365 // inputs
7466 const [currentTab, setCurrentTab] = useState(
@@ -80,45 +72,10 @@ export default function Login() {
8072 names.ACTIVATE,
8173 ]);
8274
83− const goToPoints = useCallback(() => {
75+ const doContinue = useCallback(() => {
8476 push(names.POINTS);
8577 }, [push, names]);
8678
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−
12279 return (
12380 <View pop={pop} inset>
12481 <Grid>
@@ -142,8 +99,7 @@ export default function Login() {
14299 as={ForwardButton}
143100 solid
144101 className="mt2"
145− loading={deducing}
146− disabled={Nothing.hasInstance(wallet) || deducing}
102+ disabled={Nothing.hasInstance(wallet)}
147103 onClick={doContinue}>
148104 Continue
149105 </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';
22import { Just, Nothing } from 'folktale/maybe';
33import { Grid, H5, H1, HelpText, LinkButton, Flex } from 'indigo-react';
44import { get } from 'lodash';
@@ -79,7 +79,7 @@ function ActionButtons({ actions = [] }) {
7979
8080export default function Points() {
8181 const { wallet } = useWallet();
82− const { pop, push, names } = useHistory();
82+ const { pop, push, popAndPush, names } = useHistory();
8383 const { setPointCursor } = usePointCursor();
8484 const { controlledPoints, getDetails } = usePointCache();
8585 const isEclipticOwner = useIsEclipticOwner();
@@ -88,6 +88,77 @@ export default function Points() {
8888 addRejectedPoint,
8989 ] = useRejectedIncomingPointTransfers();
9090
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+
91162 const address = need.addressFromWallet(wallet);
92163
93164 const loading = Nothing.hasInstance(controlledPoints);
@@ -101,12 +172,7 @@ export default function Points() {
101172 const managingPoints = maybeGetResult(controlledPoints, 'managingPoints', []);
102173 const votingPoints = maybeGetResult(controlledPoints, 'votingPoints', []);
103174 const spawningPoints = maybeGetResult(controlledPoints, 'spawningPoints', []);
104− const outgoingPoints = ownedPoints.filter(point =>
105− getDetails(point).matchWith({
106− Nothing: () => false,
107− Just: p => hasTransferProxy(p.value),
108− })
109− );
175+ const outgoingPoints = maybeOutgoingPoints.getOrElse([]);
110176
111177 const allPoints = [
112178 ...ownedPoints.filter(p => !outgoingPoints.includes(p)),