Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import perfUtils, {
import networkUtils, {
isEnabledNetworksInAllNetworks,
} from '@onekeyhq/shared/src/utils/networkUtils';
import type { IServerNetwork } from '@onekeyhq/shared/types';
import type { INetworkAccount } from '@onekeyhq/shared/types/account';

import ServiceBase from '../ServiceBase';
Expand Down Expand Up @@ -792,6 +793,74 @@ class ServiceAllNetwork extends ServiceBase {
return allNetworksState;
}

@backgroundMethod()
async getEnabledNetworksCompatibleWithWalletId({
walletId,
}: {
walletId: string;
}): Promise<IServerNetwork[]> {
// Mirrors useEnabledNetworksCompatibleWithWalletIdInAllNetworks (kit):
// the mainnet networks the All Networks view actually aggregates for
// this wallet. An empty result means All Networks is a dead end for
// the wallet (e.g. QR wallet with only QR-unsupported networks enabled).
const [{ enabledNetworks, disabledNetworks }, { networks }] =
await Promise.all([
this.getAllNetworksState(),
this.backgroundApi.serviceNetwork.getAllNetworks({
excludeTestNetwork: true,
excludeAllNetworkItem: true,
}),
]);
const enabledNetworkIds = networks
.filter((n) =>
isEnabledNetworksInAllNetworks({
networkId: n.id,
disabledNetworks,
enabledNetworks,
isTestnet: n.isTestnet,
}),
)
.map((n) => n.id);
if (enabledNetworkIds.length === 0) {
// getChainSelectorNetworksCompatibleWithAccountId treats an empty
// networkIds list as "all networks", so return early here.
return [];
}
const { mainnetItems } =
await this.backgroundApi.serviceNetwork.getChainSelectorNetworksCompatibleWithAccountId(
{
walletId,
networkIds: enabledNetworkIds,
},
);
return mainnetItems;
}

@backgroundMethod()
async getAllNetworksFallbackNetworkId({
walletId,
}: {
walletId: string;
}): Promise<string | undefined> {
// Returns undefined when the All Networks view is usable for this wallet
// (at least one enabled network is compatible); otherwise returns the
// first wallet-compatible mainnet as the single-chain fallback.
const compatibleEnabledNetworks =
await this.getEnabledNetworksCompatibleWithWalletId({
walletId,
});
if (compatibleEnabledNetworks.length > 0) {
return undefined;
}
const { mainnetItems } =
await this.backgroundApi.serviceNetwork.getChainSelectorNetworksCompatibleWithAccountId(
{
walletId,
},
);
return mainnetItems?.[0]?.id;
}

@backgroundMethod()
async updateAllNetworksState(params: {
disabledNetworks?: Record<string, boolean>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function AllNetworksManagerTrigger({
const {
enabledNetworksCompatibleWithWalletId,
enabledNetworksWithoutAccount,
isReady: isCompatQueryReady,
run,
} = useEnabledNetworksCompatibleWithWalletIdInAllNetworks({
walletId: compatQueryWalletId,
Expand Down Expand Up @@ -122,12 +123,47 @@ function AllNetworksManagerTrigger({

if (
showSkeleton ||
!enabledNetworksCompatibleWithWalletId ||
enabledNetworksCompatibleWithWalletId.length === 0
!isCompatQueryReady ||
!enabledNetworksCompatibleWithWalletId
) {
return <Stack h={36} />;
}

if (enabledNetworksCompatibleWithWalletId.length === 0) {
// Dead-end escape hatch: none of the enabled All Networks chains is
// compatible with this wallet, so there are no network avatars to show.
// Keep the trigger pressable so the user can still open the network
// selector and switch to a single chain (or enable a compatible one).
return (
<XStack
borderRadius="$2"
hoverStyle={{
bg: '$bgHover',
}}
pressStyle={{
bg: '$bgActive',
}}
focusable
focusVisibleStyle={{
outlineWidth: 2,
outlineColor: '$focusRing',
outlineStyle: 'solid',
}}
userSelect="none"
onPress={handleOnPress}
alignItems="center"
>
<NetworkAvatarBase
logoURI={network?.logoURI ?? ''}
size="$6"
networkName={network?.name}
isAllNetworks={network?.isAllNetworks}
/>
<Icon name="ChevronDownSmallOutline" color="$iconSubdued" size="$5" />
</XStack>
);
}

return (
<XStack
borderRadius="$2"
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/src/hooks/useAllNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,10 @@ function useEnabledNetworksCompatibleWithWalletIdInAllNetworks({
networkInfoMap: result?.networkInfoMap ?? {},
enabledNetworksCompatibleWithWalletId,
enabledNetworksWithoutAccount,
// usePromiseResult resets result to the memoized initResult reference on
// scope (swrKey) change, so identity tells "not resolved for this scope
// yet" apart from a resolved-but-empty compatible set.
isReady: result !== initResult,
run,
};
}
Expand Down
Loading
Loading