Skip to content

Commit

Permalink
fix: renterd improve estimates allowance, calculated only on save, fi…
Browse files Browse the repository at this point in the history
…at display issue
  • Loading branch information
alexfreska committed Oct 17, 2023
1 parent 5bf282a commit 829d91d
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 126 deletions.
7 changes: 7 additions & 0 deletions .changeset/late-pears-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'hostd': minor
'renterd': minor
'@siafoundation/design-system': minor
---

Fixed an issue where fiat input fields values were not displaying properly.
46 changes: 24 additions & 22 deletions apps/hostd/contexts/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getFields } from './fields'
import { calculateMaxCollateral, transformDown, transformUp } from './transform'
import { useForm } from 'react-hook-form'
import useLocalStorageState from 'use-local-storage-state'
import { useAppSettings } from '@siafoundation/react-core'

export function useConfigMain() {
const settings = useSettings({
Expand Down Expand Up @@ -95,8 +96,21 @@ export function useConfigMain() {
return
}
try {
const calculatedValues: Partial<SettingsData> = {}
if (!showAdvanced) {
calculatedValues.maxCollateral = calculateMaxCollateral(
values.storagePrice,
values.collateralMultiplier
)
}

const finalValues = {
...values,
...calculatedValues,
}

const response = await settingsUpdate.patch({
payload: transformUp(values, settings.data),
payload: transformUp(finalValues, settings.data),
})
if (response.error) {
throw Error(response.error)
Expand All @@ -117,7 +131,7 @@ export function useConfigMain() {
console.log(e)
}
},
[form, settings, settingsUpdate, revalidateAndResetFormData]
[form, showAdvanced, settings, settingsUpdate, revalidateAndResetFormData]
)

const fields = useMemo(() => getFields({ showAdvanced }), [showAdvanced])
Expand All @@ -129,26 +143,6 @@ export function useConfigMain() {
[form, onValid, onInvalid]
)

const storage = form.watch('storagePrice')
const collateralMultiplier = form.watch('collateralMultiplier')

// if simple mode, then calculate and set max collateral
useEffect(() => {
if (
!showAdvanced &&
storage?.isGreaterThan(0) &&
collateralMultiplier?.isGreaterThan(0)
) {
form.setValue(
'maxCollateral',
calculateMaxCollateral(storage, collateralMultiplier),
{
shouldDirty: true,
}
)
}
}, [form, showAdvanced, storage, collateralMultiplier])

// Resets so that stale values that are no longer in sync with what is on
// the daemon will show up as changed.
const resetWithUserChanges = useCallback(() => {
Expand All @@ -166,6 +160,14 @@ export function useConfigMain() {
}
}, [form, resetFormDataIfAllDataFetched])

const { isUnlocked } = useAppSettings()
useEffect(() => {
if (isUnlocked) {
revalidateAndResetFormData()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isUnlocked])

useEffect(() => {
if (form.formState.isSubmitting) {
return
Expand Down
10 changes: 6 additions & 4 deletions apps/renterd/contexts/config/fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ export function getFields({
units: 'SC/month',
decimalsLimitSc: scDecimalPlaces,
hidden: !isAutopilotEnabled || !showAdvanced,
// always required, but set in background unless advanced mode
validation: {
required: 'required',
},
validation:
isAutopilotEnabled && showAdvanced
? {
required: 'required',
}
: {},
},
periodWeeks: {
type: 'number',
Expand Down
Loading

0 comments on commit 829d91d

Please sign in to comment.