refactor(sweep): use geterrorreason and clarify schedule index naming (#1122)

* refactor(sweep): use getErrorReason in sweep error paths

* refactor(sweep): rename currentTransaction to currentTransactionIndex

* fix(sweep): keep runschedule mutation error typing compatible

---------

Co-authored-by: Saurabh Singh <saurabhsraghuvanshi@gmail.com>
This commit is contained in:
Parth 2026-02-18 17:50:37 +05:30 committed by theborakompanioni
parent de3f5375df
commit 992d826179
No known key found for this signature in database
GPG key ID: E8070AF0053AAC0D
4 changed files with 10 additions and 21 deletions

View file

@ -25,6 +25,7 @@ import { useJamWalletInfoContext } from '@/context/JamWalletInfoContext'
import { useApiClient } from '@/hooks/useApiClient'
import { useFeeConfigValidation } from '@/hooks/useFeeConfigValidation'
import { useRefreshSession } from '@/hooks/useRefreshSession'
import { getErrorReason } from '@/lib/errorReason'
import type { WalletFileName } from '@/lib/utils'
import { jmSessionStore } from '@/store/jmSessionStore'
import { Spinner } from '../ui/spinner'
@ -40,18 +41,6 @@ const WAIT_FOR_UPDATE_SESSION_POLLING_DELAY = 1_000
const initialDestinationAddresses = () => Array.from({ length: DESTINATION_ADDRESS_COUNT }, () => '')
const initialTouchedValues = () => Array.from({ length: DESTINATION_ADDRESS_COUNT }, () => false)
const toErrorReason = (error: unknown, fallback: string): string => {
if (typeof error === 'object' && error !== null) {
if ('message' in error && typeof error.message === 'string' && error.message.trim() !== '') {
return error.message
}
if ('error_description' in error && typeof error.error_description === 'string' && error.error_description !== '') {
return error.error_description
}
}
return fallback
}
export const SweepPage = ({ walletFileName }: SweepPageProps) => {
const { t } = useTranslation()
const client = useApiClient()
@ -103,7 +92,7 @@ export const SweepPage = ({ walletFileName }: SweepPageProps) => {
if (result.response.status === 404) {
return null
}
throw new Error(toErrorReason(result.error, 'Failed to load schedule'))
throw new Error(getErrorReason(result.error, 'Failed to load schedule'))
}
return result.data
@ -136,8 +125,7 @@ export const SweepPage = ({ walletFileName }: SweepPageProps) => {
setShowScheduleConfirmDialog(false)
},
onError: (error: ErrorMessage) => {
const reason = error.message || error.error_description || t('global.errors.reason_unknown')
// TODO: i18n add reason to message
const reason = getErrorReason(error, t('global.errors.reason_unknown'))
const message = `${t('scheduler.error_starting_schedule_failed')} ${reason}`
setAlertMessage(message)
toast.error(message)
@ -156,7 +144,8 @@ export const SweepPage = ({ walletFileName }: SweepPageProps) => {
setLocalSchedule(undefined)
},
onError: (error: unknown) => {
const message = `${t('scheduler.error_stopping_schedule_failed')} ${toErrorReason(error, t('global.errors.reason_unknown'))}`
const reason = getErrorReason(error, t('global.errors.reason_unknown'))
const message = `${t('scheduler.error_stopping_schedule_failed')} ${reason}`
setAlertMessage(message)
toast.error(message)
},

View file

@ -81,7 +81,7 @@ export const SweepScheduleProgress = ({ schedule, isStopping, onStop }: SweepSch
<Trans
i18nKey="scheduler.progress_current_state"
values={{
current: progress.currentTransaction,
current: progress.currentTransactionIndex + 1,
total: progress.totalTransactions,
}}
components={highlightedComponents}

View file

@ -24,7 +24,7 @@ describe('scheduleUtils', () => {
expect(summary.totalTransactions).toBe(3)
expect(summary.completedTransactions).toBe(1)
expect(summary.currentTransaction).toBe(2)
expect(summary.currentTransactionIndex).toBe(1)
expect(summary.isDone).toBe(false)
expect(summary.steps).toHaveLength(3)
expect(summary.steps[1].isActive).toBe(true)

View file

@ -13,7 +13,7 @@ export interface ScheduleProgressSummary {
totalWaitSeconds: number
totalTransactions: number
completedTransactions: number
currentTransaction: number
currentTransactionIndex: number
isDone: boolean
steps: ScheduleProgressStep[]
}
@ -51,7 +51,7 @@ export const toScheduleProgressSummary = (schedule: Schedule): ScheduleProgressS
totalWaitSeconds: 0,
totalTransactions: 0,
completedTransactions: 0,
currentTransaction: 0,
currentTransactionIndex: 0,
isDone: true,
steps: [],
}
@ -92,7 +92,7 @@ export const toScheduleProgressSummary = (schedule: Schedule): ScheduleProgressS
totalWaitSeconds,
totalTransactions: schedule.length,
completedTransactions: Math.min(completedTransactions, schedule.length),
currentTransaction: Math.min(completedTransactions + 1, schedule.length),
currentTransactionIndex: Math.min(completedTransactions, schedule.length - 1),
isDone,
steps,
}