From b3d846eef8359a182c25ac7335745e73a842c3c7 Mon Sep 17 00:00:00 2001 From: Nischal Shetty Date: Wed, 28 Aug 2024 20:43:07 +0530 Subject: [PATCH] chore(dev): new default for collaborators in dev mode (#820) --- src/components/Send/CollaboratorsSelector.tsx | 31 ++++++++++++------- src/components/Send/helpers.ts | 3 +- src/components/Send/index.tsx | 9 +++++- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/components/Send/CollaboratorsSelector.tsx b/src/components/Send/CollaboratorsSelector.tsx index a190cbdf4..a9253aeac 100644 --- a/src/components/Send/CollaboratorsSelector.tsx +++ b/src/components/Send/CollaboratorsSelector.tsx @@ -1,11 +1,11 @@ -import { useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useField, useFormikContext } from 'formik' import { useSettings } from '../../context/SettingsContext' import * as rb from 'react-bootstrap' import classNames from 'classnames' -import styles from './CollaboratorsSelector.module.css' import { isValidNumCollaborators } from './helpers' +import styles from './CollaboratorsSelector.module.css' type CollaboratorsSelectorProps = { name: string @@ -36,14 +36,23 @@ const CollaboratorsSelector = ({ return field.value === undefined || String(field.value) === customNumCollaboratorsInput }, [field.value, customNumCollaboratorsInput]) - const validateAndSetCustomNumCollaborators = (candidate: string) => { - const parsed = parseInt(candidate, 10) - if (isValidNumCollaborators(parsed, minNumCollaborators)) { - form.setFieldValue(field.name, parsed, true) - } else { - form.setFieldValue(field.name, undefined, true) + const validateAndSetCustomNumCollaborators = useCallback( + (candidate: string) => { + const parsed = parseInt(candidate, 10) + if (isValidNumCollaborators(parsed, minNumCollaborators)) { + form.setFieldValue(field.name, parsed, true) + } else { + form.setFieldValue(field.name, undefined, true) + } + }, + [form, field.name, minNumCollaborators], + ) + + useEffect(() => { + if (!defaultCollaboratorsSelection.includes(field.value)) { + setCustomNumCollaboratorsInput(String(field.value)) } - } + }, [validateAndSetCustomNumCollaborators, field.value, defaultCollaboratorsSelection, setCustomNumCollaboratorsInput]) return ( @@ -55,13 +64,13 @@ const CollaboratorsSelector = ({
{defaultCollaboratorsSelection.map((number) => { - const isSelected = !usesCustomNumCollaborators && field.value === number + const currentlySelected = !usesCustomNumCollaborators && field.value === number return ( { validateAndSetCustomNumCollaborators(String(number)) diff --git a/src/components/Send/helpers.ts b/src/components/Send/helpers.ts index 703244a52..40ca0d8ee 100644 --- a/src/components/Send/helpers.ts +++ b/src/components/Send/helpers.ts @@ -2,10 +2,11 @@ import { isValidNumber } from '../../utils' export const MAX_NUM_COLLABORATORS = 99 -export const initialNumCollaborators = (minValue: number) => { +export const initialNumCollaborators = (minValue: number): number => { if (minValue > 8) { return minValue + pseudoRandomNumber(0, 2) } + return pseudoRandomNumber(8, 10) } diff --git a/src/components/Send/index.tsx b/src/components/Send/index.tsx index 2d4b3f4ed..717e3a93d 100644 --- a/src/components/Send/index.tsx +++ b/src/components/Send/index.tsx @@ -4,6 +4,7 @@ import { FormikProps } from 'formik' import { useTranslation } from 'react-i18next' import * as rb from 'react-bootstrap' import * as Api from '../../libs/JmWalletApi' +import { isDevMode } from '../../constants/debugFeatures' import PageTitle from '../PageTitle' import Sprite from '../Sprite' import { SendForm, SendFormValues } from './SendForm' @@ -25,6 +26,9 @@ const INITIAL_SOURCE_JAR_INDEX = null const INITIAL_AMOUNT = null const INITIAL_IS_COINJOIN = true +// set the default to one collaborat +const DEV_INITIAL_NUM_COLLABORATORS_INPUT = 1 + type MaxFeeConfigMissingAlertProps = { onSuccess: () => void } @@ -97,7 +101,10 @@ export default function Send({ wallet }: SendProps) { const [alert, setAlert] = useState() const [isSending, setIsSending] = useState(false) const [minNumCollaborators, setMinNumCollaborators] = useState(JM_MINIMUM_MAKERS_DEFAULT) - const initNumCollaborators = useMemo(() => initialNumCollaborators(minNumCollaborators), [minNumCollaborators]) + const initNumCollaborators = useMemo( + () => (isDevMode() ? DEV_INITIAL_NUM_COLLABORATORS_INPUT : initialNumCollaborators(minNumCollaborators)), + [minNumCollaborators], + ) const [feeConfigValues, reloadFeeConfigValues] = useFeeConfigValues() const maxFeesConfigMissing = useMemo(