@rakeyshgidwani/roger-ui-bank-theme-stan-design 0.1.4 → 0.1.5

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 (164) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/index.d.ts +131 -131
  3. package/dist/index.esm.js +148 -148
  4. package/dist/index.js +148 -148
  5. package/dist/styles.css +1 -1
  6. package/package.json +1 -1
  7. package/src/components/ui/accessibility-demo.tsx +271 -0
  8. package/src/components/ui/advanced-component-architecture-demo.tsx +916 -0
  9. package/src/components/ui/advanced-transition-system-demo.tsx +670 -0
  10. package/src/components/ui/advanced-transition-system.tsx +395 -0
  11. package/src/components/ui/animation/animated-container.tsx +166 -0
  12. package/src/components/ui/animation/index.ts +19 -0
  13. package/src/components/ui/animation/staggered-container.tsx +68 -0
  14. package/src/components/ui/animation-demo.tsx +250 -0
  15. package/src/components/ui/badge.tsx +33 -0
  16. package/src/components/ui/battery-conscious-animation-demo.tsx +568 -0
  17. package/src/components/ui/border-radius-shadow-demo.tsx +187 -0
  18. package/src/components/ui/button.tsx +36 -0
  19. package/src/components/ui/card.tsx +207 -0
  20. package/src/components/ui/checkbox.tsx +30 -0
  21. package/src/components/ui/color-preview.tsx +411 -0
  22. package/src/components/ui/data-display/chart.tsx +653 -0
  23. package/src/components/ui/data-display/data-grid-simple.tsx +76 -0
  24. package/src/components/ui/data-display/data-grid.tsx +680 -0
  25. package/src/components/ui/data-display/list.tsx +456 -0
  26. package/src/components/ui/data-display/table.tsx +482 -0
  27. package/src/components/ui/data-display/timeline.tsx +441 -0
  28. package/src/components/ui/data-display/tree.tsx +602 -0
  29. package/src/components/ui/data-display/types.ts +536 -0
  30. package/src/components/ui/enterprise-mobile-experience-demo.tsx +749 -0
  31. package/src/components/ui/enterprise-mobile-experience.tsx +464 -0
  32. package/src/components/ui/feedback/alert.tsx +157 -0
  33. package/src/components/ui/feedback/progress.tsx +292 -0
  34. package/src/components/ui/feedback/skeleton.tsx +185 -0
  35. package/src/components/ui/feedback/toast.tsx +280 -0
  36. package/src/components/ui/feedback/types.ts +125 -0
  37. package/src/components/ui/font-preview.tsx +288 -0
  38. package/src/components/ui/form-demo.tsx +553 -0
  39. package/src/components/ui/hardware-acceleration-demo.tsx +547 -0
  40. package/src/components/ui/input.tsx +35 -0
  41. package/src/components/ui/label.tsx +16 -0
  42. package/src/components/ui/layout-demo.tsx +367 -0
  43. package/src/components/ui/layouts/adaptive-layout.tsx +139 -0
  44. package/src/components/ui/layouts/desktop-layout.tsx +224 -0
  45. package/src/components/ui/layouts/index.ts +10 -0
  46. package/src/components/ui/layouts/mobile-layout.tsx +162 -0
  47. package/src/components/ui/layouts/tablet-layout.tsx +197 -0
  48. package/src/components/ui/mobile-form-validation.tsx +451 -0
  49. package/src/components/ui/mobile-input-demo.tsx +201 -0
  50. package/src/components/ui/mobile-input.tsx +281 -0
  51. package/src/components/ui/mobile-skeleton-loading-demo.tsx +638 -0
  52. package/src/components/ui/navigation/breadcrumb.tsx +158 -0
  53. package/src/components/ui/navigation/index.ts +36 -0
  54. package/src/components/ui/navigation/menu.tsx +374 -0
  55. package/src/components/ui/navigation/navigation-demo.tsx +324 -0
  56. package/src/components/ui/navigation/pagination.tsx +272 -0
  57. package/src/components/ui/navigation/sidebar.tsx +383 -0
  58. package/src/components/ui/navigation/stepper.tsx +303 -0
  59. package/src/components/ui/navigation/tabs.tsx +205 -0
  60. package/src/components/ui/navigation/types.ts +299 -0
  61. package/src/components/ui/overlay/backdrop.tsx +81 -0
  62. package/src/components/ui/overlay/focus-manager.tsx +143 -0
  63. package/src/components/ui/overlay/index.ts +36 -0
  64. package/src/components/ui/overlay/modal.tsx +270 -0
  65. package/src/components/ui/overlay/overlay-manager.tsx +110 -0
  66. package/src/components/ui/overlay/popover.tsx +462 -0
  67. package/src/components/ui/overlay/portal.tsx +79 -0
  68. package/src/components/ui/overlay/tooltip.tsx +303 -0
  69. package/src/components/ui/overlay/types.ts +196 -0
  70. package/src/components/ui/performance-demo.tsx +596 -0
  71. package/src/components/ui/semantic-input-system-demo.tsx +502 -0
  72. package/src/components/ui/semantic-input-system-demo.tsx.disabled +873 -0
  73. package/src/components/ui/tablet-layout.tsx +192 -0
  74. package/src/components/ui/theme-customizer.tsx +386 -0
  75. package/src/components/ui/theme-preview.tsx +310 -0
  76. package/src/components/ui/theme-switcher.tsx +264 -0
  77. package/src/components/ui/theme-toggle.tsx +38 -0
  78. package/src/components/ui/token-demo.tsx +195 -0
  79. package/src/components/ui/touch-demo.tsx +462 -0
  80. package/src/components/ui/touch-friendly-interface-demo.tsx +519 -0
  81. package/src/components/ui/touch-friendly-interface.tsx +296 -0
  82. package/src/hooks/index.ts +190 -0
  83. package/src/hooks/use-accessibility-support.ts +518 -0
  84. package/src/hooks/use-adaptive-layout.ts +289 -0
  85. package/src/hooks/use-advanced-patterns.ts +294 -0
  86. package/src/hooks/use-advanced-transition-system.ts +393 -0
  87. package/src/hooks/use-animation-profile.ts +288 -0
  88. package/src/hooks/use-battery-animations.ts +384 -0
  89. package/src/hooks/use-battery-conscious-loading.ts +475 -0
  90. package/src/hooks/use-battery-optimization.ts +330 -0
  91. package/src/hooks/use-battery-status.ts +299 -0
  92. package/src/hooks/use-component-performance.ts +344 -0
  93. package/src/hooks/use-device-loading-states.ts +459 -0
  94. package/src/hooks/use-device.tsx +110 -0
  95. package/src/hooks/use-enterprise-mobile-experience.ts +488 -0
  96. package/src/hooks/use-form-feedback.ts +403 -0
  97. package/src/hooks/use-form-performance.ts +513 -0
  98. package/src/hooks/use-frame-rate.ts +251 -0
  99. package/src/hooks/use-gestures.ts +338 -0
  100. package/src/hooks/use-hardware-acceleration.ts +341 -0
  101. package/src/hooks/use-input-accessibility.ts +455 -0
  102. package/src/hooks/use-input-performance.ts +506 -0
  103. package/src/hooks/use-layout-performance.ts +319 -0
  104. package/src/hooks/use-loading-accessibility.ts +535 -0
  105. package/src/hooks/use-loading-performance.ts +473 -0
  106. package/src/hooks/use-memory-usage.ts +287 -0
  107. package/src/hooks/use-mobile-form-layout.ts +464 -0
  108. package/src/hooks/use-mobile-form-validation.ts +518 -0
  109. package/src/hooks/use-mobile-keyboard-optimization.ts +472 -0
  110. package/src/hooks/use-mobile-layout.ts +302 -0
  111. package/src/hooks/use-mobile-optimization.ts +406 -0
  112. package/src/hooks/use-mobile-skeleton.ts +402 -0
  113. package/src/hooks/use-mobile-touch.ts +414 -0
  114. package/src/hooks/use-performance-throttling.ts +348 -0
  115. package/src/hooks/use-performance.ts +316 -0
  116. package/src/hooks/use-reusable-architecture.ts +414 -0
  117. package/src/hooks/use-semantic-input-types.ts +357 -0
  118. package/src/hooks/use-semantic-input.ts +565 -0
  119. package/src/hooks/use-tablet-layout.ts +384 -0
  120. package/src/hooks/use-touch-friendly-input.ts +524 -0
  121. package/src/hooks/use-touch-friendly-interface.ts +331 -0
  122. package/src/hooks/use-touch-optimization.ts +375 -0
  123. package/src/index.ts +279 -279
  124. package/src/lib/utils.ts +6 -0
  125. package/src/themes/README.md +272 -0
  126. package/src/themes/ThemeContext.tsx +31 -0
  127. package/src/themes/ThemeProvider.tsx +232 -0
  128. package/src/themes/accessibility/index.ts +27 -0
  129. package/src/themes/accessibility.ts +259 -0
  130. package/src/themes/aria-patterns.ts +420 -0
  131. package/src/themes/base-themes.ts +55 -0
  132. package/src/themes/colorManager.ts +380 -0
  133. package/src/themes/examples/dark-theme.ts +154 -0
  134. package/src/themes/examples/minimal-theme.ts +108 -0
  135. package/src/themes/focus-management.ts +701 -0
  136. package/src/themes/fontLoader.ts +201 -0
  137. package/src/themes/high-contrast.ts +621 -0
  138. package/src/themes/index.ts +19 -0
  139. package/src/themes/inheritance.ts +227 -0
  140. package/src/themes/keyboard-navigation.ts +550 -0
  141. package/src/themes/motion-reduction.ts +662 -0
  142. package/src/themes/navigation.ts +238 -0
  143. package/src/themes/screen-reader.ts +645 -0
  144. package/src/themes/systemThemeDetector.ts +182 -0
  145. package/src/themes/themeCSSUpdater.ts +262 -0
  146. package/src/themes/themePersistence.ts +238 -0
  147. package/src/themes/themes/default.ts +586 -0
  148. package/src/themes/themes/harvey.ts +554 -0
  149. package/src/themes/themes/stan-design.ts +683 -0
  150. package/src/themes/types.ts +460 -0
  151. package/src/themes/useSystemTheme.ts +48 -0
  152. package/src/themes/useTheme.ts +87 -0
  153. package/src/themes/validation.ts +462 -0
  154. package/src/tokens/index.ts +34 -0
  155. package/src/tokens/tokenExporter.ts +397 -0
  156. package/src/tokens/tokenGenerator.ts +276 -0
  157. package/src/tokens/tokenManager.ts +248 -0
  158. package/src/tokens/tokenValidator.ts +543 -0
  159. package/src/tokens/types.ts +78 -0
  160. package/src/utils/bundle-analyzer.ts +260 -0
  161. package/src/utils/bundle-splitting.ts +483 -0
  162. package/src/utils/lazy-loading.ts +441 -0
  163. package/src/utils/performance-monitor.ts +513 -0
  164. package/src/utils/tree-shaking.ts +274 -0
