@dynamic-labs/sdk-react-core
Version:
A React SDK for implementing wallet web3 authentication and authorization to your website.
64 lines (61 loc) • 3.14 kB
JavaScript
'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import '@dynamic-labs/utils';
import '@dynamic-labs/sdk-api-core';
import '../../../config/ApiEndpoint.js';
import '../../../store/state/projectSettings/projectSettings.js';
import '../../constants/values.js';
import '@dynamic-labs/multi-wallet';
import '../../../shared/logger.js';
import '../../constants/colors.js';
import 'react-international-phone';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '@dynamic-labs/wallet-book';
import '../../../shared/consts/index.js';
import '../../../store/state/nonce/nonce.js';
import { getEnvironmentId } from '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import { setPrimaryWalletId } from '../../../store/state/primaryWalletId/primaryWalletId.js';
import { getUser } from '../../../store/state/user/user.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../locale/locale.js';
import { storeSelectedWallet } from '../../../data/api/wallets/wallets.js';
const updatePrimaryWalletId = (walletId) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
setPrimaryWalletId(walletId);
const user = getUser();
if (user) {
const environmentId = getEnvironmentId();
// We will compare the value of this wallet's lastSelectedAt to the latest
// selected credential's lastSelectedAt. If it's the same, we can skip the
// /select endpoint. This is to avoid unnecessary calls to the /select
// endpoint, which causes an update to the user's JWT.
const currentWalletLastSelectedAt = (_a = user.verifiedCredentials.find((credential) => credential.id === walletId)) === null || _a === void 0 ? void 0 : _a.lastSelectedAt;
if (currentWalletLastSelectedAt) {
const lastSelectedWalletCredential = user.verifiedCredentials.reduce((mostRecentSelectedCredential, credentialToCheck) => {
if (credentialToCheck.format !== 'blockchain' ||
!credentialToCheck.lastSelectedAt) {
return mostRecentSelectedCredential;
}
if (!mostRecentSelectedCredential ||
!mostRecentSelectedCredential.lastSelectedAt) {
return credentialToCheck;
}
return credentialToCheck.lastSelectedAt >
mostRecentSelectedCredential.lastSelectedAt
? credentialToCheck
: mostRecentSelectedCredential;
}, null);
if ((lastSelectedWalletCredential === null || lastSelectedWalletCredential === void 0 ? void 0 : lastSelectedWalletCredential.lastSelectedAt) &&
lastSelectedWalletCredential.lastSelectedAt.getTime() ===
currentWalletLastSelectedAt.getTime()) {
return;
}
}
yield storeSelectedWallet({ environmentId, walletId });
}
});
export { updatePrimaryWalletId };