Skip to content

Commit

Permalink
refactor(renterd): typescript strict mode for config feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Sep 30, 2024
1 parent c517b14 commit 9a99d95
Show file tree
Hide file tree
Showing 27 changed files with 639 additions and 543 deletions.
8 changes: 5 additions & 3 deletions apps/renterd/components/Config/ConfigStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ import { useApp } from '../../contexts/app'
import { useEstimatedSpending } from './useEstimatedSpending'

export function ConfigStats() {
const { autopilot } = useApp()
const { isAutopilotEnabled } = useApp()
const { redundancyMultiplier, storageTB, configViewMode } = useConfig()

const { canEstimate, estimatedSpendingPerMonth, estimatedSpendingPerTB } =
const { estimatedSpendingPerMonth, estimatedSpendingPerTB } =
useEstimatedSpending()

const perMonth = useSiacoinFiat({ sc: estimatedSpendingPerMonth })
const perTB = useSiacoinFiat({ sc: estimatedSpendingPerTB })

if (autopilot.status !== 'on') {
if (!isAutopilotEnabled) {
return null
}
const canEstimate =
estimatedSpendingPerMonth && estimatedSpendingPerTB && storageTB

return !canEstimate ? (
<Text size="12" font="mono" weight="medium">
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/components/Config/ShouldPinSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FieldSwitch, Text, Tooltip } from '@siafoundation/design-system'
import { UseFormReturn } from 'react-hook-form'
import { SettingsData } from '../../contexts/config/types'
import { InputValues } from '../../contexts/config/types'
import { Fields } from '../../contexts/config/fields'

export function ShouldPinSwitch({
name,
form,
fields,
}: {
name: keyof SettingsData
form: UseFormReturn<SettingsData>
name: keyof InputValues
form: UseFormReturn<InputValues>
fields: Fields
}) {
return (
Expand Down
8 changes: 4 additions & 4 deletions apps/renterd/components/Config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function Config() {
name="allowanceMonthPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down Expand Up @@ -154,7 +154,7 @@ export function Config() {
name="maxStoragePriceTBMonthPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down Expand Up @@ -189,7 +189,7 @@ export function Config() {
name="maxUploadPriceTBPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down Expand Up @@ -224,7 +224,7 @@ export function Config() {
name="maxDownloadPriceTBPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down
22 changes: 6 additions & 16 deletions apps/renterd/components/Config/useEstimatedSpending.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
import { useApp } from '../../contexts/app'
import { useConfig } from '../../contexts/config'
import BigNumber from 'bignumber.js'
import { useMemo } from 'react'

export function useEstimatedSpending() {
const { isAutopilotEnabled } = useApp()
const { form } = useConfig()
const allowanceMonth = form.watch('allowanceMonth')
const storageTB = form.watch('storageTB')
const canEstimate = useMemo(() => {
if (!isAutopilotEnabled) {
return false
}
return !!allowanceMonth?.gt(0) && !!storageTB?.gt(0)
}, [isAutopilotEnabled, allowanceMonth, storageTB])

const estimatedSpendingPerMonth = useMemo(() => {
if (!canEstimate) {
return new BigNumber(0)
if (!allowanceMonth?.gt(0)) {
return undefined
}
return allowanceMonth
}, [canEstimate, allowanceMonth])
}, [allowanceMonth])

const estimatedSpendingPerTB = useMemo(() => {
if (!canEstimate) {
return new BigNumber(0)
if (!estimatedSpendingPerMonth?.gt(0) || !storageTB?.gt(0)) {
return undefined
}
const totalCostPerMonthTB = estimatedSpendingPerMonth.div(storageTB)
return totalCostPerMonthTB
}, [canEstimate, estimatedSpendingPerMonth, storageTB])
}, [estimatedSpendingPerMonth, storageTB])

return {
canEstimate,
estimatedSpendingPerMonth,
estimatedSpendingPerTB,
}
Expand Down
Loading

0 comments on commit 9a99d95

Please sign in to comment.