File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments