@@ -0,0 +1,251 @@
1+import React, { useCallback, useEffect, useMemo, useState } from 'react';
2+import cn from 'classnames';
3+import { Grid } from 'indigo-react';
4+import Window from 'components/L2/Window/Window';
5+import HeaderPane from 'components/L2/Window/HeaderPane';
6+import BodyPane from 'components/L2/Window/BodyPane';
7+
8+import * as azimuth from 'azimuth-js';
9+import ob from 'urbit-ob';
10+
11+import { useNetwork } from 'store/network';
12+import { usePointCache } from 'store/pointCache';
13+import { usePointCursor } from 'store/pointCursor';
14+
15+import * as need from 'lib/need';
16+import useEthereumTransaction from 'lib/useEthereumTransaction';
17+import { GAS_LIMITS } from 'lib/constants';
18+import { patp2dec } from 'lib/patp2dec';
19+import { getSpawnCandidate } from 'lib/child';
20+import { useLocalRouter } from 'lib/LocalRouter';
21+import { validateChild } from 'lib/validators';
22+
23+import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
24+import View from 'components/View';
25+import { PointInput, AddressInput } from 'form/Inputs';
26+import {
27+ composeValidator,
28+ buildPointValidator,
29+ buildAddressValidator,
30+ hasErrors,
31+} from 'form/validators';
32+import BridgeForm from 'form/BridgeForm';
33+import FormError from 'form/FormError';
34+import CopiableAddress from 'components/copiable/CopiableAddress';
35+import { convertToInt } from 'lib/convertToInt';
36+import { Box, Icon, Row, Text } from '@tlon/indigo-react';
37+import { isGalaxy } from 'lib/utils/point';
38+import L2BackHeader from 'components/L2/Headers/L2BackHeader';
39+
40+import './IssueChild.scss';
41+import { PatpBadge } from './PatpBadge';
42+import useRoller from 'lib/useRoller';
43+import { AddressButton } from './AddressButton';
44+
45+export function useIssueChild() {
46+ const { contracts } = useNetwork();
47+ const { syncDates } = usePointCache();
48+
49+ const _contracts = need.contracts(contracts);
50+
51+ const [spawnedPoint, setSpawnedPoint] = useState();
52+
53+ return useEthereumTransaction(
54+ useCallback(
55+ (spawnedPoint, owner) => {
56+ setSpawnedPoint(spawnedPoint);
57+ return azimuth.ecliptic.spawn(_contracts, spawnedPoint, owner);
58+ },
59+ [_contracts]
60+ ),
61+ useCallback(() => syncDates(spawnedPoint), [spawnedPoint, syncDates]),
62+ GAS_LIMITS.DEFAULT
63+ );
64+}
65+
66+export default function IssueChild() {
67+ const { pop } = useLocalRouter();
68+ const { api } = useRoller();
69+ const { pointCursor } = usePointCursor();
70+
71+ const _point = convertToInt(need.point(pointCursor), 10);
72+ const [availablePoints, setAvailablePoints] = useState<Set<number> | null>(
73+ null
74+ );
75+ const [candidates, setCandidates] = useState<string[]>([]);
76+
77+ const fetchAvailablePoints = useCallback(async () => {
78+ const available = await api.getUnspawned(_point);
79+ setAvailablePoints(new Set(available));
80+ }, [_point, api]);
81+
82+ const shuffle = useCallback(() => {
83+ const newCandidates = Array.from({ length: 3 }, () => {
84+ return ob.patp(getSpawnCandidate(_point));
85+ });
86+
87+ setCandidates(newCandidates);
88+ }, [_point]);
89+
90+ const handleShuffleClick = useCallback(() => {
91+ shuffle();
92+ }, [shuffle]);
93+
94+ useEffect(() => {
95+ // load the availablePoints for validation
96+ fetchAvailablePoints();
97+ // set the candidates
98+ shuffle();
99+ }, [fetchAvailablePoints, shuffle]);
100+
101+ const {
102+ isDefaultState,
103+ construct,
104+ unconstruct,
105+ completed,
106+ inputsLocked,
107+ bind,
108+ } = useIssueChild();
109+
110+ const validateFormAsync = useCallback(
111+ async values => {
112+ const point = patp2dec(values.point);
113+ const hasPoint = availablePoints && availablePoints.has(point);
114+
115+ if (!hasPoint) {
116+ return { point: 'This point cannot be spawned.' };
117+ }
118+ },
119+ [availablePoints]
120+ );
121+
122+ const validateForm = useCallback(
123+ (values, errors) => {
124+ if (hasErrors(errors)) {
125+ return errors;
126+ }
127+
128+ return validateFormAsync(values, errors);
129+ },
130+ [validateFormAsync]
131+ );
132+
133+ const validate = useMemo(
134+ () =>
135+ composeValidator(
136+ {
137+ point: buildPointValidator(4, [validateChild(ob.patp(_point))]),
138+ owner: buildAddressValidator(),
139+ },
140+ validateForm
141+ ),
142+ [_point, validateForm]
143+ );
144+
145+ const onValues = useCallback(
146+ ({ valid, values }) => {
147+ if (valid) {
148+ construct(patp2dec(values.point), values.owner);
149+ } else {
150+ unconstruct();
151+ }
152+ },
153+ [construct, unconstruct]
154+ );
155+
156+ return (
157+ <View
158+ pop={pop}
159+ hideBack
160+ inset
161+ header={<L2BackHeader hideBalance={false} back={pop} />}>
162+ <Window className="id-issue-child">
163+ <HeaderPane>
164+ <Row>
165+ <h5>Spawn {isGalaxy(_point) ? 'Star' : 'Planet'}</h5>
166+ </Row>
167+ </HeaderPane>
168+ <BodyPane>
169+ <Box className="inner-container">
170+ <BridgeForm
171+ style={{ width: '100%' }}
172+ validate={validate}
173+ onValues={onValues}>
174+ {({ handleSubmit, values }) => (
175+ <>
176+ {completed && (
177+ <Grid.Item
178+ full
179+ as={Text}
180+ className={cn('f5 wrap', {
181+ green3: completed,
182+ })}>
183+ {values.point} has been spawned and can be claimed by{' '}
184+ <CopiableAddress>{values.owner}</CopiableAddress>.
185+ </Grid.Item>
186+ )}
187+
188+ {!completed && (
189+ <>
190+ <Grid.Item
191+ full
192+ as={PointInput}
193+ name="point"
194+ disabled={inputsLocked}
195+ className="mt4"
196+ />
197+
198+ {isDefaultState && candidates.length > 0 && (
199+ <>
200+ <Text className={'input-hint'}>Perhaps:</Text>
201+ <Row>
202+ <Box className={'patp-badge-container'}>
203+ <Box className="badges">
204+ {candidates.map(p => (
205+ <PatpBadge className={'patp-badge'}>
206+ {p}
207+ </PatpBadge>
208+ ))}
209+ </Box>
210+ <Box className="shuffle">
211+ <Icon
212+ size={16}
213+ icon="ArrowRefresh"
214+ onClick={handleShuffleClick}></Icon>
215+ </Box>
216+ </Box>
217+ </Row>
218+ </>
219+ )}
220+
221+ <Grid.Item
222+ full
223+ as={AddressInput}
224+ className="mb4"
225+ name="owner"
226+ label="Ethereum Address"
227+ disabled={inputsLocked}
228+ />
229+ <Box>
230+ <AddressButton>Use my address</AddressButton>
231+ </Box>
232+ </>
233+ )}
234+
235+ <Grid.Item full as={FormError} />
236+
237+ <Grid.Item
238+ full
239+ as={InlineEthereumTransaction}
240+ {...bind}
241+ onReturn={() => pop()}
242+ />
243+ </>
244+ )}
245+ </BridgeForm>
246+ </Box>
247+ </BodyPane>
248+ </Window>
249+ </View>
250+ );
251+}