@@ -8,9 +8,11 @@ import {
88 CompletionSchema ,
99 type Completion_ButtonOptions ,
1010 Completion_ButtonOptionsSchema ,
11+ Completion_EventSchema ,
1112 HabitSchema ,
1213} from "@/proto/models/v1/models_pb"
1314import { clone , create } from "@bufbuild/protobuf"
15+ import { timestampNow } from "@bufbuild/protobuf/wkt"
1416import { ArrowRight , Check , Plus , Undo2 } from "lucide-react"
1517import React from "react"
1618import { toast } from "sonner"
@@ -33,22 +35,30 @@ const applyButton = (
3335) => {
3436 console . info ( "apply button" , { completion, options, manualValue } )
3537
38+ const previousCount = completion . count
39+
40+ let count = 0
3641 switch ( options . kind . case ) {
3742 case "delta" :
38- completion . count += manualValue ?? options . kind . value
39- return
43+ count = previousCount + ( manualValue ?? options . kind . value )
44+ break
4045 case "percentage" :
41- completion . count += Math . ceil (
42- ( ( manualValue ?? options . kind . value ) / 100 ) * completion . target ,
43- )
44- return
46+ count =
47+ previousCount +
48+ Math . ceil ( ( ( manualValue ?? options . kind . value ) / 100 ) * completion . target )
49+ break
4550 case "complete" :
46- completion . count = completion . target
47- return
51+ count = completion . target
52+ break
4853 case "set" :
49- completion . count = manualValue ?? options . kind . value
50- return
54+ count = manualValue ?? options . kind . value
55+ break
5156 }
57+
58+ completion . count = count
59+ completion . events . push (
60+ create ( Completion_EventSchema , { time : timestampNow ( ) , previousCount, count } ) ,
61+ )
5262}
5363
5464export const CompletionButton : React . FC < P > = ( { options = defaultOptions , preview, ...rest } ) => {
0 commit comments