homepage/src/widgets/authentik/component.jsx

48 lines
1.7 KiB
React
Raw Normal View History

2022-09-25 17:42:16 -07:00
import { useTranslation } from "next-i18next";
2022-09-26 15:25:10 +03:00
import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
2022-09-27 22:59:14 +03:00
import useWidgetAPI from "utils/proxy/use-widget-api";
2022-09-25 17:42:16 -07:00
export default function Component({ service }) {
const { t } = useTranslation();
2022-09-27 22:59:14 +03:00
const { widget } = service;
2022-09-25 17:42:16 -07:00
2022-09-27 22:59:14 +03:00
const { data: usersData, error: usersError } = useWidgetAPI(widget, "users");
const { data: loginsData, error: loginsError } = useWidgetAPI(widget, "login");
const { data: failedLoginsData, error: failedLoginsError } = useWidgetAPI(widget, "login_failed");
2022-09-25 17:42:16 -07:00
if (usersError || loginsError || failedLoginsError) {
2022-09-26 15:25:10 +03:00
return <Container error={t("widget.api_error")} />;
2022-09-25 17:42:16 -07:00
}
if (!usersData || !loginsData || !failedLoginsData) {
return (
2022-09-26 15:25:10 +03:00
<Container>
2022-09-25 17:42:16 -07:00
<Block label={t("authentik.users")} />
<Block label={t("authentik.loginsLast24H")} />
<Block label={t("authentik.failedLoginsLast24H")} />
2022-09-26 15:25:10 +03:00
</Container>
2022-09-25 17:42:16 -07:00
);
}
const yesterday = new Date(Date.now()).setHours(-24);
const loginsLast24H = loginsData.reduce(
(total, current) => (current.x_cord >= yesterday ? total + current.y_cord : total),
0
);
2022-09-25 17:42:16 -07:00
const failedLoginsLast24H = failedLoginsData.reduce(
(total, current) => (current.x_cord >= yesterday ? total + current.y_cord : total),
0
);
2022-09-25 17:42:16 -07:00
return (
2022-09-26 15:25:10 +03:00
<Container>
2022-09-25 17:42:16 -07:00
<Block label={t("authentik.users")} value={t("common.number", { value: usersData.pagination.count })} />
<Block label={t("authentik.loginsLast24H")} value={t("common.number", { value: loginsLast24H })} />
<Block label={t("authentik.failedLoginsLast24H")} value={t("common.number", { value: failedLoginsLast24H })} />
2022-09-26 15:25:10 +03:00
</Container>
2022-09-25 17:42:16 -07:00
);
}