@wandelbots/wandelbots-js-react-components 2.40.0-pr.feature-seperate-timer.383.0494cf4 → 2.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/components/CycleTimer/DefaultVariant.d.ts.map +1 -1
  2. package/dist/components/CycleTimer/SmallVariant.d.ts.map +1 -1
  3. package/dist/components/CycleTimer/index.d.ts +5 -4
  4. package/dist/components/CycleTimer/index.d.ts.map +1 -1
  5. package/dist/components/CycleTimer/types.d.ts +3 -2
  6. package/dist/components/CycleTimer/types.d.ts.map +1 -1
  7. package/dist/components/CycleTimer/useTimerLogic.d.ts +2 -1
  8. package/dist/components/CycleTimer/useTimerLogic.d.ts.map +1 -1
  9. package/dist/components/TabBar.d.ts +2 -0
  10. package/dist/components/TabBar.d.ts.map +1 -1
  11. package/dist/index.cjs +47 -47
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +4840 -5249
  16. package/dist/index.js.map +1 -1
  17. package/package.json +1 -1
  18. package/src/components/CycleTimer/DefaultVariant.tsx +2 -0
  19. package/src/components/CycleTimer/SmallVariant.tsx +5 -2
  20. package/src/components/CycleTimer/index.tsx +5 -4
  21. package/src/components/CycleTimer/types.ts +3 -1
  22. package/src/components/CycleTimer/useTimerLogic.ts +96 -40
  23. package/src/components/CycleTimer/utils.ts +3 -3
  24. package/src/components/TabBar.tsx +35 -8
  25. package/src/i18n/locales/de/translations.json +0 -1
  26. package/src/i18n/locales/en/translations.json +0 -1
  27. package/src/index.ts +0 -1
  28. package/dist/components/Timer/Timer.d.ts +0 -3
  29. package/dist/components/Timer/Timer.d.ts.map +0 -1
  30. package/dist/components/Timer/TimerDefaultVariant.d.ts +0 -10
  31. package/dist/components/Timer/TimerDefaultVariant.d.ts.map +0 -1
  32. package/dist/components/Timer/TimerSmallVariant.d.ts +0 -11
  33. package/dist/components/Timer/TimerSmallVariant.d.ts.map +0 -1
  34. package/dist/components/Timer/index.d.ts +0 -19
  35. package/dist/components/Timer/index.d.ts.map +0 -1
  36. package/dist/components/Timer/types.d.ts +0 -36
  37. package/dist/components/Timer/types.d.ts.map +0 -1
  38. package/dist/components/Timer/useTimerAnimations.d.ts +0 -11
  39. package/dist/components/Timer/useTimerAnimations.d.ts.map +0 -1
  40. package/dist/components/Timer/useTimerLogic.d.ts +0 -20
  41. package/dist/components/Timer/useTimerLogic.d.ts.map +0 -1
  42. package/dist/components/Timer/utils.d.ts +0 -9
  43. package/dist/components/Timer/utils.d.ts.map +0 -1
  44. package/src/components/Timer/Timer.ts +0 -2
  45. package/src/components/Timer/TimerDefaultVariant.tsx +0 -140
  46. package/src/components/Timer/TimerSmallVariant.tsx +0 -140
  47. package/src/components/Timer/index.tsx +0 -101
  48. package/src/components/Timer/types.ts +0 -38
  49. package/src/components/Timer/useTimerAnimations.ts +0 -94
  50. package/src/components/Timer/useTimerLogic.ts +0 -214
  51. package/src/components/Timer/utils.ts +0 -15
