1"use client";
2
3import type { HTMLAttributes, ReactNode } from "react";
4import { createContext, memo, useContext, useMemo } from "react";
5import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
6import { cn } from "@/lib/utils";
7
8type TickerContextValue = {
9 formatter: Intl.NumberFormat;
10};
11
12const DEFAULT_CURRENCY = "USD";
13const DEFAULT_LOCALE = "en-US";
14
15const defaultFormatter = new Intl.NumberFormat(DEFAULT_LOCALE, {
16 style: "currency",
17 currency: DEFAULT_CURRENCY,
18 minimumFractionDigits: 2,
19 maximumFractionDigits: 2,
20});
21
22const TickerContext = createContext<TickerContextValue>({
23 formatter: defaultFormatter,
24});
25
26export const useTickerContext = () => useContext(TickerContext);
27
28export type TickerProps = HTMLAttributes<HTMLButtonElement> & {
29 currency?: string;
30 locale?: string;
31};
32
33export const Ticker = memo(
34 ({
35 children,
36 className,
37 currency = DEFAULT_CURRENCY,
38 locale = DEFAULT_LOCALE,
39 ...props
40 }: TickerProps & { children: ReactNode }) => {
41 const formatter = useMemo(() => {
42 try {
43 return new Intl.NumberFormat(locale, {
44 style: "currency",
45 currency: currency.toUpperCase(),
46 minimumFractionDigits: 2,
47 maximumFractionDigits: 2,
48 });
49 } catch {
50 return defaultFormatter;
51 }
52 }, [currency, locale]);
53
54 return (
55 <TickerContext.Provider value={{ formatter }}>
56 <button
57 className={cn(
58 "inline-flex items-center gap-1.5 whitespace-nowrap align-middle",
59 className
60 )}
61 type="button"
62 {...props}
63 >
64 {children}
65 </button>
66 </TickerContext.Provider>
67 );
68 }
69);
70Ticker.displayName = "Ticker";
71
72export type TickerIconProps = HTMLAttributes<HTMLImageElement> & {
73 src?: string;
74 symbol?: string;
75 asChild?: boolean;
76};
77
78export const TickerIcon = memo(
79 ({
80 src,
81 symbol,
82 className,
83 asChild,
84 children,
85 ...props
86 }: TickerIconProps) => {
87 if (asChild) {
88 return (
89 <div
90 className={cn(
91 "overflow-hidden rounded-full border border-border bg-muted",
92 className
93 )}
94 >
95 {children}
96 </div>
97 );
98 }
99
100 return (
101 <Avatar className={cn("size-7 border border-border bg-muted", className)}>
102 <AvatarImage src={src} {...props} />
103 <AvatarFallback className="font-semibold text-muted-foreground text-sm">
104 {symbol?.slice(0, 2).toUpperCase()}
105 </AvatarFallback>
106 </Avatar>
107 );
108 }
109);
110TickerIcon.displayName = "TickerIcon";
111
112export type TickerSymbolProps = HTMLAttributes<HTMLSpanElement> & {
113 symbol: string;
114};
115
116export const TickerSymbol = memo(
117 ({ symbol, className, ...props }: TickerSymbolProps) => (
118 <span className={cn("font-medium", className)} {...props}>
119 {symbol.toUpperCase()}
120 </span>
121 )
122);
123TickerSymbol.displayName = "TickerSymbol";
124
125export type TickerPriceProps = HTMLAttributes<HTMLSpanElement> & {
126 price: number;
127};
128
129export const TickerPrice = memo(
130 ({ price, className, ...props }: TickerPriceProps) => {
131 const context = useTickerContext();
132
133 const formattedPrice = useMemo(
134 () => context.formatter.format(price),
135 [price, context]
136 );
137
138 return (
139 <span className={cn("text-muted-foreground", className)} {...props}>
140 {formattedPrice}
141 </span>
142 );
143 }
144);
145TickerPrice.displayName = "TickerPrice";
146
147export type TickerPriceChangeProps = HTMLAttributes<HTMLSpanElement> & {
148 change: number;
149 isPercent?: boolean;
150};
151
152export const TickerPriceChange = memo(
153 ({ change, isPercent, className, ...props }: TickerPriceChangeProps) => {
154 const isPositiveChange = useMemo(() => change >= 0, [change]);
155 const context = useTickerContext();
156
157 const changeFormatted = useMemo(() => {
158 if (isPercent) {
159 return `${change.toFixed(2)}%`;
160 }
161 return context.formatter.format(change);
162 }, [change, isPercent, context]);
163
164 return (
165 <span
166 className={cn(
167 "flex items-center gap-0.5",
168 isPositiveChange
169 ? "text-green-600 dark:text-green-500"
170 : "text-red-600 dark:text-red-500",
171 className
172 )}
173 {...props}
174 >
175 <svg
176 aria-labelledby="ticker-change-icon-title"
177 className={isPositiveChange ? "" : "rotate-180"}
178 fill="currentColor"
179 height="12"
180 role="img"
181 viewBox="0 0 24 24"
182 width="12"
183 xmlns="http://www.w3.org/2000/svg"
184 >
185 <title id="ticker-change-icon-title">
186 {isPositiveChange ? "Up icon" : "Down icon"}
187 </title>
188 <path d="M24 22h-24l12-20z" />
189 </svg>
190 {changeFormatted}
191 </span>
192 );
193 }
194);
195TickerPriceChange.displayName = "TickerPriceChange";
196