|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useEffect } from 'react'; |
| 4 | +import { TransactionBuilderProvider } from './core/TransactionBuilderProvider'; |
| 5 | +import { useTransactionBuilder } from './hooks/useTransactionBuilder'; |
| 6 | +import { TransactionFlowController } from './core/TransactionFlowController'; |
| 7 | +import { TransactionType, BuildData } from './core/types'; |
| 8 | + |
| 9 | +// Import builders |
| 10 | +import { SendTokenBuilder, SendTokenBuilderLogic } from './builders/SendTokenBuilder'; |
| 11 | +import { CustomContractBuilder, CustomContractBuilderLogic } from './builders/CustomContractBuilder'; |
| 12 | + |
| 13 | +// Import preview and confirm components |
| 14 | +import { TransactionPreviewPanel } from './preview/TransactionPreviewPanel'; |
| 15 | +import { TransactionConfirmation } from './confirm/TransactionConfirmation'; |
| 16 | + |
| 17 | +// Import registry and register components |
| 18 | +import { ComponentRegistry, BuilderRegistry } from './builders/BuilderRegistry'; |
| 19 | + |
| 20 | +// Register all available builders (both logic and components) |
| 21 | +BuilderRegistry.register('send', () => new SendTokenBuilderLogic()); |
| 22 | +BuilderRegistry.register('custom', () => new CustomContractBuilderLogic()); |
| 23 | + |
| 24 | +ComponentRegistry.register('send', SendTokenBuilder); |
| 25 | +ComponentRegistry.register('custom', CustomContractBuilder); |
| 26 | + |
| 27 | +interface TransactionProcessV2Props { |
| 28 | + // For new transactions |
| 29 | + initialType?: TransactionType; |
| 30 | + |
| 31 | + // For pending transactions |
| 32 | + pendingTransactionId?: string; |
| 33 | + pendingTransactionData?: Partial<BuildData>; |
| 34 | + |
| 35 | + // Callbacks |
| 36 | + onComplete?: (transactionHash: string) => void; |
| 37 | + onCancel?: () => void; |
| 38 | +} |
| 39 | + |
| 40 | +function TransactionProcessV2Content() { |
| 41 | + const { state, startBuilding, proceedToPreview } = useTransactionBuilder(); |
| 42 | + |
| 43 | + const renderBuildStep = () => { |
| 44 | + if (!state.transactionType) { |
| 45 | + // Show transaction type selector |
| 46 | + return ( |
| 47 | + <div className='space-y-6'> |
| 48 | + <div className='grid gap-4 md:grid-cols-2'> |
| 49 | + <div |
| 50 | + className='flex flex-col items-center gap-3 p-6 border rounded-lg hover:border-primary/50 hover:text-primary transition-colors cursor-pointer' |
| 51 | + onClick={() => startBuilding('send')} |
| 52 | + > |
| 53 | + <div className='h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center'> |
| 54 | + <svg className='h-6 w-6' fill='none' stroke='currentColor' viewBox='0 0 24 24'> |
| 55 | + <path |
| 56 | + strokeLinecap='round' |
| 57 | + strokeLinejoin='round' |
| 58 | + strokeWidth={2} |
| 59 | + d='M12 19l9 2-9-18-9 18 9-2zm0 0v-8' |
| 60 | + /> |
| 61 | + </svg> |
| 62 | + </div> |
| 63 | + <div className='text-center'> |
| 64 | + <div className='font-medium text-lg'>Send Token</div> |
| 65 | + <div className='text-sm text-muted-foreground'>Transfer ETH or tokens to another address</div> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div |
| 70 | + className='flex flex-col items-center gap-3 p-6 border rounded-lg hover:border-primary/50 hover:text-primary transition-colors cursor-pointer' |
| 71 | + onClick={() => startBuilding('custom')} |
| 72 | + > |
| 73 | + <div className='h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center'> |
| 74 | + <svg className='h-6 w-6' fill='none' stroke='currentColor' viewBox='0 0 24 24'> |
| 75 | + <path |
| 76 | + strokeLinecap='round' |
| 77 | + strokeLinejoin='round' |
| 78 | + strokeWidth={2} |
| 79 | + d='M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4' |
| 80 | + /> |
| 81 | + </svg> |
| 82 | + </div> |
| 83 | + <div className='text-center'> |
| 84 | + <div className='font-medium text-lg'>Custom Transaction</div> |
| 85 | + <div className='text-sm text-muted-foreground'>Build custom smart contract interactions</div> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // Render the appropriate builder component |
| 94 | + try { |
| 95 | + const BuilderComponent = ComponentRegistry.getComponent(state.transactionType); |
| 96 | + return ( |
| 97 | + <BuilderComponent |
| 98 | + onComplete={async (data: BuildData) => { |
| 99 | + await proceedToPreview(data); |
| 100 | + }} |
| 101 | + initialData={state.buildData} |
| 102 | + errors={state.errors} |
| 103 | + isLoading={state.isLoading} |
| 104 | + /> |
| 105 | + ); |
| 106 | + } catch (error) { |
| 107 | + return ( |
| 108 | + <div className='text-center py-8'> |
| 109 | + <div className='text-destructive'>Error: Unknown transaction type "{state.transactionType}"</div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + const renderPreviewStep = () => { |
| 116 | + if (!state.buildData) { |
| 117 | + return ( |
| 118 | + <div className='text-center py-8'> |
| 119 | + <div className='text-destructive'>No transaction data available for preview</div> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + return <TransactionPreviewPanel transactionData={state.buildData} />; |
| 125 | + }; |
| 126 | + |
| 127 | + const renderConfirmStep = () => { |
| 128 | + if (!state.buildData) { |
| 129 | + return ( |
| 130 | + <div className='text-center py-8'> |
| 131 | + <div className='text-destructive'>No transaction data available for confirmation</div> |
| 132 | + </div> |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + return <TransactionConfirmation transactionData={state.buildData} />; |
| 137 | + }; |
| 138 | + |
| 139 | + const renderStepContent = () => { |
| 140 | + switch (state.currentStep) { |
| 141 | + case 'build': |
| 142 | + return renderBuildStep(); |
| 143 | + case 'preview': |
| 144 | + return renderPreviewStep(); |
| 145 | + case 'confirm': |
| 146 | + return renderConfirmStep(); |
| 147 | + default: |
| 148 | + return null; |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + return <TransactionFlowController>{renderStepContent()}</TransactionFlowController>; |
| 153 | +} |
| 154 | + |
| 155 | +export function TransactionProcessV2({ |
| 156 | + initialType, |
| 157 | + pendingTransactionId, |
| 158 | + pendingTransactionData, |
| 159 | + onComplete, |
| 160 | + onCancel, |
| 161 | +}: TransactionProcessV2Props) { |
| 162 | + return ( |
| 163 | + <TransactionBuilderProvider |
| 164 | + initialType={initialType} |
| 165 | + pendingTransactionId={pendingTransactionId} |
| 166 | + pendingTransactionData={pendingTransactionData} |
| 167 | + > |
| 168 | + <TransactionProcessV2Content /> |
| 169 | + </TransactionBuilderProvider> |
| 170 | + ); |
| 171 | +} |
0 commit comments