homepage/src/widgets/ghostfolio/component.jsx

55 lines
2.2 KiB
React
Raw Normal View History

2023-03-01 22:45:22 -08:00
import { useTranslation } from "next-i18next";
2023-03-01 19:44:13 +01:00
import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
2023-03-01 22:45:22 -08:00
function getPerformancePercent(t, performanceRange) {
// ghostfolio v2.79.0 changed to grossPerformancePercentage
// ghostfolio v2.106.0 changed to netPerformancePercentageWithCurrencyEffect
const percent =
performanceRange.performance.netPerformancePercentageWithCurrencyEffect ??
performanceRange.performance.grossPerformancePercentage ??
performanceRange.performance.currentGrossPerformancePercent;
return `${percent > 0 ? "+" : ""}${t("common.percent", {
value: percent * 100,
maximumFractionDigits: 2,
})}`;
2023-03-01 22:45:22 -08:00
}
2023-03-01 19:44:13 +01:00
2023-03-01 22:45:22 -08:00
export default function Component({ service }) {
const { t } = useTranslation();
2023-03-01 19:44:13 +01:00
const { widget } = service;
const { data: performanceToday, error: ghostfolioErrorToday } = useWidgetAPI(widget, "today");
2023-03-01 22:45:22 -08:00
const { data: performanceYear, error: ghostfolioErrorYear } = useWidgetAPI(widget, "year");
const { data: performanceMax, error: ghostfolioErrorMax } = useWidgetAPI(widget, "max");
2023-03-01 19:44:13 +01:00
2023-03-01 22:45:22 -08:00
if (ghostfolioErrorToday || ghostfolioErrorYear || ghostfolioErrorMax) {
const finalError = ghostfolioErrorToday ?? ghostfolioErrorYear ?? ghostfolioErrorMax;
2023-04-30 19:09:37 -04:00
return <Container service={service} error={finalError} />;
2023-03-01 19:44:13 +01:00
}
if (performanceToday?.statusCode === 401) {
return <Container service={service} error={performanceToday} />;
}
2023-03-01 22:45:22 -08:00
if (!performanceToday || !performanceYear || !performanceMax) {
return (
<Container service={service}>
<Block label="ghostfolio.gross_percent_today" />
<Block label="ghostfolio.gross_percent_1y" />
<Block label="ghostfolio.gross_percent_max" />
</Container>
);
2023-03-01 19:44:13 +01:00
}
return (
<Container service={service}>
2023-03-01 22:45:22 -08:00
<Block label="ghostfolio.gross_percent_today" value={getPerformancePercent(t, performanceToday)} />
<Block label="ghostfolio.gross_percent_1y" value={getPerformancePercent(t, performanceYear)} />
<Block label="ghostfolio.gross_percent_max" value={getPerformancePercent(t, performanceMax)} />
2023-03-01 19:44:13 +01:00
</Container>
);
}