Skip to content

Commit

Permalink
refactor: isSigning
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKheops committed Dec 20, 2024
1 parent 1e85e7f commit e49c95f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions apps/extension/src/ui/domains/Sign/SignLedgerEthereum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export const SignLedgerEthereum: FC<SignHardwareEthereumProps> = ({
}) => {
const { t } = useTranslation()

const { status, error, setStatus, setError } = useSignLedgerBase({ payload })
const { isSigning, error, setIsSigning, setError } = useSignLedgerBase({ payload })

const { sign } = useLedgerEthereum()

const signWithLedger = useCallback(async () => {
if (!payload || !onSigned || !account) return

onSentToDevice?.(true)
setStatus("signing")
setIsSigning(true)

try {
const signature = await sign(
Expand Down Expand Up @@ -67,15 +67,15 @@ export const SignLedgerEthereum: FC<SignHardwareEthereumProps> = ({
onSigned,
payload,
setError,
setStatus,
setIsSigning,
sign,
t,
])

return (
<SignLedgerBase
containerId={containerId}
isProcessing={status !== "ready"}
isProcessing={isSigning}
error={error}
className={className}
onSignClick={signWithLedger}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const SignLedgerSubstrateGeneric: FC<SignHardwareSubstrateProps> = ({
const legacyApp = useLedgerSubstrateAppByName(account?.migrationAppName as string)
const { sign } = useLedgerSubstrateGeneric({ legacyApp })

const { status, error, setStatus, setError } = useSignLedgerBase({ payload })
const { isSigning, error, setIsSigning, setError } = useSignLedgerBase({ payload })

const signWithLedger = useCallback(async () => {
if (!payload || !onSigned || !account) return
Expand Down Expand Up @@ -56,7 +56,7 @@ export const SignLedgerSubstrateGeneric: FC<SignHardwareSubstrateProps> = ({
}

onSentToDevice?.(true)
setStatus("signing")
setIsSigning(true)

try {
const signature = await sign(
Expand All @@ -80,7 +80,7 @@ export const SignLedgerSubstrateGeneric: FC<SignHardwareSubstrateProps> = ({
onSigned,
account,
onSentToDevice,
setStatus,
setIsSigning,
setError,
t,
registry,
Expand All @@ -91,7 +91,7 @@ export const SignLedgerSubstrateGeneric: FC<SignHardwareSubstrateProps> = ({
return (
<SignLedgerBase
containerId={containerId}
isProcessing={status !== "ready"}
isProcessing={isSigning}
error={error}
className={className}
onSignClick={signWithLedger}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export const SignLedgerSubstrateLegacy: FC<SignHardwareSubstrateProps> = ({
const account = useAccountByAddress(payload?.address)
const { sign } = useLedgerSubstrateLegacy(account?.genesisHash)

const { status, error, setStatus, setError } = useSignLedgerBase({ payload })
const { isSigning, error, setIsSigning, setError } = useSignLedgerBase({ payload })

const signWithLedger = useCallback(async () => {
if (!payload || !onSigned || !account) return
if (!registry) return setError(getCustomTalismanLedgerError(t("Missing registry.")))

onSentToDevice?.(true)
setStatus("signing")
setIsSigning(true)

try {
const signature = await sign(payload, account as AccountJsonHardwareSubstrate, registry)
Expand All @@ -45,12 +45,12 @@ export const SignLedgerSubstrateLegacy: FC<SignHardwareSubstrateProps> = ({
} finally {
onSentToDevice?.(false)
}
}, [payload, onSigned, account, registry, setError, t, onSentToDevice, setStatus, sign])
}, [payload, onSigned, account, registry, setError, t, onSentToDevice, setIsSigning, sign])

return (
<SignLedgerBase
containerId={containerId}
isProcessing={status !== "ready"}
isProcessing={isSigning}
error={error}
className={className}
onSignClick={signWithLedger}
Expand Down
18 changes: 8 additions & 10 deletions apps/extension/src/ui/domains/Sign/useSignLedgerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@ import { useCallback, useEffect, useState } from "react"

import { TalismanLedgerError } from "@ui/hooks/ledger/errors"

type SignLedgerStatus = "ready" | "signing" | "signed"

export const useSignLedgerBase = ({ payload }: { payload: unknown }) => {
const [{ status, error }, setState] = useState<{
status: SignLedgerStatus
const [{ isSigning, error }, setState] = useState<{
isSigning: boolean
error: TalismanLedgerError | null
}>({ status: "ready", error: null })
}>({ isSigning: false, error: null })

// reset
useEffect(() => {
setState({ status: "ready", error: null })
setState({ isSigning: false, error: null })
}, [payload])

const setError = useCallback((error: TalismanLedgerError | null) => {
setState({ status: "ready", error })
setState({ isSigning: false, error })
}, [])

const setStatus = useCallback((status: SignLedgerStatus) => {
setState({ status, error: null })
const setIsSigning = useCallback((isSigning: boolean) => {
setState({ isSigning, error: null })
}, [])

return { setError, setStatus, status, error }
return { setError, setIsSigning, isSigning, error }
}

0 comments on commit e49c95f

Please sign in to comment.