@@ -1,214 +0,0 @@
1
- import { useCallback, useEffect, useRef, useState } from "react"
2
- import { useInterpolation } from "../utils/interpolation"
3
- import type { TimerState } from "./types"
4
-
5
- interface UseTimerLogicProps {
6
- autoStart: boolean
7
- hasError: boolean
8
- onPauseAnimation: () => void
9
- onErrorAnimation: () => void
10
- onClearErrorAnimation: () => void
11
- }
12
-
13
- export const useTimerLogic = ({
14
- autoStart,
15
- hasError,
16
- onPauseAnimation,
17
- onErrorAnimation,
18
- onClearErrorAnimation,
19
- }: UseTimerLogicProps) => {
20
- const [timerState, setTimerState] = useState<TimerState>({
21
- elapsedTime: 0,
22
- isRunning: false,
23
- isPausedState: false,
24
- currentProgress: 0,
25
- wasRunningBeforeError: false,
26
- })
27
-
28
- // Timer-related refs
29
- const animationRef = useRef<number | null>(null)
30
- const startTimeRef = useRef<number | null>(null)
31
- const pausedTimeRef = useRef<number>(0)
32
- const lastProgressRef = useRef<number>(0)
33
-
34
- // Spring-based interpolator for smooth gauge progress animations
35
- const [progressInterpolator] = useInterpolation([0], {
36
- tension: 80,
37
- friction: 18,
38
- onChange: ([progress]) => {
39
- setTimerState((prev) => ({ ...prev, currentProgress: progress }))
40
- },
41
- })
42
-
43
- const start = useCallback(
44
- (elapsedSeconds: number = 0) => {
45
- const initialProgress = ((elapsedSeconds / 60) % 1) * 100
46
- setTimerState((prev) => ({
47
- ...prev,
48
- elapsedTime: elapsedSeconds,
49
- isPausedState: false,
50
- currentProgress: initialProgress,
51
- }))
52
- pausedTimeRef.current = 0
53
- lastProgressRef.current = initialProgress
54
-
55
- progressInterpolator.setImmediate([initialProgress])
56
-
57
- if (autoStart) {
58
- startTimeRef.current = Date.now() - elapsedSeconds * 1000
59
- setTimerState((prev) => ({ ...prev, isRunning: true }))
60
- } else {
61
- startTimeRef.current = null
62
- }
63
- },
64
- [autoStart, progressInterpolator],
65
- )
66
-
67
- const pause = useCallback(() => {
68
- if (startTimeRef.current && timerState.isRunning) {
69
- const now = Date.now()
70
- const totalElapsed = (now - startTimeRef.current) / 1000 + pausedTimeRef.current
71
- const currentProgress = ((totalElapsed / 60) % 1) * 100
72
- progressInterpolator.setTarget([currentProgress])
73
-
74
- setTimerState((prev) => ({
75
- ...prev,
76
- elapsedTime: Math.floor(totalElapsed),
77
- }))
78
- }
79
-
80
- setTimerState((prev) => ({
81
- ...prev,
82
- isRunning: false,
83
- isPausedState: true,
84
- }))
85
- onPauseAnimation()
86
- }, [
87
- timerState.isRunning,
88
- progressInterpolator,
89
- onPauseAnimation,
90
- ])
91
-
92
- const resume = useCallback(() => {
93
- if (timerState.isPausedState) {
94
- pausedTimeRef.current = timerState.elapsedTime
95
- startTimeRef.current = Date.now()
96
- setTimerState((prev) => ({
97
- ...prev,
98
- isRunning: true,
99
- isPausedState: false,
100
- }))
101
- }
102
- }, [timerState.isPausedState, timerState.elapsedTime])
103
-
104
- const reset = useCallback(() => {
105
- setTimerState((prev) => ({
106
- ...prev,
107
- elapsedTime: 0,
108
- isRunning: false,
109
- isPausedState: false,
110
- currentProgress: 0,
111
- }))
112
- pausedTimeRef.current = 0
113
- startTimeRef.current = null
114
- lastProgressRef.current = 0
115
- progressInterpolator.setImmediate([0])
116
- }, [progressInterpolator])
117
-
118
- const isPaused = useCallback(() => {
119
- return timerState.isPausedState
120
- }, [timerState.isPausedState])
121
-
122
- // Handle error state changes
123
- useEffect(() => {
124
- if (hasError) {
125
- if (timerState.isRunning) {
126
- setTimerState((prev) => ({ ...prev, wasRunningBeforeError: true }))
127
- pause()
128
- }
129
- onErrorAnimation()
130
- } else {
131
- if (timerState.wasRunningBeforeError && !timerState.isRunning) {
132
- setTimerState((prev) => ({ ...prev, wasRunningBeforeError: false }))
133
- resume()
134
- }
135
- onClearErrorAnimation()
136
- }
137
- }, [
138
- hasError,
139
- timerState.isRunning,
140
- timerState.wasRunningBeforeError,
141
- pause,
142
- resume,
143
- onErrorAnimation,
144
- onClearErrorAnimation,
145
- ])
146
-
147
- // Main timer loop
148
- useEffect(() => {
149
- if (timerState.isRunning) {
150
- const updateTimer = () => {
151
- if (startTimeRef.current) {
152
- const now = Date.now()
153
- const totalElapsed = (now - startTimeRef.current) / 1000 + pausedTimeRef.current
154
- const currentProgress = ((totalElapsed / 60) % 1) * 100
155
-
156
- setTimerState((prev) => ({
157
- ...prev,
158
- elapsedTime: Math.floor(totalElapsed),
159
- }))
160
-
161
- // Only update progress interpolator if progress changed significantly
162
- const progressDiff = Math.abs(currentProgress - lastProgressRef.current)
163
- if (progressDiff > 0.1) {
164
- progressInterpolator.setTarget([currentProgress])
165
- lastProgressRef.current = currentProgress
166
- }
167
- }
168
- animationRef.current = requestAnimationFrame(updateTimer)
169
- }
170
- animationRef.current = requestAnimationFrame(updateTimer)
171
- } else {
172
- if (animationRef.current) {
173
- cancelAnimationFrame(animationRef.current)
174
- animationRef.current = null
175
- }
176
- }
177
-
178
- return () => {
179
- if (animationRef.current) {
180
- cancelAnimationFrame(animationRef.current)
181
- animationRef.current = null
182
- }
183
- }
184
- }, [timerState.isRunning, progressInterpolator])
185
-
186
- // Interpolation animation loop
187
- useEffect(() => {
188
- let interpolationAnimationId: number | null = null
189
-
190
- const animateInterpolation = () => {
191
- progressInterpolator.update()
192
- interpolationAnimationId = requestAnimationFrame(animateInterpolation)
193
- }
194
-
195
- interpolationAnimationId = requestAnimationFrame(animateInterpolation)
196
-
197
- return () => {
198
- if (interpolationAnimationId) {
199
- cancelAnimationFrame(interpolationAnimationId)
200
- }
201
- }
202
- }, [progressInterpolator])
203
-
204
- return {
205
- timerState,
206
- controls: {
207
- start,
208
- pause,
209
- resume,
210
- reset,
211
- isPaused,
212
- },
213
- }
214
- }
@@ -1,15 +0,0 @@
1
- /**
2
- * Formats time in seconds to MM:SS format
3
- */
4
- export const formatTime = (seconds: number): string => {
5
- const minutes = Math.floor(seconds / 60)
6
- const remainingSeconds = seconds % 60
7
- return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
8
- }
9
-
10
- /**
11
- * Calculates progress percentage for timer (minute-based cycles)
12
- */
13
- export const calculateTimerProgress = (elapsedTime: number): number => {
14
- return ((elapsedTime / 60) % 1) * 100
15
- }