|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + Card, |
| 5 | + CardContent, |
| 6 | + CardDescription, |
| 7 | + CardHeader, |
| 8 | + CardTitle, |
| 9 | +} from '@/components/ui/card'; |
| 10 | +import { Skeleton } from '@/components/ui/skeleton'; |
| 11 | +import { Button } from '@/components/ui/button'; |
| 12 | +import { api } from '@/trpc/client'; |
| 13 | +import { toast } from 'sonner'; |
| 14 | +import { |
| 15 | + Table, |
| 16 | + TableBody, |
| 17 | + TableCell, |
| 18 | + TableHead, |
| 19 | + TableHeader, |
| 20 | + TableRow, |
| 21 | +} from '@/components/ui/table'; |
| 22 | +import { useState } from 'react'; |
| 23 | + |
| 24 | +export function AppX402ProfitTotal() { |
| 25 | + const [processingAppIds, setProcessingAppIds] = useState<Set<string>>( |
| 26 | + new Set() |
| 27 | + ); |
| 28 | + const [isSendingAll, setIsSendingAll] = useState(false); |
| 29 | + const utils = api.useUtils(); |
| 30 | + |
| 31 | + const { data: totalProfit, isLoading: isTotalLoading } = |
| 32 | + api.admin.wallet.getX402AppProfit.useQuery(); |
| 33 | + |
| 34 | + const { data: appBreakdown, isLoading: isBreakdownLoading } = |
| 35 | + api.admin.wallet.getX402AppProfitByApp.useQuery(); |
| 36 | + |
| 37 | + const payoutMutation = api.admin.wallet.payoutX402AppProfit.useMutation({ |
| 38 | + onSuccess: data => { |
| 39 | + toast.success('Payout successful!', { |
| 40 | + description: `Sent to ECHO_PAYOUTS. Tx: ${data.userOpHash.slice(0, 10)}...`, |
| 41 | + }); |
| 42 | + void utils.admin.wallet.getX402AppProfit.invalidate(); |
| 43 | + void utils.admin.wallet.getX402AppProfitByApp.invalidate(); |
| 44 | + }, |
| 45 | + onError: error => { |
| 46 | + toast.error('Payout failed', { |
| 47 | + description: error.message, |
| 48 | + }); |
| 49 | + }, |
| 50 | + onSettled: (data, error, variables) => { |
| 51 | + setProcessingAppIds(prev => { |
| 52 | + const next = new Set(prev); |
| 53 | + next.delete(variables.appId); |
| 54 | + return next; |
| 55 | + }); |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const handlePayout = (appId: string, amount: number) => { |
| 60 | + if (amount <= 0) { |
| 61 | + toast.error('Invalid amount', { |
| 62 | + description: 'Amount must be greater than 0', |
| 63 | + }); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + setProcessingAppIds(prev => new Set(prev).add(appId)); |
| 68 | + payoutMutation.mutate({ appId, amount }); |
| 69 | + }; |
| 70 | + |
| 71 | + const handleSendAll = async () => { |
| 72 | + if (!appBreakdown || appBreakdown.length === 0) { |
| 73 | + toast.error('No apps to payout'); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const appsWithProfit = appBreakdown.filter(app => app.remainingProfit > 0); |
| 78 | + |
| 79 | + if (appsWithProfit.length === 0) { |
| 80 | + toast.error('No apps with remaining profit'); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + setIsSendingAll(true); |
| 85 | + |
| 86 | + for (const app of appsWithProfit) { |
| 87 | + setProcessingAppIds(prev => new Set(prev).add(app.appId)); |
| 88 | + payoutMutation.mutate({ |
| 89 | + appId: app.appId, |
| 90 | + amount: app.remainingProfit, |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + setIsSendingAll(false); |
| 95 | + }; |
| 96 | + |
| 97 | + return ( |
| 98 | + <div className="space-y-6"> |
| 99 | + <Card> |
| 100 | + <CardHeader> |
| 101 | + <CardTitle>App X402 Profit Total</CardTitle> |
| 102 | + <CardDescription> |
| 103 | + Total unclaimed profit generated by apps from X402 transactions |
| 104 | + </CardDescription> |
| 105 | + </CardHeader> |
| 106 | + |
| 107 | + <CardContent className="space-y-6"> |
| 108 | + <div> |
| 109 | + <h3 className="text-sm font-medium text-muted-foreground mb-2"> |
| 110 | + Total App Profit |
| 111 | + </h3> |
| 112 | + {isTotalLoading ? ( |
| 113 | + <Skeleton className="h-10 w-32" /> |
| 114 | + ) : ( |
| 115 | + <div className="text-3xl font-bold text-green-600"> |
| 116 | + ${(totalProfit ?? 0).toFixed(6)} |
| 117 | + </div> |
| 118 | + )} |
| 119 | + <p className="text-xs text-muted-foreground mt-1"> |
| 120 | + Available for payout to applications |
| 121 | + </p> |
| 122 | + </div> |
| 123 | + |
| 124 | + <p className="text-sm text-muted-foreground"> |
| 125 | + The App Profit represents the sum of all appProfit from X402 |
| 126 | + transactions minus any payouts already made to applications. |
| 127 | + </p> |
| 128 | + </CardContent> |
| 129 | + </Card> |
| 130 | + |
| 131 | + <Card> |
| 132 | + <CardHeader> |
| 133 | + <div className="flex items-center justify-between"> |
| 134 | + <div> |
| 135 | + <CardTitle>Profit Breakdown by Application</CardTitle> |
| 136 | + <CardDescription> |
| 137 | + X402 profit generated by each application |
| 138 | + </CardDescription> |
| 139 | + </div> |
| 140 | + {appBreakdown && |
| 141 | + appBreakdown.some(app => app.remainingProfit > 0) && ( |
| 142 | + <Button |
| 143 | + onClick={handleSendAll} |
| 144 | + disabled={isSendingAll || payoutMutation.isPending} |
| 145 | + variant="default" |
| 146 | + > |
| 147 | + {isSendingAll ? 'Processing...' : 'Send All'} |
| 148 | + </Button> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + </CardHeader> |
| 152 | + <CardContent> |
| 153 | + {isBreakdownLoading ? ( |
| 154 | + <div className="space-y-2"> |
| 155 | + <Skeleton className="h-12 w-full" /> |
| 156 | + <Skeleton className="h-12 w-full" /> |
| 157 | + <Skeleton className="h-12 w-full" /> |
| 158 | + </div> |
| 159 | + ) : !appBreakdown || appBreakdown.length === 0 ? ( |
| 160 | + <p className="text-sm text-muted-foreground text-center py-8"> |
| 161 | + No app profit data available |
| 162 | + </p> |
| 163 | + ) : ( |
| 164 | + <div className="overflow-x-auto"> |
| 165 | + <Table> |
| 166 | + <TableHeader> |
| 167 | + <TableRow> |
| 168 | + <TableHead>Application</TableHead> |
| 169 | + <TableHead className="text-right">Total Profit</TableHead> |
| 170 | + <TableHead className="text-right">Total Payouts</TableHead> |
| 171 | + <TableHead className="text-right">Remaining</TableHead> |
| 172 | + <TableHead className="text-right">Actions</TableHead> |
| 173 | + </TableRow> |
| 174 | + </TableHeader> |
| 175 | + <TableBody> |
| 176 | + {appBreakdown.map(app => ( |
| 177 | + <TableRow key={app.appId}> |
| 178 | + <TableCell className="font-medium"> |
| 179 | + {app.appName} |
| 180 | + </TableCell> |
| 181 | + <TableCell className="text-right font-mono text-sm"> |
| 182 | + ${app.totalProfit.toFixed(6)} |
| 183 | + </TableCell> |
| 184 | + <TableCell className="text-right font-mono text-sm"> |
| 185 | + ${app.totalPayouts.toFixed(6)} |
| 186 | + </TableCell> |
| 187 | + <TableCell className="text-right font-mono text-sm"> |
| 188 | + <span |
| 189 | + className={ |
| 190 | + app.remainingProfit > 0 |
| 191 | + ? 'text-green-600 font-semibold' |
| 192 | + : '' |
| 193 | + } |
| 194 | + > |
| 195 | + ${app.remainingProfit.toFixed(6)} |
| 196 | + </span> |
| 197 | + </TableCell> |
| 198 | + <TableCell className="text-right"> |
| 199 | + <Button |
| 200 | + onClick={() => |
| 201 | + handlePayout(app.appId, app.remainingProfit) |
| 202 | + } |
| 203 | + disabled={ |
| 204 | + app.remainingProfit <= 0 || |
| 205 | + processingAppIds.has(app.appId) |
| 206 | + } |
| 207 | + size="sm" |
| 208 | + variant="outline" |
| 209 | + > |
| 210 | + {processingAppIds.has(app.appId) |
| 211 | + ? 'Sending...' |
| 212 | + : 'Send'} |
| 213 | + </Button> |
| 214 | + </TableCell> |
| 215 | + </TableRow> |
| 216 | + ))} |
| 217 | + </TableBody> |
| 218 | + </Table> |
| 219 | + </div> |
| 220 | + )} |
| 221 | + </CardContent> |
| 222 | + </Card> |
| 223 | + </div> |
| 224 | + ); |
| 225 | +} |
0 commit comments