forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream-card.tsx
More file actions
207 lines (195 loc) · 7.34 KB
/
Copy pathstream-card.tsx
File metadata and controls
207 lines (195 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"use client";
import { memo } from "react";
import Link from "next/link";
import { ArrowDownLeft, ArrowUpRight } from "lucide-react";
import { useNow } from "@/hooks/use-now";
import { useWallet } from "@/hooks/use-wallet";
import { useTokenPrice, formatUsd } from "@/hooks/use-token-price";
import { useShowUsd } from "@/hooks/use-show-usd";
import {
getStreamProgress,
getStreamStatus,
formatTokenAmount,
shortenAddress,
formatRate,
} from "@/lib/stream-utils";
import { ProgressBar } from "@/components/ui/progress-bar";
import { TokenAmount } from "@/components/ui/token-amount";
import { CountdownTimer } from "@/components/ui/countdown-timer";
import { AccessibleCountdownTimer } from "@/components/ui/accessible-countdown-timer";
import { StreamStatusBadge } from "@/components/streams/stream-status-badge";
import type { StreamData } from "@/types/stream";
// Pick update interval based on a quick pre-check of stream state.
// completed/cancelled streams never change — no interval needed.
// scheduled streams only need minute-level updates for the countdown.
// streaming streams need per-second updates for the live counter.
function getInterval(stream: StreamData): number | null {
const nowSec = Math.floor(Date.now() / 1000);
if (stream.cancelled) return null;
if (nowSec >= Number(stream.endTime)) return null; // completed
if (nowSec < Number(stream.startTime)) return 60000; // scheduled: 1 min
return 1000; // streaming: 1 sec
}
function StreamCardInner({ stream }: { stream: StreamData }) {
const interval = getInterval(stream);
const now = useNow(interval);
const { address } = useWallet();
const { usdPrice } = useTokenPrice(stream.token.symbol);
const [showUsd] = useShowUsd();
const status = getStreamStatus(stream, now);
const progress = getStreamProgress(stream, now);
const withdrawnFrac =
stream.depositedAmount > 0n
? Number((stream.withdrawnAmount * 10000n) / stream.depositedAmount) /
10000
: 0;
const rate = formatRate(
stream.amountPerSecond,
stream.token.decimals,
stream.token.symbol,
);
const isOutgoing = address === stream.sender;
const counterparty = isOutgoing ? stream.recipient : stream.sender;
const direction = isOutgoing ? "Sending" : "Receiving";
const displayAmount = formatTokenAmount(
stream.depositedAmount,
stream.token.decimals,
2,
);
const ariaLabel = `${direction} ${displayAmount} ${stream.token.symbol}, ${status}, ${(progress * 100).toFixed(0)}% unlocked`;
const usdValue =
showUsd && usdPrice !== null
? (Number(stream.depositedAmount) / Math.pow(10, stream.token.decimals)) *
usdPrice
: null;
return (
<Link
href={`/app/stream/${stream.id}`}
className="group block rounded-2xl border border-border bg-card p-5 transition-colors hover:border-primary/40"
aria-label={ariaLabel}
data-testid={`stream-card-${stream.id}`}
>
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-3">
<span
className={
"flex size-9 items-center justify-center rounded-lg " +
(isOutgoing
? "bg-secondary text-muted-foreground"
: "bg-primary/10 text-primary")
}
>
{isOutgoing ? (
<ArrowUpRight className="size-4.5" />
) : (
<ArrowDownLeft className="size-4.5" />
)}
</span>
<div>
<p className="text-sm font-medium">
{stream.metadata?.name ??
(isOutgoing ? "Sending to" : "Receiving from")}
</p>
<p className="font-mono text-xs text-muted-foreground">
{shortenAddress(counterparty, 5)}
</p>
</div>
</div>
<StreamStatusBadge status={status} />
</div>
<div className="mt-5 flex items-end justify-between">
<div>
<p className="text-xs text-muted-foreground">Total</p>
<TokenAmount
amount={stream.depositedAmount}
token={stream.token}
className="text-lg font-semibold"
maxFractionDigits={2}
/>
{usdValue !== null && (
<p className="text-xs text-muted-foreground">
{formatUsd(usdValue)}
</p>
)}
</div>
<div className="text-right">
<p className="text-xs text-muted-foreground">
{status === "scheduled"
? "Starts in"
: status === "completed" || status === "cancelled"
? "Ended"
: "Ends in"}
</p>
<div className="text-sm font-medium">
{status === "scheduled" ? (
<AccessibleCountdownTimer target={stream.startTime} hideButton />
) : status === "completed" || status === "cancelled" ? (
<span className="text-muted-foreground">—</span>
) : (
<AccessibleCountdownTimer target={stream.endTime} hideButton />
)}
</div>
</div>
</div>
{(status === "streaming" || status === "scheduled") && (
<p className="mt-3 text-xs font-mono text-muted-foreground">
{rate.best}
</p>
)}
<div className="mt-4">
<ProgressBar
value={progress}
marker={withdrawnFrac}
indeterminateShimmer={status === "streaming"}
/>
<div className="mt-2 flex justify-between text-xs text-muted-foreground">
<span>{(progress * 100).toFixed(1)}% unlocked</span>
<span>
<TokenAmount
amount={stream.withdrawnAmount}
token={stream.token}
showSymbol={false}
maxFractionDigits={2}
/>{" "}
withdrawn
</span>
</div>
</div>
</Link>
);
}
export const StreamCard = memo(StreamCardInner);
// ─── Skeleton ────────────────────────────────────────────────────────────────
export function StreamCardSkeleton() {
return (
<div className="rounded-2xl border border-border bg-card p-5 animate-pulse">
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-3">
<div className="size-9 rounded-lg bg-muted" />
<div className="space-y-1.5">
<div className="h-3.5 w-20 rounded bg-muted" />
<div className="h-3 w-28 rounded bg-muted" />
</div>
</div>
<div className="h-5 w-16 rounded-full bg-muted" />
</div>
<div className="mt-5 flex items-end justify-between">
<div className="space-y-1.5">
<div className="h-3 w-8 rounded bg-muted" />
<div className="h-6 w-24 rounded bg-muted" />
</div>
<div className="space-y-1.5 text-right">
<div className="h-3 w-12 rounded bg-muted" />
<div className="h-4 w-16 rounded bg-muted" />
</div>
</div>
<div className="mt-4 space-y-2">
<div className="h-2 w-full rounded-full bg-muted" />
<div className="flex justify-between">
<div className="h-3 w-20 rounded bg-muted" />
<div className="h-3 w-24 rounded bg-muted" />
</div>
</div>
</div>
);
}