@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
82 lines • 3.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRecipientSearchState = useRecipientSearchState;
const errors_1 = require("@ledgerhq/errors");
const react_1 = require("react");
function useRecipientSearchState({ searchValue, result, isLoading, recipientSupportsDomain, }) {
const hasSearchValue = searchValue.length > 0;
const showSearchResults = hasSearchValue && !isLoading;
const isSanctioned = result.status === "sanctioned";
const isAddressComplete = (0, react_1.useMemo)(() => {
return (result.status === "valid" ||
result.status === "ens_resolved" ||
result.status === "sanctioned");
}, [result.status]);
const hasAnyMatches = (result.matchedAccounts && result.matchedAccounts.length > 0) ||
!!result.matchedRecentAddress ||
!!result.ensName ||
result.isLedgerAccount ||
isSanctioned;
const showSanctionedBanner = isSanctioned && hasSearchValue;
const bridgeRecipientError = result.bridgeErrors?.recipient;
const bridgeRecipientWarning = result.bridgeWarnings?.recipient;
const bridgeSenderError = result.bridgeErrors?.sender;
const isSelfTransferError = bridgeRecipientError instanceof errors_1.InvalidAddressBecauseDestinationIsAlsoSource;
const isBridgeInvalidAddress = bridgeRecipientError instanceof errors_1.InvalidAddress && !isSelfTransferError;
const hasBridgeRecipientError = !!bridgeRecipientError && !isBridgeInvalidAddress && !showSanctionedBanner;
const hasBridgeRecipientWarning = !!bridgeRecipientWarning;
const showMatchedAddress = showSearchResults &&
(hasAnyMatches ||
(result.status === "valid" && !result.error && !isBridgeInvalidAddress) ||
(isAddressComplete && (hasBridgeRecipientError || hasBridgeRecipientWarning))) &&
(result.status === "valid" ||
(recipientSupportsDomain && result.status === "ens_resolved") ||
result.isLedgerAccount ||
!!result.matchedRecentAddress ||
isSanctioned ||
(isAddressComplete && (hasBridgeRecipientError || hasBridgeRecipientWarning)));
const showAddressValidationError = showSearchResults &&
!showSanctionedBanner &&
!hasAnyMatches &&
(!!result.error || isBridgeInvalidAddress);
const addressValidationErrorType = (0, react_1.useMemo)(() => {
if (result.error)
return result.error;
if (isBridgeInvalidAddress) {
if (searchValue.includes(".")) {
return "wallet_not_exist";
}
return "incorrect_format";
}
return null;
}, [result.error, isBridgeInvalidAddress, searchValue]);
const showBridgeRecipientError = showSearchResults && hasBridgeRecipientError && !showAddressValidationError;
const showBridgeRecipientWarning = showSearchResults &&
!!bridgeRecipientWarning &&
!showBridgeRecipientError &&
!showAddressValidationError;
const showBridgeSenderError = showSearchResults && !!bridgeSenderError;
const showEmptyState = showSearchResults &&
(!isAddressComplete || !hasAnyMatches) &&
!showMatchedAddress &&
!showSanctionedBanner &&
!showAddressValidationError &&
!showBridgeRecipientError;
return {
showSearchResults,
showMatchedAddress,
showAddressValidationError,
showEmptyState,
showBridgeSenderError,
showSanctionedBanner,
showBridgeRecipientError,
showBridgeRecipientWarning,
isSanctioned,
isAddressComplete,
addressValidationErrorType,
bridgeRecipientError,
bridgeRecipientWarning,
bridgeSenderError,
};
}
//# sourceMappingURL=useRecipientSearchState.js.map