on master help sign in
Merge pull request #992 from urbit/hm/fix-shard-login

login: fix shard validation

commitdd11cb575caa8e52d74dc5dd92a8a7d461c4b91b authorHunter Miller <hunter@hmiller.dev> dateThu 10 Mar 2022, 16:26 (5 years ago) parent749ce562b42591
3 files changed+52−37
src/components/L2/Headers/AdvancedOptions.tsx+17 src/views/Login/Ticket.tsx+34−36 src/views/UrbitOS/NetworkKeys.scss+1−1
src/components/L2/Headers/AdvancedOptions.tsx+17
@@ -1,6 +1,7 @@
11import React, { useState } from 'react';
22import { Text, Box, Checkbox, Icon, Row } from '@tlon/indigo-react';
33import './AdvancedOptions.scss';
4+import styled from 'styled-components';
45
56interface AdvancedOptionsProps {
67 options: {
@@ -11,6 +12,15 @@ interface AdvancedOptionsProps {
1112 }[];
1213}
1314
15+// Hide this input completely
16+const HiddenInput = styled.input`
17+ position: absolute;
18+ opacity: 0;
19+ height: 0;
20+ width: 0;
21+ margin: 0px;
22+`;
23+
1424export default function AdvancedOptions({ options }: AdvancedOptionsProps) {
1525 const showingAny = options.reduce(
1626 (anySelected, { selected }) => anySelected || selected,
@@ -33,6 +43,13 @@ export default function AdvancedOptions({ options }: AdvancedOptionsProps) {
3343 <Box className="options">
3444 {options.map((option, ind) => (
3545 <Row className="option" key={`adv-opt-${ind}`}>
46+ <HiddenInput
47+ checked={option.selected}
48+ name={option.key}
49+ id={option.key}
50+ type="checkbox"
51+ readOnly
52+ />
3653 <Checkbox
3754 className={`checkbox ${option.selected ? 'checked' : ''}`}
3855 {...option}
src/views/Login/Ticket.tsx+34−36
@@ -17,7 +17,6 @@ import { timeout } from 'lib/timeout';
1717import useRoller from 'lib/useRoller';
1818
1919import BridgeForm from 'form/BridgeForm';
20−import Condition from 'form/Condition';
2120import { TicketInput, PassphraseInput, PointInput } from 'form/Inputs';
2221import {
2322 composeValidator,
@@ -53,35 +52,38 @@ export default function Ticket({ className, goHome }: TicketProps) {
5352 const [usePassphrase, setUsePassphrase] = useState(false);
5453 const [useShards, setUseShards] = useState(false);
5554
56− const validateForm = useCallback((values, errors) => {
57− didWarn.current = false;
55+ const validateForm = useCallback(
56+ (values, errors) => {
57+ didWarn.current = false;
5858
59− if (errors.point) {
60− return errors;
61− }
62−
63− if (values.useShards) {
64− if (errors.shard1 || errors.shard2 || errors.shard3) {
65− return errors;
66− }
67− const empty = ['shard1', 'shard2', 'shard3'].filter(s => !values[s]);
68− if (empty.length > 1) {
69− let errors: Record<string, string> = {};
70− empty.forEach(s => {
71− errors[s] = 'Please provide at least two out of three shards.';
72− });
59+ if (errors.point) {
7360 return errors;
7461 }
75− } else {
76− if (errors.ticket) {
77− return errors;
62+
63+ if (useShards) {
64+ if (errors.shard1 || errors.shard2 || errors.shard3) {
65+ return errors;
66+ }
67+ const empty = ['shard1', 'shard2', 'shard3'].filter(s => !values[s]);
68+ if (empty.length > 1) {
69+ let errors: Record<string, string> = {};
70+ empty.forEach(s => {
71+ errors[s] = 'Please provide at least two out of three shards.';
72+ });
73+ return errors;
74+ }
75+ } else {
76+ if (errors.ticket) {
77+ return errors;
78+ }
7879 }
79− }
80− }, []);
80+ },
81+ [useShards]
82+ );
8183
8284 const onSubmit = useCallback(
8385 async values => {
84− const ticket = values.useShards
86+ const ticket = useShards
8587 ? kg.combine(
8688 [values.shard1, values.shard2, values.shard3].map(v =>
8789 v === '' ? undefined : v
@@ -122,41 +124,37 @@ export default function Ticket({ className, goHome }: TicketProps) {
122124 console.error(error);
123125 return {
124126 [FORM_ERROR]: `Unable to derive wallet from ${
125− values.useShards ? 'shards' : 'ticket'
127+ useShards ? 'shards' : 'ticket'
126128 }.`,
127129 };
128130 }
129131 },
130− [setPointCursor, setUrbitWallet, getPoints]
132+ [setPointCursor, setUrbitWallet, getPoints, useShards]
131133 );
132134
135+ const emptyValidator = () => undefined;
136+
133137 const validate = useMemo(
134138 () =>
135139 composeValidator(
136140 {
137− useAdvanced: buildCheckboxValidator(),
138− usePassphrase: buildCheckboxValidator(),
139− useShards: buildCheckboxValidator(),
140141 point: buildPointValidator(4),
141− ticket: buildPatqValidator(),
142− shard1: buildShardValidator(),
143− shard2: buildShardValidator(),
144− shard3: buildShardValidator(),
142+ ticket: useShards ? emptyValidator : buildPatqValidator(),
143+ shard1: useShards ? buildShardValidator() : emptyValidator,
144+ shard2: useShards ? buildShardValidator() : emptyValidator,
145+ shard3: useShards ? buildShardValidator() : emptyValidator,
145146 ticketHidden: buildCheckboxValidator(),
146147 passphrase: buildPassphraseValidator(),
147148 },
148149 validateForm
149150 ),
150− [validateForm]
151+ [validateForm, useShards]
151152 );
152153
153154 const initialValues = useMemo(
154155 () => ({
155156 point: impliedPoint || '',
156− usePasshrase: false,
157− useShards: false,
158157 ticketHidden: true,
159− useAdvanced: false,
160158 }),
161159 [impliedPoint]
162160 );
src/views/UrbitOS/NetworkKeys.scss+1−1
@@ -24,7 +24,7 @@
2424 }
2525
2626 .contents {
27− height: 448px;
27+ height: 100%;
2828 width: 100%;
2929 display: flex;
3030 flex-direction: column;