import * as React from 'react'; import { useState } from 'react' import { useThemeHooks } from '@redocly/theme/core/hooks'; import { clsx } from 'clsx' import { type Transaction, type Wallet } from 'xrpl' import { SubmitConstData, submitAndUpdateUI, canSendTransaction } from '../utils'; export interface TransactionButtonProps { submitConstData: SubmitConstData, connectionReady: boolean, transaction: Transaction, sendingWallet: Wallet | undefined id: string, // Used to set all ids within component content: { buttonText: string, units: string, // Displays after the input number longerDescription: React.JSX.Element // JSX allows for embedding links within the longer description buttonTitle?: string // Only used while loading bar is activated }, inputSettings?: { defaultValue: number, // Should NOT be a dynamic number setInputValue: React.Dispatch>, min: number, max: number, }, loadingBar?: { id: string, widthPercent: number, description: string, defaultOn: boolean, }, checkBox?: { setCheckValue: React.Dispatch>, defaultValue: boolean, description: string, } customOnClick?: Function } function shouldDisableButton( connectionReady: boolean, sendingWallet: Wallet | undefined, waitingForTransaction: boolean, loadingBar?: { widthPercent: number defaultOn: boolean } ): boolean { const loadingBarInProgress = !!loadingBar && loadingBar.widthPercent < 100 && (loadingBar.defaultOn || loadingBar.widthPercent > 0) return !canSendTransaction(connectionReady, sendingWallet?.address) || waitingForTransaction || loadingBarInProgress } export function TransactionButton({ id, submitConstData, connectionReady, transaction, sendingWallet, content, inputSettings, loadingBar, checkBox, customOnClick }: TransactionButtonProps ) { const { useTranslate } = useThemeHooks() const { translate } = useTranslate() const [waitingForTransaction, setWaitingForTransaction] = useState(false) const loadingBarComplete = (loadingBar?.widthPercent ?? 0) >= 100 const loadingBarInProgress = !!loadingBar && loadingBar.widthPercent > 0 && loadingBar.widthPercent < 100 const showLoadingStatus = !!loadingBar && !loadingBarComplete && (loadingBar.defaultOn || loadingBar.widthPercent > 0) return (
{/* Status + optional progress: Partial Payments setup and EscrowFinish wait */} {showLoadingStatus && loadingBar &&
{loadingBarInProgress ? (
{translate(loadingBar.description)}
) : ( {translate(loadingBar.description)} )}
}
{/* Loading icon for when transaction is being submitted */}
{translate("(loading)")}
{inputSettings && ) => { // Enforce min / max values let { value, min, max } = event.target; const newValue = Math.max(Number(min), Math.min(Number(max), Number(value))); // Share the value so other logic can update based on it inputSettings?.setInputValue(newValue) } } />} {inputSettings &&
{translate(content.units)}
} {/* Used for Escrow */} {checkBox && ( ) => checkBox.setCheckValue(event.target.checked)} /> ) }
{content.longerDescription}

) }