91 lines
2.7 KiB
React
Raw Normal View History

2022-09-12 01:30:42 -05:00
import useSWR from "swr";
2022-09-14 09:09:11 +03:00
import { useState } from "react";
2022-09-12 01:30:42 -05:00
import { useTranslation } from "react-i18next";
import Widget from "../widget";
import Block from "../block";
2022-09-14 09:09:11 +03:00
import Dropdown from "components/services/dropdown";
2022-09-12 01:30:42 -05:00
import { formatApiUrl } from "utils/api-helpers";
2022-09-14 09:09:11 +03:00
import classNames from "classnames";
2022-09-12 01:30:42 -05:00
2022-09-14 09:09:11 +03:00
export default function CoinMarketCap({ service }) {
2022-09-12 01:30:42 -05:00
const { t } = useTranslation();
2022-09-14 09:09:11 +03:00
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);
2022-09-12 01:30:42 -05:00
const config = service.widget;
2022-09-12 12:38:50 +03:00
const currencyCode = config.currency ?? "USD";
const { symbols } = config;
2022-09-12 01:30:42 -05:00
const { data: statsData, error: statsError } = useSWR(
formatApiUrl(config, `v1/cryptocurrency/quotes/latest?symbol=${symbols.join(",")}&convert=${currencyCode}`)
);
if (!symbols || symbols.length === 0) {
return (
<Widget>
<Block value={t("coinmarketcap.configure")} />
</Widget>
);
2022-09-12 01:30:42 -05:00
}
if (statsError) {
return <Widget error={t("widget.api_error")} />;
}
2022-09-14 09:09:11 +03:00
if (!statsData || !dateRange) {
2022-09-12 01:30:42 -05:00
return (
<Widget>
<Block value={t("coinmarketcap.configure")} />
</Widget>
);
}
const { data } = statsData;
2022-09-12 12:38:50 +03:00
return (
<Widget>
2022-09-14 09:09:11 +03:00
<div className={classNames(service.description ? "-top-10" : "-top-8", "absolute right-1")}>
<Dropdown options={dateRangeOptions} value={dateRange} setValue={setDateRange} />
</div>
2022-09-12 12:38:50 +03:00
<div className="flex flex-col w-full">
2022-09-14 09:09:11 +03:00
{symbols.map((symbol) => (
2022-09-12 01:30:42 -05:00
<div
2022-09-14 09:09:11 +03:00
key={data[symbol].symbol}
2022-09-12 12:38:50 +03:00
className="bg-theme-200/50 dark:bg-theme-900/20 rounded m-1 flex-1 flex flex-row items-center justify-between p-1 text-xs"
2022-09-12 01:30:42 -05:00
>
2022-09-14 09:09:11 +03:00
<div className="font-thin pl-2">{data[symbol].name}</div>
2022-09-12 12:38:50 +03:00
<div className="flex flex-row text-right">
<div className="font-bold mr-2">
{t("common.number", {
2022-09-14 09:09:11 +03:00
value: data[symbol].quote[currencyCode].price,
style: "currency",
currency: currencyCode,
})}
2022-09-12 12:38:50 +03:00
</div>
<div
className={`font-bold w-10 mr-2 ${
2022-09-14 09:09:11 +03:00
data[symbol].quote[currencyCode][`percent_change_${dateRange}`] > 0
? "text-emerald-300"
: "text-rose-300"
2022-09-12 12:38:50 +03:00
}`}
>
2022-09-14 09:09:11 +03:00
{data[symbol].quote[currencyCode][`percent_change_${dateRange}`].toFixed(2)}%
2022-09-12 12:38:50 +03:00
</div>
</div>
2022-09-12 01:30:42 -05:00
</div>
2022-09-12 12:38:50 +03:00
))}
2022-09-12 01:30:42 -05:00
</div>
</Widget>
2022-09-12 12:38:50 +03:00
);
2022-09-12 01:30:42 -05:00
}