@openocean.finance/wallet-management
Version:
Openocean Wallet Management solution.
44 lines • 1.78 kB
JavaScript
import { useEffect } from 'react';
import { ChainId, ChainType } from '@openocean.finance/widget-sdk';
import { useLastConnectedAccount, useNearAccountStore, } from '../../hooks/useAccount.js';
// @ts-ignore - runtime implementation is provided by the host app (widget package)
import { useWalletSelector } from '@near-wallet-selector/react-hook';
/**
* 在应用挂载时,从 Near wallet-selector 中恢复已登录的 Near 账户,
* 这样刷新页面后 Near 账户也会出现在 useAccount / 钱包菜单中。
*/
export const NearAccountHydrator = () => {
const { setNearAccount } = useNearAccountStore();
const { setLastConnectedAccount } = useLastConnectedAccount();
const nearWallet = useWalletSelector();
useEffect(() => {
if (!nearWallet)
return;
// 当前已登录的 Near 账户(如果有)
const accountId = nearWallet.signedAccountId;
if (!accountId)
return;
const connectorId = nearWallet.id || 'near-wallet';
const connectorName = nearWallet.metadata?.name || 'Near Wallet';
setNearAccount({
address: accountId,
chainId: ChainId.NEAR,
chainType: ChainType.NVM,
connector: {
id: connectorId,
name: connectorName,
},
isConnected: true,
isConnecting: false,
isReconnecting: false,
isDisconnected: false,
status: 'connected',
});
setLastConnectedAccount({
id: connectorId,
name: connectorName,
});
}, [nearWallet?.signedAccountId, nearWallet, setNearAccount, setLastConnectedAccount]);
return null;
};
//# sourceMappingURL=NearAccountHydrator.js.map