Skip to content
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

Allow Disconnect of MWA #960

Merged
merged 8 commits into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions packages/core/react/src/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ export function WalletProvider({
}
return [mobileWalletAdapter, ...adaptersWithStandardAdapters];
}, [adaptersWithStandardAdapters, mobileWalletAdapter]);
const [walletName, setWalletName] = useLocalStorage<WalletName | null>(
localStorageKey,
getIsMobile(adaptersWithStandardAdapters) ? SolanaMobileWalletAdapterWalletName : null
);
const [walletName, setWalletName] = useLocalStorage<WalletName | null>(localStorageKey, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure there's anything good to be done about this, but if the user has opened the app before, I think it'll probably still have MWA in local storage and selected by default.

Copy link
Contributor Author

@alex-fung alex-fung Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea fair enough – though that behavior exists for any adapter so at least it's fair?

const adapter = useMemo(
() => adaptersWithMobileWalletAdapter.find((a) => a.name === walletName) ?? null,
[adaptersWithMobileWalletAdapter, walletName]
() =>
adaptersWithMobileWalletAdapter.find((a) => a.name === walletName) ??
(adaptersWithMobileWalletAdapter.length === 1 && adaptersWithMobileWalletAdapter[0] === mobileWalletAdapter
? mobileWalletAdapter
: null),
[adaptersWithMobileWalletAdapter, walletName, mobileWalletAdapter]
);
const changeWallet = useCallback(
(nextWalletName: WalletName<string> | null) => {
Expand All @@ -105,8 +106,6 @@ export function WalletProvider({
if (!adapter) return;
function handleDisconnect() {
if (isUnloadingRef.current) return;
// Leave the adapter selected in the event of a disconnection.
if (walletName === SolanaMobileWalletAdapterWalletName && getIsMobile(adaptersWithStandardAdapters)) return;
setWalletName(null);
}
adapter.on('disconnect', handleDisconnect);
Expand Down Expand Up @@ -150,7 +149,7 @@ export function WalletProvider({
};
}, [adaptersWithStandardAdapters, walletName]);
const handleConnectError = useCallback(() => {
if (adapter && adapter.name !== SolanaMobileWalletAdapterWalletName) {
if (adapter) {
// If any error happens while connecting, unset the adapter.
changeWallet(null);
}
Expand Down