@@ -0,0 +1,330 @@
1
+ import { useState, useEffect, useCallback, useRef } from 'react'
2
+
3
+ export interface BatteryStatus {
4
+ level: number
5
+ charging: boolean
6
+ chargingTime: number
7
+ dischargingTime: number
8
+ isLow: boolean
9
+ isCritical: boolean
10
+ }
11
+
12
+ export interface BatteryOptimizationConfig {
13
+ enableBatteryMonitoring?: boolean
14
+ enablePerformanceThrottling?: boolean
15
+ enableUserPreferences?: boolean
16
+ enableAdaptiveOptimization?: boolean
17
+ lowBatteryThreshold?: number
18
+ criticalBatteryThreshold?: number
19
+ performanceThrottleLevel?: 'none' | 'light' | 'moderate' | 'aggressive'
20
+ userPreferenceLevel?: 'performance' | 'balanced' | 'battery'
21
+ }
22
+
23
+ export interface BatteryOptimizationCallbacks {
24
+ onBatteryStatusChange?: (status: BatteryStatus) => void
25
+ onLowBatteryWarning?: (status: BatteryStatus) => void
26
+ onCriticalBatteryWarning?: (status: BatteryStatus) => void
27
+ onPerformanceThrottled?: (level: string, reason: string) => void
28
+ onOptimizationApplied?: (optimization: string) => void
29
+ }
30
+
31
+ export const useBatteryOptimization = (
32
+ config: BatteryOptimizationConfig = {},
33
+ callbacks: BatteryOptimizationCallbacks = {}
34
+ ) => {
35
+ const {
36
+ enableBatteryMonitoring = true,
37
+ enablePerformanceThrottling = true,
38
+ enableUserPreferences = true,
39
+ enableAdaptiveOptimization = true,
40
+ lowBatteryThreshold = 0.2,
41
+ criticalBatteryThreshold = 0.1,
42
+ userPreferenceLevel = 'balanced'
43
+ } = config
44
+
45
+ const [batteryStatus, setBatteryStatus] = useState<BatteryStatus>({
46
+ level: 1,
47
+ charging: false,
48
+ chargingTime: 0,
49
+ dischargingTime: 0,
50
+ isLow: false,
51
+ isCritical: false
52
+ })
53
+ const [isOptimizing, setIsOptimizing] = useState(false)
54
+ const [optimizations, setOptimizations] = useState<string[]>([])
55
+ const [currentThrottleLevel, setCurrentThrottleLevel] = useState<string>('none')
56
+ const [userPreferences, setUserPreferences] = useState<string>(userPreferenceLevel)
57
+
58
+ const batteryRef = useRef<any>(null)
59
+ const batteryCheckIntervalRef = useRef<number | null>(null)
60
+ const optimizationTimerRef = useRef<number | null>(null)
61
+
62
+ // Get battery status
63
+ const getBatteryStatus = useCallback(async (): Promise<BatteryStatus | null> => {
64
+ if (!enableBatteryMonitoring || !('getBattery' in navigator)) return null
65
+
66
+ try {
67
+ const battery = await (navigator as any).getBattery()
68
+ batteryRef.current = battery
69
+
70
+ const status: BatteryStatus = {
71
+ level: battery.level,
72
+ charging: battery.charging,
73
+ chargingTime: battery.chargingTime,
74
+ dischargingTime: battery.dischargingTime,
75
+ isLow: battery.level <= lowBatteryThreshold,
76
+ isCritical: battery.level <= criticalBatteryThreshold
77
+ }
78
+
79
+ setBatteryStatus(status)
80
+ callbacks.onBatteryStatusChange?.(status)
81
+
82
+ // Check for warnings
83
+ if (status.isCritical) {
84
+ callbacks.onCriticalBatteryWarning?.(status)
85
+ } else if (status.isLow) {
86
+ callbacks.onLowBatteryWarning?.(status)
87
+ }
88
+
89
+ return status
90
+ } catch (error) {
91
+ console.warn('Battery API not available:', error)
92
+ return null
93
+ }
94
+ }, [enableBatteryMonitoring, lowBatteryThreshold, criticalBatteryThreshold, callbacks])
95
+
96
+ // Apply performance throttling based on battery level
97
+ const applyPerformanceThrottling = useCallback((level: string) => {
98
+ if (!enablePerformanceThrottling) return
99
+
100
+ setIsOptimizing(true)
101
+ setCurrentThrottleLevel(level)
102
+
103
+ setTimeout(() => {
104
+ let optimizations: string[] = []
105
+
106
+ switch (level) {
107
+ case 'light':
108
+ optimizations = [
109
+ 'Light performance throttling applied',
110
+ 'Animation complexity reduced by 20%',
111
+ 'Hardware acceleration optimized for battery'
112
+ ]
113
+ break
114
+ case 'moderate':
115
+ optimizations = [
116
+ 'Moderate performance throttling applied',
117
+ 'Animation complexity reduced by 40%',
118
+ 'GPU utilization limited for battery efficiency',
119
+ 'Non-essential animations disabled'
120
+ ]
121
+ break
122
+ case 'aggressive':
123
+ optimizations = [
124
+ 'Aggressive performance throttling applied',
125
+ 'Animation complexity reduced by 70%',
126
+ 'Hardware acceleration minimized',
127
+ 'Only essential animations enabled',
128
+ 'Background processes optimized'
129
+ ]
130
+ break
131
+ default:
132
+ optimizations = ['Performance throttling disabled']
133
+ }
134
+
135
+ setOptimizations(prev => [...prev, ...optimizations])
136
+ setIsOptimizing(false)
137
+
138
+ callbacks.onPerformanceThrottled?.(level, 'Battery optimization')
139
+ optimizations.forEach(optimization => {
140
+ callbacks.onOptimizationApplied?.(optimization)
141
+ })
142
+ }, 300)
143
+ }, [enablePerformanceThrottling, callbacks])
144
+
145
+ // Apply battery optimizations
146
+ const applyBatteryOptimizations = useCallback(() => {
147
+ if (!enableAdaptiveOptimization) return
148
+
149
+ setIsOptimizing(true)
150
+
151
+ setTimeout(() => {
152
+ const optimizations: string[] = []
153
+
154
+ if (batteryStatus.isCritical) {
155
+ optimizations.push('Critical battery mode: Maximum power saving enabled')
156
+ optimizations.push('All non-essential features disabled')
157
+ optimizations.push('Hardware acceleration completely disabled')
158
+ applyPerformanceThrottling('aggressive')
159
+ } else if (batteryStatus.isLow) {
160
+ optimizations.push('Low battery mode: Enhanced power saving enabled')
161
+ optimizations.push('Performance throttling applied')
162
+ optimizations.push('Hardware acceleration optimized for battery')
163
+ applyPerformanceThrottling('moderate')
164
+ } else if (batteryStatus.level < 0.5) {
165
+ optimizations.push('Medium battery: Light optimization applied')
166
+ optimizations.push('Hardware acceleration optimized')
167
+ applyPerformanceThrottling('light')
168
+ } else {
169
+ optimizations.push('High battery: Performance mode enabled')
170
+ optimizations.push('Hardware acceleration at full capacity')
171
+ applyPerformanceThrottling('none')
172
+ }
173
+
174
+ setOptimizations(prev => [...prev, ...optimizations])
175
+ setIsOptimizing(false)
176
+
177
+ optimizations.forEach(optimization => {
178
+ callbacks.onOptimizationApplied?.(optimization)
179
+ })
180
+ }, 200)
181
+ }, [enableAdaptiveOptimization, batteryStatus, applyPerformanceThrottling, callbacks])
182
+
183
+ // Handle user preferences
184
+ const updateUserPreferences = useCallback((preference: string) => {
185
+ if (!enableUserPreferences) return
186
+
187
+ setUserPreferences(preference)
188
+
189
+ let optimizations: string[] = []
190
+
191
+ switch (preference) {
192
+ case 'performance':
193
+ optimizations = [
194
+ 'User preference: Performance mode enabled',
195
+ 'Hardware acceleration at maximum',
196
+ 'Battery optimization disabled'
197
+ ]
198
+ applyPerformanceThrottling('none')
199
+ break
200
+ case 'balanced':
201
+ optimizations = [
202
+ 'User preference: Balanced mode enabled',
203
+ 'Hardware acceleration optimized',
204
+ 'Battery and performance balanced'
205
+ ]
206
+ applyPerformanceThrottling('light')
207
+ break
208
+ case 'battery':
209
+ optimizations = [
210
+ 'User preference: Battery saver mode enabled',
211
+ 'Hardware acceleration optimized for battery',
212
+ 'Performance throttling applied'
213
+ ]
214
+ applyPerformanceThrottling('moderate')
215
+ break
216
+ }
217
+
218
+ setOptimizations(prev => [...prev, ...optimizations])
219
+ optimizations.forEach(optimization => {
220
+ callbacks.onOptimizationApplied?.(optimization)
221
+ })
222
+ }, [enableUserPreferences, applyPerformanceThrottling, callbacks])
223
+
224
+ // Get optimization recommendations
225
+ const getOptimizationRecommendations = useCallback(() => {
226
+ const recommendations = []
227
+
228
+ if (batteryStatus.isCritical) {
229
+ recommendations.push('Critical battery: Disable all non-essential features')
230
+ recommendations.push('Reduce screen brightness and disable animations')
231
+ recommendations.push('Close unnecessary applications and tabs')
232
+ } else if (batteryStatus.isLow) {
233
+ recommendations.push('Low battery: Enable battery saver mode')
234
+ recommendations.push('Reduce animation complexity and GPU usage')
235
+ recommendations.push('Optimize background processes')
236
+ } else if (batteryStatus.level < 0.5) {
237
+ recommendations.push('Medium battery: Consider light optimizations')
238
+ recommendations.push('Monitor hardware acceleration usage')
239
+ recommendations.push('Balance performance and battery life')
240
+ } else {
241
+ recommendations.push('High battery: Full performance available')
242
+ recommendations.push('Hardware acceleration at maximum capacity')
243
+ recommendations.push('Monitor for optimal performance')
244
+ }
245
+
246
+ return recommendations
247
+ }, [batteryStatus])
248
+
249
+ // Initialize battery monitoring
250
+ useEffect(() => {
251
+ if (!enableBatteryMonitoring) return
252
+
253
+ try {
254
+ // Get initial battery status
255
+ getBatteryStatus()
256
+
257
+ // Set up battery monitoring interval
258
+ batteryCheckIntervalRef.current = window.setInterval(getBatteryStatus, 10000) // Check every 10 seconds
259
+
260
+ return () => {
261
+ if (batteryCheckIntervalRef.current) {
262
+ clearInterval(batteryCheckIntervalRef.current)
263
+ }
264
+ }
265
+ } catch (error) {
266
+ console.warn('Battery optimization initialization failed:', error)
267
+ }
268
+ }, [enableBatteryMonitoring, getBatteryStatus])
269
+
270
+ // Apply optimizations when battery status changes
271
+ useEffect(() => {
272
+ if (enableAdaptiveOptimization) {
273
+ applyBatteryOptimizations()
274
+ }
275
+ }, [enableAdaptiveOptimization, applyBatteryOptimizations])
276
+
277
+ // Set initial user preferences
278
+ useEffect(() => {
279
+ if (enableUserPreferences) {
280
+ updateUserPreferences(userPreferenceLevel)
281
+ }
282
+ }, [enableUserPreferences, userPreferenceLevel, updateUserPreferences])
283
+
284
+ // Cleanup on unmount
285
+ useEffect(() => {
286
+ return () => {
287
+ if (batteryCheckIntervalRef.current) {
288
+ clearInterval(batteryCheckIntervalRef.current)
289
+ }
290
+ if (optimizationTimerRef.current) {
291
+ clearTimeout(optimizationTimerRef.current)
292
+ }
293
+ }
294
+ }, [])
295
+
296
+ return {
297
+ // State
298
+ batteryStatus,
299
+ isOptimizing,
300
+ optimizations,
301
+ currentThrottleLevel,
302
+ userPreferences,
303
+
304
+ // Actions
305
+ getBatteryStatus,
306
+ applyPerformanceThrottling,
307
+ applyBatteryOptimizations,
308
+ updateUserPreferences,
309
+
310
+ // Computed values
311
+ isLowBattery: batteryStatus.isLow,
312
+ isCriticalBattery: batteryStatus.isCritical,
313
+ batteryPercentage: Math.round(batteryStatus.level * 100),
314
+ optimizationRecommendations: getOptimizationRecommendations(),
315
+ isCharging: batteryStatus.charging,
316
+ estimatedTimeRemaining: batteryStatus.charging ?
317
+ batteryStatus.chargingTime : batteryStatus.dischargingTime,
318
+
319
+ // Utility functions
320
+ getBatteryHealth: () => {
321
+ if (batteryStatus.level >= 0.8) return 'excellent'
322
+ if (batteryStatus.level >= 0.6) return 'good'
323
+ if (batteryStatus.level >= 0.4) return 'fair'
324
+ if (batteryStatus.level >= 0.2) return 'poor'
325
+ return 'critical'
326
+ }
327
+ }
328
+ }
329
+
330
+ export default useBatteryOptimization
@@ -0,0 +1,299 @@
1
+ import { useState, useEffect, useCallback, useRef, useMemo } from 'react'
2
+
3
+ export interface BatteryMetrics {
4
+ level: number
5
+ charging: boolean
6
+ chargingTime: number
7
+ dischargingTime: number
8
+ isLowPowerMode: boolean
9
+ isCriticalBattery: boolean
10
+ performanceMode: 'high' | 'medium' | 'low'
11
+ throttlingEnabled: boolean
12
+ throttlingReason?: string
13
+ }
14
+
15
+ export interface BatteryThresholds {
16
+ low: number
17
+ critical: number
18
+ warning: number
19
+ }
20
+
21
+ export interface BatteryCallbacks {
22
+ onBatteryLow?: (level: number) => void
23
+ onBatteryCritical?: (level: number) => void
24
+ onChargingStateChange?: (charging: boolean) => void
25
+ onPerformanceModeChange?: (mode: BatteryMetrics['performanceMode']) => void
26
+ onThrottlingChange?: (enabled: boolean, reason?: string) => void
27
+ }
28
+
29
+ export interface BatteryOptions {
30
+ updateInterval?: number
31
+ thresholds?: Partial<BatteryThresholds>
32
+ enableThrottling?: boolean
33
+ callbacks?: BatteryCallbacks
34
+ }
35
+
36
+ const DEFAULT_THRESHOLDS: BatteryThresholds = {
37
+ low: 0.3,
38
+ critical: 0.15,
39
+ warning: 0.5
40
+ }
41
+
42
+ export const useBatteryStatus = (options: BatteryOptions = {}) => {
43
+ const {
44
+ updateInterval = 5000,
45
+ thresholds = {},
46
+ enableThrottling = true,
47
+ callbacks = {}
48
+ } = options
49
+
50
+ const [metrics, setMetrics] = useState<BatteryMetrics>({
51
+ level: 1,
52
+ charging: true,
53
+ chargingTime: 0,
54
+ dischargingTime: 0,
55
+ isLowPowerMode: false,
56
+ isCriticalBattery: false,
57
+ performanceMode: 'high',
58
+ throttlingEnabled: false
59
+ })
60
+
61
+ const [isMonitoring, setIsMonitoring] = useState(false)
62
+ const intervalIdRef = useRef<NodeJS.Timeout>()
63
+ const batteryRef = useRef<any>(null)
64
+
65
+ // Merge default thresholds with custom ones
66
+ const finalThresholds = useMemo(() => ({
67
+ ...DEFAULT_THRESHOLDS,
68
+ ...thresholds
69
+ }), [thresholds])
70
+
71
+ // Determine performance mode based on battery level
72
+ const determinePerformanceMode = useCallback((level: number, charging: boolean): BatteryMetrics['performanceMode'] => {
73
+ if (charging) return 'high'
74
+ if (level <= finalThresholds.critical) return 'low'
75
+ if (level <= finalThresholds.low) return 'medium'
76
+ return 'high'
77
+ }, [finalThresholds])
78
+
79
+ // Determine if throttling should be enabled
80
+ const shouldThrottle = useCallback((level: number, charging: boolean): { should: boolean; reason?: string } => {
81
+ if (!enableThrottling) return { should: false }
82
+
83
+ if (level <= finalThresholds.critical) {
84
+ return { should: true, reason: 'Critical battery level' }
85
+ }
86
+
87
+ if (level <= finalThresholds.low && !charging) {
88
+ return { should: true, reason: 'Low battery level' }
89
+ }
90
+
91
+ return { should: false }
92
+ }, [enableThrottling, finalThresholds])
93
+
94
+ // Update battery information
95
+ const updateBatteryInfo = useCallback(async () => {
96
+ if ('getBattery' in navigator) {
97
+ try {
98
+ if (!batteryRef.current) {
99
+ batteryRef.current = await (navigator as any).getBattery()
100
+ }
101
+
102
+ const battery = batteryRef.current
103
+ const level = battery.level
104
+ const charging = battery.charging
105
+ const chargingTime = battery.chargingTime
106
+ const dischargingTime = battery.dischargingTime
107
+
108
+ const isLowPowerMode = level < finalThresholds.low
109
+ const isCriticalBattery = level < finalThresholds.critical
110
+
111
+ // Determine performance mode
112
+ const performanceMode = determinePerformanceMode(level, charging)
113
+
114
+ // Determine throttling
115
+ const { should: shouldThrottleNow, reason } = shouldThrottle(level, charging)
116
+
117
+ const newMetrics: BatteryMetrics = {
118
+ level,
119
+ charging,
120
+ chargingTime,
121
+ dischargingTime,
122
+ isLowPowerMode,
123
+ isCriticalBattery,
124
+ performanceMode,
125
+ throttlingEnabled: shouldThrottleNow,
126
+ throttlingReason: reason
127
+ }
128
+
129
+ setMetrics(newMetrics)
130
+
131
+ // Check thresholds and call callbacks
132
+ if (level <= finalThresholds.critical && callbacks.onBatteryCritical) {
133
+ callbacks.onBatteryCritical(level)
134
+ } else if (level <= finalThresholds.low && callbacks.onBatteryLow) {
135
+ callbacks.onBatteryLow(level)
136
+ }
137
+
138
+ if (charging !== metrics.charging && callbacks.onChargingStateChange) {
139
+ callbacks.onChargingStateChange(charging)
140
+ }
141
+
142
+ if (performanceMode !== metrics.performanceMode && callbacks.onPerformanceModeChange) {
143
+ callbacks.onPerformanceModeChange(performanceMode)
144
+ }
145
+
146
+ if (shouldThrottleNow !== metrics.throttlingEnabled && callbacks.onThrottlingChange) {
147
+ callbacks.onThrottlingChange(shouldThrottleNow, reason)
148
+ }
149
+
150
+ } catch (error) {
151
+ console.warn('Battery API not available:', error)
152
+ }
153
+ }
154
+ }, [finalThresholds, callbacks, metrics.charging, metrics.performanceMode, metrics.throttlingEnabled, determinePerformanceMode, shouldThrottle])
155
+
156
+ // Start monitoring
157
+ const startMonitoring = useCallback(() => {
158
+ if (isMonitoring) return
159
+
160
+ setIsMonitoring(true)
161
+
162
+ // Initial battery check
163
+ updateBatteryInfo()
164
+
165
+ // Set up interval for battery updates
166
+ intervalIdRef.current = setInterval(updateBatteryInfo, updateInterval)
167
+ }, [isMonitoring, updateBatteryInfo, updateInterval])
168
+
169
+ // Stop monitoring
170
+ const stopMonitoring = useCallback(() => {
171
+ setIsMonitoring(false)
172
+
173
+ if (intervalIdRef.current) {
174
+ clearInterval(intervalIdRef.current)
175
+ }
176
+ }, [])
177
+
178
+ // Manual throttling controls
179
+ const enableThrottlingMode = useCallback((reason?: string) => {
180
+ setMetrics(prev => ({
181
+ ...prev,
182
+ throttlingEnabled: true,
183
+ throttlingReason: reason || 'Manual override'
184
+ }))
185
+ }, [])
186
+
187
+ const disableThrottlingMode = useCallback(() => {
188
+ setMetrics(prev => ({
189
+ ...prev,
190
+ throttlingEnabled: false,
191
+ throttlingReason: undefined
192
+ }))
193
+ }, [])
194
+
195
+ // Auto-start monitoring on mount
196
+ useEffect(() => {
197
+ startMonitoring()
198
+ return () => stopMonitoring()
199
+ }, [startMonitoring, stopMonitoring])
200
+
201
+ // Battery optimization recommendations
202
+ const getOptimizationRecommendations = useCallback(() => {
203
+ const recommendations: string[] = []
204
+
205
+ if (metrics.isCriticalBattery) {
206
+ recommendations.push('Critical battery level - enable maximum power saving mode')
207
+ recommendations.push('Reduce animation complexity and disable non-essential features')
208
+ recommendations.push('Consider switching to low-power theme')
209
+ } else if (metrics.isLowPowerMode) {
210
+ recommendations.push('Low battery detected - enable power saving optimizations')
211
+ recommendations.push('Reduce background processing and network requests')
212
+ recommendations.push('Optimize rendering performance')
213
+ }
214
+
215
+ if (metrics.throttlingEnabled) {
216
+ recommendations.push('Performance throttling active - adjust user experience accordingly')
217
+ }
218
+
219
+ if (!metrics.charging && metrics.level < 0.5) {
220
+ recommendations.push('Battery below 50% - consider enabling power saving features')
221
+ }
222
+
223
+ return recommendations
224
+ }, [metrics])
225
+
226
+ // Power saving suggestions
227
+ const getPowerSavingSuggestions = useCallback(() => {
228
+ const suggestions: string[] = []
229
+
230
+ if (metrics.isLowPowerMode) {
231
+ suggestions.push('Reduce animation frame rate')
232
+ suggestions.push('Implement lazy loading for images and content')
233
+ suggestions.push('Use CSS transforms instead of JavaScript animations')
234
+ suggestions.push('Optimize network requests and caching')
235
+ suggestions.push('Consider using Intersection Observer for scroll-based animations')
236
+ }
237
+
238
+ if (metrics.isCriticalBattery) {
239
+ suggestions.push('Disable all non-essential animations')
240
+ suggestions.push('Implement aggressive content virtualization')
241
+ suggestions.push('Reduce DOM manipulation frequency')
242
+ suggestions.push('Use passive event listeners')
243
+ }
244
+
245
+ return suggestions
246
+ }, [metrics])
247
+
248
+ // Battery status summary
249
+ const getBatteryStatus = useCallback(() => {
250
+ if (metrics.charging) {
251
+ return `Charging (${Math.round(metrics.level * 100)}%)`
252
+ }
253
+
254
+ if (metrics.isCriticalBattery) {
255
+ return `Critical (${Math.round(metrics.level * 100)}%)`
256
+ }
257
+
258
+ if (metrics.isLowPowerMode) {
259
+ return `Low (${Math.round(metrics.level * 100)}%)`
260
+ }
261
+
262
+ return `Good (${Math.round(metrics.level * 100)}%)`
263
+ }, [metrics])
264
+
265
+ return {
266
+ // Metrics
267
+ metrics,
268
+
269
+ // Controls
270
+ startMonitoring,
271
+ stopMonitoring,
272
+ isMonitoring,
273
+ enableThrottlingMode,
274
+ disableThrottlingMode,
275
+
276
+ // Status
277
+ batteryLevel: metrics.level,
278
+ isCharging: metrics.charging,
279
+ isLowPowerMode: metrics.isLowPowerMode,
280
+ isCriticalBattery: metrics.isCriticalBattery,
281
+ performanceMode: metrics.performanceMode,
282
+ throttlingEnabled: metrics.throttlingEnabled,
283
+ throttlingReason: metrics.throttlingReason,
284
+
285
+ // Optimization
286
+ getOptimizationRecommendations,
287
+ getPowerSavingSuggestions,
288
+ getBatteryStatus,
289
+
290
+ // Raw values
291
+ level: metrics.level,
292
+ charging: metrics.charging,
293
+ chargingTime: metrics.chargingTime,
294
+ dischargingTime: metrics.dischargingTime,
295
+
296
+ // Thresholds
297
+ thresholds: finalThresholds
298
+ }
299
+ }