From fcc3896ee44c93b07363944079738861457741f8 Mon Sep 17 00:00:00 2001 From: Nischal Shetty Date: Wed, 21 Aug 2024 23:27:37 +0530 Subject: [PATCH 1/6] default for collaborators to 1 and added dev tag --- src/components/Send/CollaboratorsSelector.tsx | 25 ++++++++++++++----- src/components/Send/helpers.ts | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/components/Send/CollaboratorsSelector.tsx b/src/components/Send/CollaboratorsSelector.tsx index a190cbdf4..5c7f21504 100644 --- a/src/components/Send/CollaboratorsSelector.tsx +++ b/src/components/Send/CollaboratorsSelector.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react' +import { useEffect, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useField, useFormikContext } from 'formik' import { useSettings } from '../../context/SettingsContext' @@ -25,7 +25,12 @@ const CollaboratorsSelector = ({ const [field] = useField(name) const form = useFormikContext() - const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState() + const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState(String(field.value) || '1') + const [isSelected, setIsSelected] = useState(true) + + useEffect(() => { + validateAndSetCustomNumCollaborators(customNumCollaboratorsInput) + }, [customNumCollaboratorsInput]) const defaultCollaboratorsSelection = useMemo(() => { const start = Math.max(minNumCollaborators, 8) @@ -49,22 +54,29 @@ const CollaboratorsSelector = ({ {t('send.label_num_collaborators', { numCollaborators: field.value })} + dev
{t('send.description_num_collaborators')}
{defaultCollaboratorsSelection.map((number) => { - const isSelected = !usesCustomNumCollaborators && field.value === number + const currentlySelected = !isSelected && field.value === number return ( { validateAndSetCustomNumCollaborators(String(number)) + setIsSelected(!isSelected) + defaultCollaboratorsSelection.forEach((number) => { + if (field.value === number || customNumCollaboratorsInput === String(number)) { + setIsSelected(false) + } + }) }} disabled={disabled} > @@ -78,9 +90,9 @@ const CollaboratorsSelector = ({ max={maxNumCollaborators} isInvalid={usesCustomNumCollaborators && !isValidNumCollaborators(field.value, minNumCollaborators)} placeholder={t('send.input_num_collaborators_placeholder')} - value={customNumCollaboratorsInput || ''} + value={customNumCollaboratorsInput} className={classNames(styles.collaboratorsSelectorElement, 'border', 'border-1', { - [styles.selected]: usesCustomNumCollaborators, + [styles.selected]: isSelected, })} onChange={(e) => { setCustomNumCollaboratorsInput(e.target.value) @@ -91,6 +103,7 @@ const CollaboratorsSelector = ({ if (val !== undefined && val !== '') { setCustomNumCollaboratorsInput(val) validateAndSetCustomNumCollaborators(val) + setIsSelected(!isSelected) } }} disabled={disabled} diff --git a/src/components/Send/helpers.ts b/src/components/Send/helpers.ts index 703244a52..09d216165 100644 --- a/src/components/Send/helpers.ts +++ b/src/components/Send/helpers.ts @@ -6,7 +6,7 @@ export const initialNumCollaborators = (minValue: number) => { if (minValue > 8) { return minValue + pseudoRandomNumber(0, 2) } - return pseudoRandomNumber(8, 10) + return 1 } // not cryptographically random. returned number is in range [min, max] (both inclusive). From 14a87bff65306fd52eccf18367d63c6ba94e0e5d Mon Sep 17 00:00:00 2001 From: Nischal Shetty Date: Thu, 22 Aug 2024 00:01:12 +0530 Subject: [PATCH 2/6] Removed unnecessary states --- src/components/Send/CollaboratorsSelector.tsx | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/components/Send/CollaboratorsSelector.tsx b/src/components/Send/CollaboratorsSelector.tsx index 5c7f21504..13d637610 100644 --- a/src/components/Send/CollaboratorsSelector.tsx +++ b/src/components/Send/CollaboratorsSelector.tsx @@ -1,4 +1,4 @@ -import { useEffect, useMemo, useState } from 'react' +import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useField, useFormikContext } from 'formik' import { useSettings } from '../../context/SettingsContext' @@ -26,11 +26,6 @@ const CollaboratorsSelector = ({ const form = useFormikContext() const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState(String(field.value) || '1') - const [isSelected, setIsSelected] = useState(true) - - useEffect(() => { - validateAndSetCustomNumCollaborators(customNumCollaboratorsInput) - }, [customNumCollaboratorsInput]) const defaultCollaboratorsSelection = useMemo(() => { const start = Math.max(minNumCollaborators, 8) @@ -61,7 +56,7 @@ const CollaboratorsSelector = ({
{defaultCollaboratorsSelection.map((number) => { - const currentlySelected = !isSelected && field.value === number + const currentlySelected = !usesCustomNumCollaborators && field.value === number return ( { validateAndSetCustomNumCollaborators(String(number)) - setIsSelected(!isSelected) - defaultCollaboratorsSelection.forEach((number) => { - if (field.value === number || customNumCollaboratorsInput === String(number)) { - setIsSelected(false) - } - }) }} disabled={disabled} > @@ -92,7 +81,7 @@ const CollaboratorsSelector = ({ placeholder={t('send.input_num_collaborators_placeholder')} value={customNumCollaboratorsInput} className={classNames(styles.collaboratorsSelectorElement, 'border', 'border-1', { - [styles.selected]: isSelected, + [styles.selected]: usesCustomNumCollaborators, })} onChange={(e) => { setCustomNumCollaboratorsInput(e.target.value) @@ -103,7 +92,6 @@ const CollaboratorsSelector = ({ if (val !== undefined && val !== '') { setCustomNumCollaboratorsInput(val) validateAndSetCustomNumCollaborators(val) - setIsSelected(!isSelected) } }} disabled={disabled} From c89d3e9b9c754de9422db8ee3de0c9f63546b1fa Mon Sep 17 00:00:00 2001 From: Nischal Shetty Date: Thu, 22 Aug 2024 19:52:40 +0530 Subject: [PATCH 3/6] Added isDevMode checks --- src/components/Send/CollaboratorsSelector.tsx | 6 ++++-- src/components/Send/helpers.ts | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/Send/CollaboratorsSelector.tsx b/src/components/Send/CollaboratorsSelector.tsx index 13d637610..73d6417e4 100644 --- a/src/components/Send/CollaboratorsSelector.tsx +++ b/src/components/Send/CollaboratorsSelector.tsx @@ -6,6 +6,7 @@ import * as rb from 'react-bootstrap' import classNames from 'classnames' import styles from './CollaboratorsSelector.module.css' import { isValidNumCollaborators } from './helpers' +import { isDevMode } from '../../constants/debugFeatures' type CollaboratorsSelectorProps = { name: string @@ -24,8 +25,9 @@ const CollaboratorsSelector = ({ const [field] = useField(name) const form = useFormikContext() + const initialNumCollaboratorsInput = isDevMode() ? '1' : '' - const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState(String(field.value) || '1') + const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState(initialNumCollaboratorsInput) const defaultCollaboratorsSelection = useMemo(() => { const start = Math.max(minNumCollaborators, 8) @@ -49,7 +51,7 @@ const CollaboratorsSelector = ({ {t('send.label_num_collaborators', { numCollaborators: field.value })} - dev + {isDevMode() && dev}
{t('send.description_num_collaborators')} diff --git a/src/components/Send/helpers.ts b/src/components/Send/helpers.ts index 09d216165..4a1bbc928 100644 --- a/src/components/Send/helpers.ts +++ b/src/components/Send/helpers.ts @@ -1,12 +1,14 @@ +import { isDevMode } from '../../constants/debugFeatures' 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 1 + + return isDevMode() ? 1 : pseudoRandomNumber(8, 10) } // not cryptographically random. returned number is in range [min, max] (both inclusive). From 24c73323b2571ac7caa32cc371707684b1fbd0c2 Mon Sep 17 00:00:00 2001 From: Nischal Shetty Date: Wed, 28 Aug 2024 18:53:35 +0530 Subject: [PATCH 4/6] Removed dev tag ,changed empty string to undefined --- src/components/Send/CollaboratorsSelector.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/components/Send/CollaboratorsSelector.tsx b/src/components/Send/CollaboratorsSelector.tsx index 73d6417e4..c36863fbf 100644 --- a/src/components/Send/CollaboratorsSelector.tsx +++ b/src/components/Send/CollaboratorsSelector.tsx @@ -25,9 +25,11 @@ const CollaboratorsSelector = ({ const [field] = useField(name) const form = useFormikContext() - const initialNumCollaboratorsInput = isDevMode() ? '1' : '' + const initialNumCollaboratorsInput = isDevMode() ? '1' : undefined - const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState(initialNumCollaboratorsInput) + const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState( + initialNumCollaboratorsInput, + ) const defaultCollaboratorsSelection = useMemo(() => { const start = Math.max(minNumCollaborators, 8) @@ -51,7 +53,6 @@ const CollaboratorsSelector = ({ {t('send.label_num_collaborators', { numCollaborators: field.value })} - {isDevMode() && dev}
{t('send.description_num_collaborators')} @@ -81,7 +82,7 @@ const CollaboratorsSelector = ({ max={maxNumCollaborators} isInvalid={usesCustomNumCollaborators && !isValidNumCollaborators(field.value, minNumCollaborators)} placeholder={t('send.input_num_collaborators_placeholder')} - value={customNumCollaboratorsInput} + value={customNumCollaboratorsInput || ''} className={classNames(styles.collaboratorsSelectorElement, 'border', 'border-1', { [styles.selected]: usesCustomNumCollaborators, })} From 2a647b26c0132164e06baedfd2206849815d1469 Mon Sep 17 00:00:00 2001 From: theborakompanioni Date: Wed, 28 Aug 2024 16:21:48 +0200 Subject: [PATCH 5/6] refactor: set initial collaborators in dev mode from main component --- src/components/Send/CollaboratorsSelector.tsx | 33 +++++++++++-------- src/components/Send/helpers.ts | 3 +- src/components/Send/index.tsx | 10 ++++-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/components/Send/CollaboratorsSelector.tsx b/src/components/Send/CollaboratorsSelector.tsx index c36863fbf..a9253aeac 100644 --- a/src/components/Send/CollaboratorsSelector.tsx +++ b/src/components/Send/CollaboratorsSelector.tsx @@ -1,12 +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 { isDevMode } from '../../constants/debugFeatures' +import styles from './CollaboratorsSelector.module.css' type CollaboratorsSelectorProps = { name: string @@ -25,11 +24,8 @@ const CollaboratorsSelector = ({ const [field] = useField(name) const form = useFormikContext() - const initialNumCollaboratorsInput = isDevMode() ? '1' : undefined - const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState( - initialNumCollaboratorsInput, - ) + const [customNumCollaboratorsInput, setCustomNumCollaboratorsInput] = useState() const defaultCollaboratorsSelection = useMemo(() => { const start = Math.max(minNumCollaborators, 8) @@ -40,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 ( diff --git a/src/components/Send/helpers.ts b/src/components/Send/helpers.ts index 4a1bbc928..40ca0d8ee 100644 --- a/src/components/Send/helpers.ts +++ b/src/components/Send/helpers.ts @@ -1,4 +1,3 @@ -import { isDevMode } from '../../constants/debugFeatures' import { isValidNumber } from '../../utils' export const MAX_NUM_COLLABORATORS = 99 @@ -8,7 +7,7 @@ export const initialNumCollaborators = (minValue: number): number => { return minValue + pseudoRandomNumber(0, 2) } - return isDevMode() ? 1 : pseudoRandomNumber(8, 10) + return pseudoRandomNumber(8, 10) } // not cryptographically random. returned number is in range [min, max] (both inclusive). diff --git a/src/components/Send/index.tsx b/src/components/Send/index.tsx index 7f6dd561d..ab0a4829f 100644 --- a/src/components/Send/index.tsx +++ b/src/components/Send/index.tsx @@ -18,7 +18,7 @@ import { useLoadConfigValue } from '../../context/ServiceConfigContext' import { useWaitForUtxosToBeSpent } from '../../hooks/WaitForUtxosToBeSpent' import { routes } from '../../constants/routes' import { JM_MINIMUM_MAKERS_DEFAULT } from '../../constants/config' - +import { isDevMode } from '../../constants/debugFeatures' import { initialNumCollaborators } from './helpers' const INITIAL_DESTINATION = null @@ -26,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 } @@ -98,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( From fc8a863814b4d16ee5ad39b57c3b6f361b878f0a Mon Sep 17 00:00:00 2001 From: theborakompanioni Date: Wed, 28 Aug 2024 16:23:50 +0200 Subject: [PATCH 6/6] refactor: move import --- src/components/Send/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Send/index.tsx b/src/components/Send/index.tsx index ab0a4829f..bd9041a03 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' @@ -18,7 +19,6 @@ import { useLoadConfigValue } from '../../context/ServiceConfigContext' import { useWaitForUtxosToBeSpent } from '../../hooks/WaitForUtxosToBeSpent' import { routes } from '../../constants/routes' import { JM_MINIMUM_MAKERS_DEFAULT } from '../../constants/config' -import { isDevMode } from '../../constants/debugFeatures' import { initialNumCollaborators } from './helpers' const INITIAL_DESTINATION = null