forked from
chadtmiller.com/slices-teal-relay
Teal.fm frontend powered by slices.network
tealfm-slices.wisp.place
tealfm
slices
1import { useMemo } from "react";
2
3export function useDateRangeFilter(period: string | undefined) {
4 return useMemo(() => {
5 if (!period || period === "all") {
6 return { where: undefined };
7 }
8
9 // Round to start of current day to keep the timestamp stable
10 const now = new Date();
11 now.setHours(0, 0, 0, 0);
12
13 let daysAgo = 0;
14 switch (period) {
15 case "daily":
16 daysAgo = 1;
17 break;
18 case "weekly":
19 daysAgo = 7;
20 break;
21 case "monthly":
22 daysAgo = 30;
23 break;
24 default:
25 return { where: undefined };
26 }
27
28 const startDate = new Date(now.getTime() - daysAgo * 24 * 60 * 60 * 1000);
29 return { where: { playedTime: { gte: startDate.toISOString() } } };
30 }, [period]);
31}