/*
This file is part of GNU Taler
(C) 2022-2024 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see
*/
import {
AmountString,
Amounts,
CurrencySpecification,
HttpStatusCode,
TalerCorebankApi,
TalerError,
assertUnreachable,
} from "@gnu-taler/taler-util";
import { Attention, useTranslationContext } from "@gnu-taler/web-util/browser";
import {
format,
getDate,
getHours,
getMonth,
getYear,
setDate,
setHours,
setMonth,
setYear,
sub,
} from "date-fns";
import { Fragment, VNode, h } from "preact";
import { useState } from "preact/hooks";
import { ErrorLoadingWithDebug } from "../../components/ErrorLoadingWithDebug.js";
import { Transactions } from "../../components/Transactions/index.js";
import { useBankCoreApiContext } from "../../context/config.js";
import { useConversionInfo, useLastMonitorInfo } from "../../hooks/circuit.js";
import { RouteDefinition } from "../../route.js";
import { RenderAmount } from "../PaytoWireTransferForm.js";
import { WireTransfer } from "../WireTransfer.js";
import { AccountList } from "./AccountList.js";
/**
* Query account information and show QR code if there is pending withdrawal
*/
interface Props {
routeCreate: RouteDefinition>;
routeDownloadStats: RouteDefinition>;
routeCreateWireTransfer: RouteDefinition<{ destination: string }>;
routeShowAccount: RouteDefinition<{ account: string }>;
routeRemoveAccount: RouteDefinition<{ account: string }>;
routeUpdatePasswordAccount: RouteDefinition<{ account: string }>;
routeShowCashoutsAccount: RouteDefinition<{ account: string }>;
onAuthorizationRequired: () => void;
}
export function AdminHome({
routeCreate,
routeRemoveAccount,
routeShowAccount,
routeShowCashoutsAccount,
routeUpdatePasswordAccount,
routeDownloadStats,
routeCreateWireTransfer,
onAuthorizationRequired,
}: Props): VNode {
return (
);
}
function getDateForTimeframe(
which: number,
timeframe: TalerCorebankApi.MonitorTimeframeParam,
locale: Locale,
): string {
const time = Date.now();
switch (timeframe) {
case TalerCorebankApi.MonitorTimeframeParam.hour:
return `${format(setHours(time, which), "HH", { locale })}hs`;
case TalerCorebankApi.MonitorTimeframeParam.day:
return format(setDate(time, which), "EEEE", { locale });
case TalerCorebankApi.MonitorTimeframeParam.month:
return format(setMonth(time, which), "MMMM", { locale });
case TalerCorebankApi.MonitorTimeframeParam.year:
return format(setYear(time, which), "yyyy", { locale });
case TalerCorebankApi.MonitorTimeframeParam.decade:
return format(setYear(time, which), "yyyy", { locale });
}
assertUnreachable(timeframe);
}
export function getTimeframesForDate(
time: Date,
timeframe: TalerCorebankApi.MonitorTimeframeParam,
): { current: number; previous: number } {
switch (timeframe) {
case TalerCorebankApi.MonitorTimeframeParam.hour:
return {
current: getHours(sub(time, { hours: 1 })),
previous: getHours(sub(time, { hours: 2 })),
};
case TalerCorebankApi.MonitorTimeframeParam.day:
return {
current: getDate(sub(time, { days: 1 })),
previous: getDate(sub(time, { days: 2 })),
};
case TalerCorebankApi.MonitorTimeframeParam.month:
return {
current: getMonth(sub(time, { months: 1 })),
previous: getMonth(sub(time, { months: 2 })),
};
case TalerCorebankApi.MonitorTimeframeParam.year:
return {
current: getYear(sub(time, { years: 1 })),
previous: getYear(sub(time, { years: 2 })),
};
case TalerCorebankApi.MonitorTimeframeParam.decade:
return {
current: getYear(sub(time, { years: 10 })),
previous: getYear(sub(time, { years: 20 })),
};
default:
assertUnreachable(timeframe);
}
}
function Metrics({
routeDownloadStats,
}: {
routeDownloadStats: RouteDefinition>;
}): VNode {
const { i18n, dateLocale } = useTranslationContext();
const [metricType, setMetricType] =
useState(
TalerCorebankApi.MonitorTimeframeParam.hour,
);
const { config } = useBankCoreApiContext();
const respInfo = useConversionInfo();
const params = getTimeframesForDate(new Date(), metricType);
const resp = useLastMonitorInfo(params.current, params.previous, metricType);
if (!resp) return ;
if (resp instanceof TalerError) {
return ;
}
if (!respInfo) return ;
if (respInfo instanceof TalerError) {
return ;
}
if (respInfo.type === "fail") {
switch (respInfo.case) {
case HttpStatusCode.NotImplemented: {
return (
Cashout should be enable by configuration and the conversion rate
should be initialized with fee, ratio and rounding mode.
);
}
default:
assertUnreachable(respInfo.case);
}
}
if (resp.current.type !== "ok" || resp.previous.type !== "ok") {
return ;
}
return (
{/* FIXME: This should be LINKS */}
{i18n.str`Trading volume on ${getDateForTimeframe(
params.current,
metricType,
dateLocale,
)} compared to ${getDateForTimeframe(
params.previous,
metricType,
dateLocale,
)}`}