Skip to content

Commit

Permalink
feat(ReportMonthly): Implement change month by using L2 or R2 buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
ynhhoJ committed Jan 6, 2025
1 parent 2a3e4a8 commit ed6bee3
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/containers/ReportMonthly.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PanelSection, PanelSectionRow } from "@decky/ui";
import { type VFC, useEffect, useState } from "react";
import { Button } from "@src/steam/enums/Button";
import { registerForInputEvent } from "@src/steam/registerForInputEvent";
import { useEffect, useState } from "react";
import { formatMonthInterval } from "../app/formatters";
import {
type DailyStatistics,
Expand All @@ -14,35 +16,73 @@ import { MonthView } from "../components/statistics/MonthView";
import { PieView } from "../components/statistics/PieView";
import { useLocator } from "../locator";

export const ReportMonthly: VFC = () => {
export const ReportMonthly = () => {
const { reports, currentSettings: settings } = useLocator();
const [lastChangedPageTimeStamp, setLastChangedPageTimeStamp] =
useState<number>(0);
const [isLoading, setLoading] = useState<boolean>(false);
const [currentPage, setCurrentPage] = useState<Paginated<DailyStatistics>>(
empty(),
);

useEffect(() => {
setLoading(true);

reports.monthlyStatistics().then((it) => {
setCurrentPage(it);
setLoading(false);
});
}, []);

useEffect(() => {
const { unregister } = registerForInputEvent((buttons) => {
if (buttons.length !== 1) {
return;
}

if (new Date().getTime() - lastChangedPageTimeStamp <= 500) {
return;
}

if (buttons.includes(Button.L2) && currentPage.hasPrev()) {
setLastChangedPageTimeStamp(new Date().getTime());

onPrevWeek();
}

if (buttons.includes(Button.R2) && currentPage.hasNext()) {
setLastChangedPageTimeStamp(new Date().getTime());

onNextWeek();
}
});

return () => {
unregister();
};
}, [
currentPage.current().interval.start.getTime(),
currentPage.current().interval.end.getTime(),
]);

const onNextWeek = () => {
setLoading(true);

currentPage?.next().then((it) => {
setCurrentPage(it);
setLoading(false);
});
};

const onPrevWeek = () => {
setLoading(true);

currentPage?.prev().then((it) => {
setCurrentPage(it);
setLoading(false);
});
};

return (
<div>
<PanelSection>
Expand All @@ -53,6 +93,8 @@ export const ReportMonthly: VFC = () => {
currentText={formatMonthInterval(currentPage.current().interval)}
hasNext={currentPage.hasNext()}
hasPrev={currentPage.hasPrev()}
prevKey="l2"
nextKey="r2"
/>
</PanelSectionRow>
</PanelSection>
Expand Down

0 comments on commit ed6bee3

Please sign in to comment.