Skip to content

Commit 46d3228

Browse files
committed
refactor: make updates after review
1 parent 6de53fd commit 46d3228

File tree

7 files changed

+40
-78
lines changed

7 files changed

+40
-78
lines changed

features/settings/main/components/controllers/radio-selector/radio-selector.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ export type RadioFormData = {
2222

2323
type VotingSelectorProps = {
2424
data?: RadioFormData[];
25-
radioType: keyof VaultInfo;
25+
vaultKey: keyof VaultInfo;
2626
title: string;
2727
};
2828

2929
export const RadioSelector: FC<VotingSelectorProps> = ({
3030
data,
31-
radioType,
31+
vaultKey,
3232
title,
3333
}) => {
3434
const { isLoading, errors, disabled } = useFormState();
3535
const { hasConfirmingRole } = useVaultConfirmingRoles();
3636
const { hasPermission } = useVaultPermission();
3737
const { register, setValue } = useFormContext();
38-
const inputError = errors[radioType];
38+
const inputError = errors[vaultKey];
3939
const isEditable = !disabled && (hasConfirmingRole || hasPermission);
4040

4141
return (
@@ -45,7 +45,7 @@ export const RadioSelector: FC<VotingSelectorProps> = ({
4545
{isEditable && !isLoading ? (
4646
<>
4747
{data?.map(({ type, value, tags, symbol, placeholder }, index) => {
48-
const key = `${radioType}-${type}-${index}-${value}`;
48+
const key = `${vaultKey}-${type}-${index}-${value}`;
4949
const isCustom = type === 'custom';
5050
const isMy = type === 'My proposal';
5151

@@ -58,9 +58,9 @@ export const RadioSelector: FC<VotingSelectorProps> = ({
5858
symbol: symbol,
5959
tags: tags,
6060
id: key,
61-
...register(radioType),
61+
...register(vaultKey),
6262
}}
63-
type={radioType}
63+
type={vaultKey}
6464
placeholder={placeholder}
6565
setRadioValue={setValue}
6666
error={inputError?.message as string}
@@ -74,14 +74,14 @@ export const RadioSelector: FC<VotingSelectorProps> = ({
7474
tags={tags}
7575
id={key}
7676
symbol={symbol}
77-
{...register(radioType)}
77+
{...register(vaultKey)}
7878
disabled={isMy}
7979
/>
8080
);
8181
})}
8282
</>
8383
) : (
84-
<ReadonlyView vaultKey={radioType} />
84+
<ReadonlyView vaultKey={vaultKey} />
8585
)}
8686
</RadioSelectorContainer>
8787
);

features/settings/main/components/voting/voting.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
import { FC, useMemo } from 'react';
22

3+
import { vaultTexts } from 'modules/vaults';
4+
35
import { useMainSettingsData } from 'features/settings/main/contexts';
46
import { RadioSelector } from '../controllers/radio-selector';
57
import { VotingBlock } from './styles';
68

79
export const Voting: FC = () => {
810
const mainSettingsData = useMainSettingsData();
911

10-
const nodeOperatorFeeBP = useMemo(() => {
11-
return mainSettingsData?.nodeOperatorFeeBP;
12-
}, [mainSettingsData]);
13-
14-
const confirmExpiry = useMemo(() => {
15-
return mainSettingsData?.confirmExpiry;
12+
const [nodeOperatorFeeBP, confirmExpiry] = useMemo(() => {
13+
return [
14+
mainSettingsData?.nodeOperatorFeeBP,
15+
mainSettingsData?.confirmExpiry,
16+
];
1617
}, [mainSettingsData]);
1718

1819
return (
1920
<VotingBlock>
2021
<RadioSelector
2122
data={nodeOperatorFeeBP}
22-
radioType="nodeOperatorFeeBP"
23-
title="Node Operator Fee"
23+
vaultKey="nodeOperatorFeeBP"
24+
title={vaultTexts.actions.settings.fields.nodeOperatorFee.title}
2425
/>
2526
<RadioSelector
2627
data={confirmExpiry}
27-
radioType="confirmExpiry"
28-
title="Confirmation Lifetime"
28+
vaultKey="confirmExpiry"
29+
title={vaultTexts.actions.settings.fields.confirmationLifetime.title}
2930
/>
3031
</VotingBlock>
3132
);

features/settings/main/consts.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { z } from 'zod';
22
import { isValidAnyAddress } from 'utils/address-validation';
33
import {
44
MainSettingsOverview,
5-
MainSettingsVoting,
65
ManagersKeys,
76
ManagersNewAddresses,
87
RoleFieldSchema,
@@ -25,6 +24,7 @@ const INVALID_NUMBER_MAX_MESSAGE = `Must be ${MAX_FEE_VALUE} or less`;
2524
const INVALID_NUMBER_EXPIRY_MIN_MESSAGE = `Must be ${MIN_CONFIRM_EXPIRY} or above`;
2625
const INVALID_NUMBER_EXPIRY_MAX_MESSAGE = `Must be ${MAX_CONFIRM_EXPIRY} or less`;
2726
const INVALID_NUMBER_DATA_OBJECT_MESSAGE = { message: 'Only number is valid' };
27+
const INVALID_EMPTY_STRING = 'Value cannot be empty';
2828

2929
export const accountSchema = z
3030
.string()
@@ -56,35 +56,16 @@ export const editMainSettingsSchema = z.object({
5656
defaultAdmins: z.array(addressSchema),
5757
nodeOperatorFeeBP: z
5858
.string()
59-
.refine((val) => val !== '', { message: 'Value cannot be empty' })
59+
.refine((val) => val !== '', { message: INVALID_EMPTY_STRING })
6060
.pipe(votingFeeSchema)
6161
.transform((val) => String(val)),
6262
confirmExpiry: z
6363
.string()
64-
.refine((val) => val !== '', { message: 'Value cannot be empty' })
64+
.refine((val) => val !== '', { message: INVALID_EMPTY_STRING })
6565
.pipe(votingLifetimeSchema)
6666
.transform((val) => String(val)),
6767
});
6868

69-
export const indicatorsForRender: MainSettingsVoting[] = [
70-
{
71-
name: 'nodeOperatorFeeBP',
72-
textFieldName: 'nodeOperatorFeeBPCustom',
73-
unitIndicator: '%',
74-
vaultKey: 'nodeOperatorFeeBP',
75-
canEditRole: 'confirmingRoles',
76-
...vaultTexts.actions.settings.fields.nodeOperatorFee,
77-
},
78-
{
79-
name: 'confirmExpiry',
80-
textFieldName: 'confirmExpiryCustom',
81-
unitIndicator: ' hours',
82-
vaultKey: 'confirmExpiry',
83-
canEditRole: 'confirmingRoles',
84-
...vaultTexts.actions.settings.fields.confirmationLifetime,
85-
},
86-
];
87-
8869
export const adminsForRender: MainSettingsOverview[] = [
8970
{
9071
name: 'defaultAdmins',

features/settings/main/contexts/main-settings-data-provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const MainSettingsDataProvider: FC<PropsWithChildren> = ({
3535
}
3636

3737
// Current values for voting
38-
const currentConfirmExpiry = activeVault.confirmExpiry / (60n * 60n);
38+
const currentConfirmExpiry = Number(activeVault.confirmExpiry) / 3600;
3939
const currentNodeOperatorFeeBP = Number(
4040
(activeVault.nodeOperatorFeeBP * 100n) / VAULT_TOTAL_BASIS_POINTS_BN,
4141
);

features/settings/main/types.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ export type MainSettingsOverview = {
3636
canEditRole: VAULT_ROOT_ROLES | 'confirmingRoles';
3737
};
3838

39-
export type MainSettingsVoting = Omit<
40-
MainSettingsOverview,
41-
'dataType' | 'name'
42-
> & {
43-
unitIndicator: '%' | ' hours';
44-
name: VotingKeys;
45-
textFieldName: VotingCustomKeys;
46-
};
47-
4839
export type TxData = {
4940
grantRoles?: GrantOrRevokeRole[];
5041
revokeRoles?: GrantOrRevokeRole[];
@@ -54,16 +45,10 @@ export type TxData = {
5445

5546
export type RoleFieldSchema = z.infer<typeof addressSchema>;
5647
export type ManagersKeys = 'nodeOperatorManagers' | 'defaultAdmins';
57-
export type VotingKeys = 'nodeOperatorFeeBP' | 'confirmExpiry';
58-
export type VotingCustomKeys =
59-
| 'nodeOperatorFeeBPCustom'
60-
| 'confirmExpiryCustom';
6148
export type ManagersNewAddresses = {
6249
addresses: Record<ManagersKeys, RoleFieldSchema[]>;
6350
};
6451

65-
export type ChipBadge = Date | string | undefined;
66-
6752
export type MainSettingsDataContextValue = {
6853
defaultAdmins: RoleFieldSchema[];
6954
nodeOperatorManagers: RoleFieldSchema[];

features/settings/main/utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ export const shouldIncrementTxCounter = (
88
return value !== defaultValue;
99
};
1010

11-
export const getHoursDifference = (dateA: Date, dateB: Date) => {
12-
return Math.ceil((dateA.getTime() - dateB.getTime()) / (1000 * 60 * 60));
13-
};
14-
1511
export const prepareDefaultValues = (
1612
promisifiedSettingsData: Promise<MainSettingsDataContextValue | null>,
1713
) => {
@@ -40,3 +36,15 @@ export const prepareDefaultValues = (
4036
};
4137
};
4238
};
39+
40+
export const formatInputValue = (
41+
value: string,
42+
symbol: string | undefined,
43+
isFocused: boolean,
44+
) => {
45+
if (symbol && value) {
46+
return isFocused ? value : `${value}${symbol}`;
47+
}
48+
49+
return value;
50+
};

shared/components/radio-input/radio-with-input/radio-with-input.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
useRef,
3-
forwardRef,
4-
useState,
5-
ChangeEvent,
6-
useCallback,
7-
useMemo,
8-
} from 'react';
1+
import { useRef, forwardRef, useState, ChangeEvent, useCallback } from 'react';
92
import { UseFormSetValue, FieldValues } from 'react-hook-form';
103
import { mergeRefs } from 'utils';
114
import { InputProps } from '@lidofinance/lido-ui';
@@ -14,6 +7,8 @@ import { RadioInput, RadioInputProps } from '../radio';
147

158
import { InputStyled } from './radio-with-input.styles';
169

10+
import { formatInputValue } from 'features/settings/main/utils';
11+
1712
type RadioWithInputProps = Omit<InputProps, 'type'> & {
1813
radioProps: RadioInputProps;
1914
setRadioValue: UseFormSetValue<FieldValues>;
@@ -26,18 +21,10 @@ export const RadioWithInput = forwardRef<HTMLInputElement, RadioWithInputProps>(
2621

2722
const [value, setValue] = useState<string>('');
2823
const [isFocused, setIsFocused] = useState(false);
29-
30-
const displayValue = useMemo(() => {
31-
if (radioProps.symbol && value) {
32-
return isFocused ? value : `${value}${radioProps.symbol}`;
33-
}
34-
35-
return value;
36-
// eslint-disable-next-line react-hooks/exhaustive-deps
37-
}, [value, radioProps.symbol, isFocused]);
38-
3924
const radioRef = useRef<HTMLInputElement>(null);
4025

26+
const displayValue = formatInputValue(value, radioProps.symbol, isFocused);
27+
4128
const updateRadioValue = useCallback(
4229
(value: string) => {
4330
setRadioValue(type, String(value), {

0 commit comments

Comments
 (0)