Skip to content

Commit

Permalink
fix: change sign hook values to match other hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jul 1, 2022
1 parent 9642726 commit eebac96
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-rivers-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@starknet-react/core': patch
---

Change sign hook values to match other hooks
12 changes: 4 additions & 8 deletions examples/starknet-react-next/pages/sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,20 @@ const Sign: NextPage = () => {
}

const { account } = useStarknet()
const { data, error, isIdle, isError, isSuccess, signTypedData, reset } =
useSignTypedData(typedData)
const { data, error, signTypedData, reset } = useSignTypedData(typedData)

return (
<div>
<ConnectWallet />
<div>
<p>isIdle: {String(isIdle)}</p>
<p>isError: {String(isError)}</p>
<p>{isError && `error: ${error}`}</p>
<p>isSuccess: {String(isSuccess)}</p>
<p>{error && `error: ${error}`}</p>
<p>{data && `data: ${data}`}</p>
</div>
{account && (
<>
<input type="text" value={message} onChange={(evt) => setMessage(evt.target.value)} />
<input type="button" value="Sign Message" disabled={!isIdle} onClick={signTypedData} />
<input type="button" value="Reset" disabled={!isIdle} onClick={reset} />
<input type="button" value="Sign Message" onClick={signTypedData} />
<input type="button" value="Reset" onClick={reset} />
</>
)}
</div>
Expand Down
31 changes: 10 additions & 21 deletions packages/core/src/hooks/sign.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { TypedData } from 'starknet/utils/typedData'
import type { AccountInterface, Signature } from 'starknet'
import { useCallback, useReducer } from 'react'
import { useStarknet } from '..'
import { useStarknet } from '../providers/starknet'

interface State {
data?: string[]
error?: string
isLoading: boolean
isSuccess: boolean
loading: boolean
}

interface StartSigning {
Expand Down Expand Up @@ -39,29 +38,26 @@ function starknetSignReducer(state: State, action: Action): State {
if (action.type === 'start_signing') {
return {
...state,
isLoading: true,
loading: true,
}
} else if (action.type === 'set_signature') {
return {
...state,
data: action.data,
isLoading: false,
isSuccess: true,
loading: false,
}
} else if (action.type === 'set_error') {
return {
...state,
error: action.error,
isLoading: false,
isSuccess: false,
loading: false,
}
} else if (action.type === 'reset') {
return {
...state,
data: undefined,
error: undefined,
isLoading: false,
isSuccess: false,
loading: false,
}
}
return state
Expand All @@ -70,18 +66,14 @@ function starknetSignReducer(state: State, action: Action): State {
export interface UseSignTypedData {
data?: string[]
error?: string
isError: boolean
isIdle: boolean
isLoading: boolean
isSuccess: boolean
loading: boolean
signTypedData: () => Promise<Signature | undefined>
reset: () => void
}

export function useSignTypedData(typedData: TypedData): UseSignTypedData {
const [state, dispatch] = useReducer(starknetSignReducer, {
isLoading: false,
isSuccess: false,
loading: false,
})

const { account: accountAddress, connectors } = useStarknet()
Expand All @@ -90,7 +82,7 @@ export function useSignTypedData(typedData: TypedData): UseSignTypedData {
dispatch({ type: 'reset' })
}, [dispatch])

const { data, error, isLoading, isSuccess } = state
const { data, error, loading } = state

const signTypedData = useCallback(async () => {
dispatch({ type: 'reset' })
Expand Down Expand Up @@ -120,10 +112,7 @@ export function useSignTypedData(typedData: TypedData): UseSignTypedData {
return {
data,
error,
isError: !!error,
isIdle: !isLoading,
isLoading,
isSuccess,
loading,
signTypedData,
reset,
}
Expand Down

0 comments on commit eebac96

Please sign in to comment.