Spaces:
Running
Running
Download algospaced-ui/src/hooks/useSystemStats.ts from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/hooks/useSystemStats.ts
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/algospaced-ui/src/hooks/useSystemStats.ts
-
curl -L -o useSystemStats.ts https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/hooks/useSystemStats.ts
5.14 kB
| import { useState, useEffect, useCallback } from 'react'; | |
| import { supabase, type AuthUser } from '../firebaseClient'; | |
| import type { StreakInfo } from '../types/windowTypes'; | |
| import { syncWithBackendJourneyProgress } from '../utils/companionService'; | |
| export function useSystemStats(user: AuthUser | null, onOpenSyncWindow?: () => void) { | |
| const [time, setTime] = useState(''); | |
| const [streak, setStreak] = useState<StreakInfo | null>(null); | |
| const [isMobile, setIsMobile] = useState(false); | |
| const [isFullscreen, setIsFullscreen] = useState(false); | |
| const [syncing, setSyncing] = useState(false); | |
| const [syncResult, setSyncResult] = useState<string | null>(null); | |
| // Clock Updater | |
| useEffect(() => { | |
| const updateTime = () => { | |
| const date = new Date(); | |
| let hours = date.getHours(); | |
| const minutes = date.getMinutes(); | |
| const ampm = hours >= 12 ? 'PM' : 'AM'; | |
| hours = hours % 12; | |
| hours = hours ? hours : 12; | |
| const minStr = minutes < 10 ? '0' + minutes : String(minutes); | |
| setTime(`${hours}:${minStr} ${ampm}`); | |
| }; | |
| updateTime(); | |
| const interval = setInterval(updateTime, 30000); | |
| return () => clearInterval(interval); | |
| }, []); | |
| // Fetch Streak Info | |
| const fetchStreak = useCallback(async () => { | |
| try { | |
| const { data: { session } } = await supabase.auth.getSession(); | |
| if (!session) return; | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/streak`, { | |
| headers: { 'Authorization': `Bearer ${session.access_token}` } | |
| }); | |
| if (res.ok) { | |
| const data: StreakInfo = await res.json(); | |
| setStreak(data); | |
| } | |
| } catch (err) { | |
| console.error('Error fetching streak:', err); | |
| } | |
| }, []); | |
| // Sync on user change | |
| useEffect(() => { | |
| if (user) { | |
| const timer = setTimeout(() => { | |
| void fetchStreak(); | |
| }, 0); | |
| syncWithBackendJourneyProgress(); | |
| return () => clearTimeout(timer); | |
| } | |
| }, [user, fetchStreak]); | |
| // Mobile viewport detection | |
| useEffect(() => { | |
| const checkMobile = () => { | |
| setIsMobile(window.innerWidth < 768); | |
| }; | |
| checkMobile(); | |
| window.addEventListener('resize', checkMobile); | |
| return () => { | |
| window.removeEventListener('resize', checkMobile); | |
| }; | |
| }, []); | |
| // Fullscreen change listener | |
| useEffect(() => { | |
| const handleFullscreenChange = () => { | |
| setIsFullscreen(!!document.fullscreenElement); | |
| }; | |
| document.addEventListener('fullscreenchange', handleFullscreenChange); | |
| return () => { | |
| document.removeEventListener('fullscreenchange', handleFullscreenChange); | |
| }; | |
| }, []); | |
| // Zoom prevention listener | |
| useEffect(() => { | |
| const handleWheel = (e: WheelEvent) => { | |
| if (e.ctrlKey) { | |
| e.preventDefault(); | |
| } | |
| }; | |
| const handleKeydown = (e: KeyboardEvent) => { | |
| if (e.ctrlKey && ( | |
| e.key === '=' || | |
| e.key === '-' || | |
| e.key === '0' || | |
| e.key === '+' || | |
| e.code === 'NumpadAdd' || | |
| e.code === 'NumpadSubtract' | |
| )) { | |
| e.preventDefault(); | |
| } | |
| }; | |
| const handleTouchmove = (e: TouchEvent) => { | |
| if (e.touches.length > 1) { | |
| e.preventDefault(); | |
| } | |
| }; | |
| document.addEventListener('wheel', handleWheel, { passive: false }); | |
| document.addEventListener('keydown', handleKeydown); | |
| document.addEventListener('touchmove', handleTouchmove, { passive: false }); | |
| return () => { | |
| document.removeEventListener('wheel', handleWheel); | |
| document.removeEventListener('keydown', handleKeydown); | |
| document.removeEventListener('touchmove', handleTouchmove); | |
| }; | |
| }, []); | |
| const toggleFullscreen = useCallback(() => { | |
| if (!document.fullscreenElement) { | |
| document.documentElement.requestFullscreen().catch((err: Error) => { | |
| console.error(`Error enabling fullscreen: ${err.message}`); | |
| }); | |
| } else { | |
| document.exitFullscreen().catch((err: Error) => { | |
| console.error(`Error exiting fullscreen: ${err.message}`); | |
| }); | |
| } | |
| }, []); | |
| const triggerSync = useCallback(async () => { | |
| if (onOpenSyncWindow) { | |
| onOpenSyncWindow(); | |
| } | |
| setSyncing(true); | |
| setSyncResult(null); | |
| try { | |
| const { data: { session } } = await supabase.auth.getSession(); | |
| if (!session) return; | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/sync`, { | |
| method: 'POST', | |
| headers: { 'Authorization': `Bearer ${session.access_token}` } | |
| }); | |
| if (res.ok) { | |
| const data = await res.json(); | |
| setSyncResult(data.message || 'Synchronization successfully completed!'); | |
| void fetchStreak(); | |
| syncWithBackendJourneyProgress(); | |
| } else { | |
| setSyncResult('Sync operation failed at server host.'); | |
| } | |
| } catch { | |
| setSyncResult('Cloud synchronization server unreachable.'); | |
| } finally { | |
| setSyncing(false); | |
| } | |
| }, [onOpenSyncWindow, fetchStreak]); | |
| return { | |
| time, | |
| streak, | |
| fetchStreak, | |
| isMobile, | |
| isFullscreen, | |
| toggleFullscreen, | |
| syncing, | |
| syncResult, | |
| triggerSync | |
| }; | |
| } | |