|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { useClaimableAmount } from "@/hooks/useClaimableAmount"; |
| 5 | + |
| 6 | +interface ClaimableAmountDisplayProps { |
| 7 | + streamId: string; |
| 8 | + initialAmount: number; // The starting claimable amount |
| 9 | + ratePerSecond: number; // Rate in tokens/sec |
| 10 | + isPaused?: boolean; |
| 11 | + isActive: boolean; |
| 12 | + label?: string; |
| 13 | + pausedAt?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export default function ClaimableAmountDisplay({ |
| 17 | + streamId, |
| 18 | + initialAmount, |
| 19 | + ratePerSecond, |
| 20 | + isPaused = false, |
| 21 | + isActive, |
| 22 | + label = "Available to withdraw", |
| 23 | + pausedAt, |
| 24 | +}: ClaimableAmountDisplayProps) { |
| 25 | + const { claimable, tick } = useClaimableAmount({ |
| 26 | + streamId, |
| 27 | + initialClaimable: initialAmount, |
| 28 | + ratePerSecond, |
| 29 | + isActive, |
| 30 | + isPaused, |
| 31 | + }); |
| 32 | + |
| 33 | + const [highlight, setHighlight] = useState(false); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + if (tick > 0 && isActive && !isPaused) { |
| 37 | + setHighlight(true); |
| 38 | + const timer = setTimeout(() => setHighlight(false), 200); |
| 39 | + return () => clearTimeout(timer); |
| 40 | + } |
| 41 | + }, [tick, isActive, isPaused]); |
| 42 | + |
| 43 | + const formatPausedTime = (pausedAtStr: string | undefined): string => { |
| 44 | + if (!pausedAtStr) return ""; |
| 45 | + try { |
| 46 | + // Stream pausedAt might be in seconds or a date string |
| 47 | + const parsed = parseInt(pausedAtStr); |
| 48 | + const isSeconds = parsed.toString() === pausedAtStr && parsed < 10000000000; |
| 49 | + const pausedDate = isSeconds ? new Date(parsed * 1000) : new Date(pausedAtStr); |
| 50 | + |
| 51 | + const now = new Date(); |
| 52 | + const diffMs = now.getTime() - pausedDate.getTime(); |
| 53 | + const diffMins = Math.floor(diffMs / 60000); |
| 54 | + const diffHours = Math.floor(diffMs / 3600000); |
| 55 | + const diffDays = Math.floor(diffMs / 86400000); |
| 56 | + |
| 57 | + if (diffDays > 0) return `Paused ${diffDays} day${diffDays > 1 ? 's' : ''} ago`; |
| 58 | + if (diffHours > 0) return `Paused ${diffHours} hour${diffHours > 1 ? 's' : ''} ago`; |
| 59 | + if (diffMins > 0) return `Paused ${diffMins} minute${diffMins > 1 ? 's' : ''} ago`; |
| 60 | + return "Paused now"; |
| 61 | + } catch { |
| 62 | + return "Paused"; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <div style={{ display: "flex", alignItems: "center", gap: "0.75rem" }}> |
| 68 | + <span |
| 69 | + style={{ |
| 70 | + position: "relative", |
| 71 | + display: "inline-flex", |
| 72 | + width: "0.75rem", |
| 73 | + height: "0.75rem", |
| 74 | + }} |
| 75 | + > |
| 76 | + <span |
| 77 | + style={{ |
| 78 | + position: "absolute", |
| 79 | + inset: 0, |
| 80 | + borderRadius: "999px", |
| 81 | + background: isPaused ? "#ef4444" : "#10b981", |
| 82 | + opacity: isPaused ? 0.5 : 0.75, |
| 83 | + animation: isPaused ? "none" : "pulse-slow 4s cubic-bezier(0.4, 0, 0.6, 1) infinite", |
| 84 | + }} |
| 85 | + /> |
| 86 | + <span |
| 87 | + style={{ |
| 88 | + position: "relative", |
| 89 | + display: "inline-flex", |
| 90 | + width: "0.75rem", |
| 91 | + height: "0.75rem", |
| 92 | + borderRadius: "999px", |
| 93 | + background: isPaused ? "#ef4444" : "#10b981", |
| 94 | + }} |
| 95 | + /> |
| 96 | + </span> |
| 97 | + |
| 98 | + <p style={{ margin: 0, fontSize: "0.92rem", color: "var(--text-muted)" }}> |
| 99 | + {isPaused ? ( |
| 100 | + formatPausedTime(pausedAt) |
| 101 | + ) : ( |
| 102 | + <> |
| 103 | + {label}:{" "} |
| 104 | + <strong |
| 105 | + style={{ |
| 106 | + fontSize: "1rem", |
| 107 | + color: highlight ? "#10b981" : "var(--text-main)", |
| 108 | + transition: "color 0.2s ease", |
| 109 | + fontVariantNumeric: "tabular-nums", |
| 110 | + }} |
| 111 | + > |
| 112 | + {claimable.toFixed(7)} |
| 113 | + </strong> |
| 114 | + </> |
| 115 | + )} |
| 116 | + </p> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +} |
0 commit comments