import useSWR from "swr"; import { useState } from "react"; import { useTranslation } from "next-i18next"; import classNames from "classnames"; import Widget from "../widget"; import Block from "../block"; import Dropdown from "components/services/dropdown"; import { formatProxyUrl } from "utils/api-helpers"; export default function CoinMarketCap({ service }) { const { t } = useTranslation(); const dateRangeOptions = [ { label: t("coinmarketcap.1hour"), value: "1h" }, { label: t("coinmarketcap.1day"), value: "24h" }, { label: t("coinmarketcap.7days"), value: "7d" }, { label: t("coinmarketcap.30days"), value: "30d" }, ]; const [dateRange, setDateRange] = useState(dateRangeOptions[0].value); const config = service.widget; const currencyCode = config.currency ?? "USD"; const { symbols } = config; const { data: statsData, error: statsError } = useSWR( formatProxyUrl(config, `v1/cryptocurrency/quotes/latest?symbol=${symbols.join(",")}&convert=${currencyCode}`) ); if (!symbols || symbols.length === 0) { return ( ); } if (statsError) { return ; } if (!statsData || !dateRange) { return ( ); } const { data } = statsData; return (
{symbols.map((symbol) => (
{data[symbol].name}
{t("common.number", { value: data[symbol].quote[currencyCode].price, style: "currency", currency: currencyCode, })}
0 ? "text-emerald-300" : "text-rose-300" }`} > {data[symbol].quote[currencyCode][`percent_change_${dateRange}`].toFixed(2)}%
))}
); }