Skip to content

Commit

Permalink
feat: trim leading/trailing whitespace in verification codes/rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
kleysc committed Dec 18, 2024
1 parent d42ce8a commit d5dcca2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import {
useConsensusPolling,
useGuardianSetupApi,
useGuardianSetupContext,
useTrimmedInputArray,
} from '../../../../../hooks';
import { useTrimmedInputArray } from '../../../../../hooks';

interface PeerWithHash {
id: string;
Expand All @@ -64,6 +64,9 @@ export const VerifyGuardians: React.FC<Props> = ({ next }) => {
const isHost = role === GuardianRole.Host;
const [myHash, setMyHash] = useState('');
const [peersWithHash, setPeersWithHash] = useState<PeerWithHash[]>();
const [enteredHashes, handleHashChange] = useTrimmedInputArray(
peersWithHash ? peersWithHash.map(() => '') : []
);
const [verifiedConfigs, setVerifiedConfigs] = useState<boolean>(false);
const [isStarting, setIsStarting] = useState(false);
const [error, setError] = useState<string>();
Expand All @@ -73,9 +76,6 @@ export const VerifyGuardians: React.FC<Props> = ({ next }) => {
// Poll for peers and configGenParams while on this page.
useConsensusPolling();

const [enteredHashes, handleHashChange, setEnteredHashes] =
useTrimmedInputArray(peersWithHash ? peersWithHash.map(() => '') : []);

useEffect(() => {
async function assembleHashInfo() {
if (peers.length === 0) {
Expand Down Expand Up @@ -110,49 +110,16 @@ export const VerifyGuardians: React.FC<Props> = ({ next }) => {
const otherPeers = Object.entries(peers).filter(
([id]) => id !== ourCurrentId.toString()
);
setEnteredHashes(
otherPeers.map(([id]) => hashes[id as unknown as number])
);
otherPeers.forEach(([id], idx) => {
handleHashChange(idx, hashes[id as unknown as number]);
});
}
} catch (err) {
setError(formatApiErrorMessage(err));
}
}
assembleHashInfo();
}, [api, peers, ourCurrentId, t, setEnteredHashes]);

const tableRows = useMemo(() => {
if (!peersWithHash) return [];
return peersWithHash.map(({ peer, hash }, idx) => {
const value = enteredHashes[idx] || '';
const isValid = Boolean(value && value === hash);
const isError = Boolean(value && !isValid);
return {
key: peer.cert,
name: (
<Text maxWidth={200} isTruncated>
{peer.name}
</Text>
),
status: isValid ? (
<Tag colorScheme='green'>{t('verify-guardians.verified')}</Tag>
) : (
''
),
hashInput: (
<FormControl isInvalid={isError}>
<Input
variant='filled'
value={value}
placeholder={`${t('verify-guardians.verified-placeholder')}`}
onChange={(ev) => handleHashChange(idx, ev.currentTarget.value)}
readOnly={isValid}
/>
</FormControl>
),
};
});
}, [peersWithHash, enteredHashes, t, handleHashChange]);
}, [api, peers, ourCurrentId, t, handleHashChange]);

useEffect(() => {
// If we're the only guardian, skip this verify other guardians step.
Expand Down Expand Up @@ -234,6 +201,39 @@ export const VerifyGuardians: React.FC<Props> = ({ next }) => {
sm: 'row',
}) as StackDirection | undefined;

const tableRows = useMemo(() => {
if (!peersWithHash) return [];
return peersWithHash.map(({ peer, hash }, idx) => {
const value = enteredHashes[idx] || '';
const isValid = Boolean(value && value === hash);
const isError = Boolean(value && !isValid);
return {
key: peer.cert,
name: (
<Text maxWidth={200} isTruncated>
{peer.name}
</Text>
),
status: isValid ? (
<Tag colorScheme='green'>{t('verify-guardians.verified')}</Tag>
) : (
''
),
hashInput: (
<FormControl isInvalid={isError}>
<Input
variant='filled'
value={value}
placeholder={`${t('verify-guardians.verified-placeholder')}`}
onChange={(ev) => handleHashChange(idx, ev.currentTarget.value)}
readOnly={isValid}
/>
</FormControl>
),
};
});
}, [peersWithHash, enteredHashes, handleHashChange, t]);

if (error) {
return (
<Flex direction='column' gap={6}>
Expand Down
16 changes: 1 addition & 15 deletions apps/router/src/hooks/custom/useTrimmedInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ import { useState } from 'react';

const cleanInput = (value: string) => value.trim();

export const useTrimmedInput = (initialValue: string) => {
const [value, setValue] = useState(initialValue);

const handleChange = (newValue: string) => {
setValue(cleanInput(newValue));
};

return [value, handleChange] as const;
};

export const useTrimmedInputArray = (initialValues: string[]) => {
const [values, setValues] = useState<string[]>(initialValues);

Expand All @@ -23,9 +13,5 @@ export const useTrimmedInputArray = (initialValues: string[]) => {
});
};

const setAllValues = (newValues: string[]) => {
setValues(newValues.map(cleanInput));
};

return [values, handleChange, setAllValues] as const;
return [values, handleChange] as const;
};
5 changes: 1 addition & 4 deletions apps/router/src/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { useLocation } from 'react-router-dom';
export * from './guardian/useGuardian';
export * from './guardian/useGuardianSetup';
export * from './gateway/useGateway';
export {
useTrimmedInput,
useTrimmedInputArray,
} from './custom/useTrimmedInput';
export * from './custom/useTrimmedInput';

import { useContext } from 'react';
import { AppContext, AppContextValue } from '../context/AppContext';
Expand Down

0 comments on commit d5dcca2

Please sign in to comment.