1import React, { useCallback, useEffect, useMemo, useState } from 'react';
2import cn from 'classnames';
3import { Grid } from 'indigo-react';
4import Window from 'components/L2/Window/Window';
5import HeaderPane from 'components/L2/Window/HeaderPane';
6import BodyPane from 'components/L2/Window/BodyPane';
8import * as azimuth from 'azimuth-js';
9import ob from 'urbit-ob';
11import { useNetwork } from 'store/network';
12import { usePointCache } from 'store/pointCache';
13import { usePointCursor } from 'store/pointCursor';
15import * as need from 'lib/need';
16import useEthereumTransaction from 'lib/useEthereumTransaction';
17import { GAS_LIMITS } from 'lib/constants';
18import { patp2dec } from 'lib/patp2dec';
19import { getSpawnCandidate } from 'lib/child';
20import { useLocalRouter } from 'lib/LocalRouter';
21import { validateChild } from 'lib/validators';
23import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
24import View from 'components/View';
25import { PointInput, AddressInput } from 'form/Inputs';
29 buildAddressValidator,
31} from 'form/validators';
32import BridgeForm from 'form/BridgeForm';
33import FormError from 'form/FormError';
34import CopiableAddress from 'components/copiable/CopiableAddress';
35import { convertToInt } from 'lib/convertToInt';
36import { Box, Icon, Row, Text } from '@tlon/indigo-react';
37import { isGalaxy } from 'lib/utils/point';
38import L2BackHeader from 'components/L2/Headers/L2BackHeader';
40import './IssueChild.scss';
41import { PatpBadge } from './PatpBadge';
42import useRoller from 'lib/useRoller';
43import { AddressButton } from './AddressButton';
45export function useIssueChild() {
46 const { contracts } = useNetwork();
47 const { syncDates } = usePointCache();
49 const _contracts = need.contracts(contracts);
51 const [spawnedPoint, setSpawnedPoint] = useState();
53 return useEthereumTransaction(
55 (spawnedPoint, owner) => {
56 setSpawnedPoint(spawnedPoint);
57 return azimuth.ecliptic.spawn(_contracts, spawnedPoint, owner);
61 useCallback(() => syncDates(spawnedPoint), [spawnedPoint, syncDates]),
66export default function IssueChild() {
67 const { pop } = useLocalRouter();
68 const { api } = useRoller();
69 const { pointCursor } = usePointCursor();
71 const _point = convertToInt(need.point(pointCursor), 10);
72 const [availablePoints, setAvailablePoints] = useState<Set<number> | null>(
75 const [candidates, setCandidates] = useState<string[]>([]);
77 const fetchAvailablePoints = useCallback(async () => {
78 const available = await api.getUnspawned(_point);
79 setAvailablePoints(new Set(available));
82 const shuffle = useCallback(() => {
83 const newCandidates = Array.from({ length: 3 }, () => {
84 return ob.patp(getSpawnCandidate(_point));
87 setCandidates(newCandidates);
90 const handleShuffleClick = useCallback(() => {
95 // load the availablePoints for validation
96 fetchAvailablePoints();
99 }, [fetchAvailablePoints, shuffle]);
110 const validateFormAsync = useCallback(
112 const point = patp2dec(values.point);
113 const hasPoint = availablePoints && availablePoints.has(point);
116 return { point: 'This point cannot be spawned.' };
122 const validateForm = useCallback(
123 (values, errors) => {
124 if (hasErrors(errors)) {
128 return validateFormAsync(values, errors);
133 const validate = useMemo(
137 point: buildPointValidator(4, [validateChild(ob.patp(_point))]),
138 owner: buildAddressValidator(),
142 [_point, validateForm]
145 const onValues = useCallback(
146 ({ valid, values }) => {
148 construct(patp2dec(values.point), values.owner);
153 [construct, unconstruct]
161 header={<L2BackHeader hideBalance={false} back={pop} />}>
162 <Window className="id-issue-child">
165 <h5>Spawn {isGalaxy(_point) ? 'Star' : 'Planet'}</h5>
169 <Box className="inner-container">
171 style={{ width: '100%' }}
174 {({ handleSubmit, values }) => (
180 className={cn('f5 wrap', {
183 {values.point} has been spawned and can be claimed by{' '}
184 <CopiableAddress>{values.owner}</CopiableAddress>.
194 disabled={inputsLocked}
198 {isDefaultState && candidates.length > 0 && (
200 <Text className={'input-hint'}>Perhaps:</Text>
202 <Box className={'patp-badge-container'}>
203 <Box className="badges">
204 {candidates.map(p => (
205 <PatpBadge className={'patp-badge'}>
210 <Box className="shuffle">
214 onClick={handleShuffleClick}></Icon>
226 label="Ethereum Address"
227 disabled={inputsLocked}
231 <AddressButton>Use my address</AddressButton>
237 <Grid.Item full as={FormError} />
241 as={InlineEthereumTransaction}
243 onReturn={() => pop()}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list