Skip to content

Commit ea38541

Browse files
feat: src/useRecurring.ts (#17)
1 parent 0053bdd commit ea38541

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/useRecurring.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useCallback } from "react";
2+
3+
type Frequency = "daily" | "weekly" | "monthly";
4+
5+
interface RecurringTask {
6+
text: string;
7+
category: string;
8+
priority: "low" | "medium" | "high";
9+
frequency: Frequency;
10+
lastCreated: number;
11+
}
12+
13+
const INTERVALS: Record<Frequency, number> = {
14+
daily: 86400000,
15+
weekly: 604800000,
16+
monthly: 2592000000,
17+
};
18+
19+
export function useRecurring(
20+
recurringTasks: RecurringTask[],
21+
addTodo: (text: string, category: string, priority: "low" | "medium" | "high") => void
22+
) {
23+
const checkAndCreate = useCallback(() => {
24+
const now = Date.now();
25+
for (const task of recurringTasks) {
26+
if (now - task.lastCreated >= INTERVALS[task.frequency]) {
27+
addTodo(task.text, task.category, task.priority);
28+
task.lastCreated = now;
29+
}
30+
}
31+
}, [recurringTasks, addTodo]);
32+
33+
return { checkAndCreate };
34+
}

0 commit comments

Comments
 (0)