3
3
import { useState , useEffect } from 'react'
4
4
import { RRule , RRuleSet , rrulestr } from 'rrule'
5
5
import { useAtom } from 'jotai'
6
- import { settingsAtom } from '@/lib/atoms'
6
+ import { settingsAtom , browserSettingsAtom } from '@/lib/atoms'
7
7
import { Dialog , DialogContent , DialogHeader , DialogTitle , DialogFooter } from '@/components/ui/dialog'
8
8
import { Button } from '@/components/ui/button'
9
9
import { Input } from '@/components/ui/input'
@@ -16,8 +16,10 @@ import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover
16
16
import data from '@emoji-mart/data'
17
17
import Picker from '@emoji-mart/react'
18
18
import { Habit } from '@/lib/types'
19
- import { parseNaturalLanguageRRule , parseRRule , serializeRRule } from '@/lib/utils'
20
- import { INITIAL_RECURRENCE_RULE } from '@/lib/constants'
19
+ import { d2s , d2t , getISODate , getNow , parseNaturalLanguageDate , parseNaturalLanguageRRule , parseRRule , serializeRRule } from '@/lib/utils'
20
+ import { INITIAL_DUE , INITIAL_RECURRENCE_RULE } from '@/lib/constants'
21
+ import * as chrono from 'chrono-node' ;
22
+ import { DateTime } from 'luxon'
21
23
22
24
interface AddEditHabitModalProps {
23
25
onClose : ( ) => void
@@ -27,12 +29,16 @@ interface AddEditHabitModalProps {
27
29
28
30
export default function AddEditHabitModal ( { onClose, onSave, habit } : AddEditHabitModalProps ) {
29
31
const [ settings ] = useAtom ( settingsAtom )
32
+ const [ browserSettings ] = useAtom ( browserSettingsAtom )
33
+ const isTasksView = browserSettings . viewType === 'tasks'
30
34
const [ name , setName ] = useState ( habit ?. name || '' )
31
35
const [ description , setDescription ] = useState ( habit ?. description || '' )
32
36
const [ coinReward , setCoinReward ] = useState ( habit ?. coinReward || 1 )
33
37
const [ targetCompletions , setTargetCompletions ] = useState ( habit ?. targetCompletions || 1 )
34
- const origRuleText = parseRRule ( habit ?. frequency || INITIAL_RECURRENCE_RULE ) . toText ( )
38
+ const isRecurRule = ! isTasksView
39
+ const origRuleText = isRecurRule ? parseRRule ( habit ?. frequency || INITIAL_RECURRENCE_RULE ) . toText ( ) : habit ?. frequency || INITIAL_DUE
35
40
const [ ruleText , setRuleText ] = useState < string > ( origRuleText )
41
+ const now = getNow ( { timezone : settings . system . timezone } )
36
42
37
43
const handleSubmit = async ( e : React . FormEvent ) => {
38
44
e . preventDefault ( )
@@ -42,17 +48,16 @@ export default function AddEditHabitModal({ onClose, onSave, habit }: AddEditHab
42
48
coinReward,
43
49
targetCompletions : targetCompletions > 1 ? targetCompletions : undefined ,
44
50
completions : habit ?. completions || [ ] ,
45
- frequency : habit ? (
46
- origRuleText === ruleText ? habit . frequency : serializeRRule ( parseNaturalLanguageRRule ( ruleText ) )
47
- ) : serializeRRule ( parseNaturalLanguageRRule ( ruleText ) ) ,
51
+ frequency : isRecurRule ? serializeRRule ( parseNaturalLanguageRRule ( ruleText ) ) : d2t ( { dateTime : parseNaturalLanguageDate ( { text : ruleText , timezone : settings . system . timezone } ) } ) ,
52
+ isTask : isTasksView ? true : undefined
48
53
} )
49
54
}
50
55
51
56
return (
52
57
< Dialog open = { true } onOpenChange = { onClose } >
53
58
< DialogContent >
54
59
< DialogHeader >
55
- < DialogTitle > { habit ? ' Edit Habit' : 'Add New Habit' } </ DialogTitle >
60
+ < DialogTitle > { habit ? ` Edit ${ isTasksView ? 'Task' : 'Habit' } ` : ` Add New ${ isTasksView ? 'Task' : ' Habit'} ` } </ DialogTitle >
56
61
</ DialogHeader >
57
62
< form onSubmit = { handleSubmit } >
58
63
< div className = "grid gap-4 py-4" >
@@ -109,7 +114,7 @@ export default function AddEditHabitModal({ onClose, onSave, habit }: AddEditHab
109
114
</ div >
110
115
< div className = "grid grid-cols-4 items-center gap-4" >
111
116
< Label htmlFor = "recurrence" className = "text-right" >
112
- Frequency
117
+ When
113
118
</ Label >
114
119
< div className = "col-span-3 space-y-2" >
115
120
< Input
@@ -123,7 +128,7 @@ export default function AddEditHabitModal({ onClose, onSave, habit }: AddEditHab
123
128
< span >
124
129
{ ( ( ) => {
125
130
try {
126
- return parseNaturalLanguageRRule ( ruleText ) . toText ( )
131
+ return isRecurRule ? parseNaturalLanguageRRule ( ruleText ) . toText ( ) : d2s ( { dateTime : parseNaturalLanguageDate ( { text : ruleText , timezone : settings . system . timezone } ) , timezone : settings . system . timezone , format : DateTime . DATE_MED_WITH_WEEKDAY } )
127
132
} catch ( e : unknown ) {
128
133
return `Invalid rule: ${ e instanceof Error ? e . message : 'Invalid recurrence rule' } `
129
134
}
@@ -134,7 +139,7 @@ export default function AddEditHabitModal({ onClose, onSave, habit }: AddEditHab
134
139
< div className = "grid grid-cols-4 items-center gap-4" >
135
140
< div className = "flex items-center gap-2 justify-end" >
136
141
< Label htmlFor = "targetCompletions" >
137
- Repetitions
142
+ Complete
138
143
</ Label >
139
144
</ div >
140
145
< div className = "col-span-3" >
@@ -168,15 +173,15 @@ export default function AddEditHabitModal({ onClose, onSave, habit }: AddEditHab
168
173
</ button >
169
174
</ div >
170
175
< span className = "text-sm text-muted-foreground" >
171
- times per occurrence
176
+ times
172
177
</ span >
173
178
</ div >
174
179
</ div >
175
180
</ div >
176
181
< div className = "grid grid-cols-4 items-center gap-4" >
177
182
< div className = "flex items-center gap-2 justify-end" >
178
183
< Label htmlFor = "coinReward" >
179
- Coin Reward
184
+ Reward
180
185
</ Label >
181
186
</ div >
182
187
< div className = "col-span-3" >
@@ -207,14 +212,14 @@ export default function AddEditHabitModal({ onClose, onSave, habit }: AddEditHab
207
212
</ button >
208
213
</ div >
209
214
< span className = "text-sm text-muted-foreground" >
210
- coins per completion
215
+ coins
211
216
</ span >
212
217
</ div >
213
218
</ div >
214
219
</ div >
215
220
</ div >
216
221
< DialogFooter >
217
- < Button type = "submit" > { habit ? 'Save Changes' : ' Add Habit'} </ Button >
222
+ < Button type = "submit" > { habit ? 'Save Changes' : ` Add ${ isTasksView ? 'Task' : ' Habit'} ` } </ Button >
218
223
</ DialogFooter >
219
224
</ form >
220
225
</ DialogContent >
0 commit comments