-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π¦βπ₯ [FIX]: Refresh ledger sync QR code on expiration #7690
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@ledgerhq/errors": patch | ||
"ledger-live-desktop": patch | ||
"live-mobile": patch | ||
"@ledgerhq/trustchain": patch | ||
--- | ||
|
||
Refresh ledger sync QR code on expiration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { createQRCodeHostInstance } from "@ledgerhq/trustchain/qrcode/index"; | ||
import { InvalidDigitsError, NoTrustchainInitialized } from "@ledgerhq/trustchain/errors"; | ||
import { | ||
InvalidDigitsError, | ||
NoTrustchainInitialized, | ||
QRCodeWSClosed, | ||
} from "@ledgerhq/trustchain/errors"; | ||
import { MemberCredentials } from "@ledgerhq/trustchain/types"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { | ||
trustchainSelector, | ||
|
@@ -13,10 +18,12 @@ import { useNavigation } from "@react-navigation/native"; | |
import { NavigatorName, ScreenName } from "~/const"; | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import getWalletSyncEnvironmentParams from "@ledgerhq/live-common/walletSync/getEnvironmentParams"; | ||
import { useQueryClient } from "@tanstack/react-query"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { QueryKey } from "./type.hooks"; | ||
import { useInstanceName } from "./useInstanceName"; | ||
|
||
const MIN_TIME_TO_REFRESH = 30_000; | ||
|
||
interface Props { | ||
setCurrentStep: (step: Steps) => void; | ||
currentStep: Steps; | ||
|
@@ -36,77 +43,68 @@ export function useQRCodeHost({ setCurrentStep, currentStep, currentOption }: Pr | |
); | ||
const memberName = useInstanceName(); | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
const [url, setUrl] = useState<string | null>(null); | ||
const [error, setError] = useState<Error | null>(null); | ||
const [pinCode, setPinCode] = useState<string | null>(null); | ||
|
||
const navigation = useNavigation(); | ||
|
||
const startQRCodeProcessing = useCallback(() => { | ||
if (!memberCredentials || isLoading) return; | ||
|
||
setError(null); | ||
setIsLoading(true); | ||
createQRCodeHostInstance({ | ||
trustchainApiBaseUrl, | ||
onDisplayQRCode: url => { | ||
setUrl(url); | ||
}, | ||
onDisplayDigits: digits => { | ||
setPinCode(digits); | ||
setCurrentStep(Steps.PinDisplay); | ||
}, | ||
addMember: async member => { | ||
if (trustchain) { | ||
await sdk.addMember(trustchain, memberCredentials, member); | ||
return trustchain; | ||
} | ||
throw new NoTrustchainInitialized(); | ||
}, | ||
memberCredentials, | ||
memberName, | ||
alreadyHasATrustchain: !!trustchain, | ||
}) | ||
.then(newTrustchain => { | ||
if (newTrustchain) { | ||
dispatch(setTrustchain(newTrustchain)); | ||
} | ||
queryClient.invalidateQueries({ queryKey: [QueryKey.getMembers] }); | ||
navigation.navigate(NavigatorName.WalletSync, { | ||
screen: ScreenName.WalletSyncLoading, | ||
params: { | ||
created: false, | ||
}, | ||
}); | ||
const { mutate, isPending, error } = useMutation({ | ||
mutationFn: (memberCredentials: MemberCredentials) => | ||
createQRCodeHostInstance({ | ||
trustchainApiBaseUrl, | ||
onDisplayQRCode: url => { | ||
setUrl(url); | ||
}, | ||
onDisplayDigits: digits => { | ||
setPinCode(digits); | ||
setCurrentStep(Steps.PinDisplay); | ||
}, | ||
addMember: async member => { | ||
if (trustchain) { | ||
await sdk.addMember(trustchain, memberCredentials, member); | ||
return trustchain; | ||
} | ||
throw new NoTrustchainInitialized(); | ||
}, | ||
memberCredentials, | ||
memberName, | ||
alreadyHasATrustchain: !!trustchain, | ||
}), | ||
|
||
setUrl(null); | ||
setPinCode(null); | ||
setIsLoading(false); | ||
}) | ||
.catch(e => { | ||
if (e instanceof InvalidDigitsError) { | ||
setCurrentStep(Steps.SyncError); | ||
return; | ||
} else if (e instanceof NoTrustchainInitialized) { | ||
setCurrentStep(Steps.UnbackedError); | ||
return; | ||
} | ||
setError(e); | ||
throw e; | ||
onSuccess: newTrustchain => { | ||
if (newTrustchain) { | ||
dispatch(setTrustchain(newTrustchain)); | ||
} | ||
queryClient.invalidateQueries({ queryKey: [QueryKey.getMembers] }); | ||
navigation.navigate(NavigatorName.WalletSync, { | ||
screen: ScreenName.WalletSyncLoading, | ||
params: { | ||
created: false, | ||
}, | ||
}); | ||
}, [ | ||
trustchain, | ||
memberCredentials, | ||
isLoading, | ||
trustchainApiBaseUrl, | ||
memberName, | ||
setCurrentStep, | ||
sdk, | ||
queryClient, | ||
navigation, | ||
dispatch, | ||
]); | ||
|
||
setUrl(null); | ||
setPinCode(null); | ||
}, | ||
|
||
// Don't use retry here because it always uses a delay despite setting it to 0 | ||
onError: e => { | ||
if (e instanceof QRCodeWSClosed) { | ||
const { time } = e as unknown as { time: number }; | ||
if (time >= MIN_TIME_TO_REFRESH) startQRCodeProcessing(); | ||
} | ||
if (e instanceof InvalidDigitsError) { | ||
setCurrentStep(Steps.SyncError); | ||
} | ||
if (e instanceof NoTrustchainInitialized) { | ||
setCurrentStep(Steps.UnbackedError); | ||
} | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here? |
||
}); | ||
|
||
const startQRCodeProcessing = useCallback(() => { | ||
if (memberCredentials) mutate(memberCredentials); | ||
}, [mutate, memberCredentials]); | ||
|
||
useEffect(() => { | ||
if (currentStep === Steps.QrCodeMethod && currentOption === Options.SHOW_QR) { | ||
|
@@ -117,7 +115,7 @@ export function useQRCodeHost({ setCurrentStep, currentStep, currentOption }: Pr | |
return { | ||
url, | ||
error, | ||
isLoading, | ||
isLoading: isPending, | ||
startQRCodeProcessing, | ||
pinCode, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to add
setError(e); throw e;
also no? Like beforeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
setError
is not needed because react-query manages the error (that's why I removed theuseState
).For the
throw
I don't think it's needed either. From my understanding it was here to avoid thethen
that follows. Or am I missing something ?