Download src/components/mode-selection/useDocDoeMode.ts from asnannp/DocDoeAI: direct link, hf CLI and curl.
- Browser
- Download file 3.01 kB
-
https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/components/mode-selection/useDocDoeMode.ts
- Command line
-
hf download hf://spaces/asnannp/DocDoeAI/src/components/mode-selection/useDocDoeMode.ts
-
curl -L -o useDocDoeMode.ts https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/components/mode-selection/useDocDoeMode.ts
3.01 kB
| "use client"; | |
| /** | |
| * useDocDoeMode Hook | |
| * React hook for managing DocDoe mode state | |
| * Persists mode in localStorage | |
| * Allows mode switching anytime | |
| */ | |
| import { useState, useEffect } from "react"; | |
| import { useRouter } from "next/navigation"; | |
| import { | |
| type DocDoeMode, | |
| getMode, | |
| setMode, | |
| getLastQuery, | |
| setLastQuery, | |
| getLastExamDate, | |
| setLastExamDate, | |
| getLastDuration, | |
| setLastDuration, | |
| } from "./modeStorage"; | |
| export interface UseDocDoeModeReturn { | |
| mode: DocDoeMode | null; | |
| setMode: (mode: DocDoeMode) => void; | |
| switchMode: (mode: DocDoeMode) => void; | |
| lastQuery: string | null; | |
| setLastQuery: (query: string) => void; | |
| lastExamDate: string | null; | |
| setLastExamDate: (date: string) => void; | |
| lastDuration: number | null; | |
| setLastDuration: (duration: number) => void; | |
| isLoaded: boolean; | |
| } | |
| export function useDocDoeMode(): UseDocDoeModeReturn { | |
| const router = useRouter(); | |
| const [mode, setModeState] = useState<DocDoeMode | null>(null); | |
| const [lastQuery, setLastQueryState] = useState<string | null>(null); | |
| const [lastExamDate, setLastExamDateState] = useState<string | null>(null); | |
| const [lastDuration, setLastDurationState] = useState<number | null>(null); | |
| const [isLoaded, setIsLoaded] = useState(false); | |
| // Load mode from localStorage on mount | |
| useEffect(() => { | |
| const timeoutId = window.setTimeout(() => { | |
| setModeState(getMode()); | |
| setLastQueryState(getLastQuery()); | |
| setLastExamDateState(getLastExamDate()); | |
| setLastDurationState(getLastDuration()); | |
| setIsLoaded(true); | |
| }, 0); | |
| return () => window.clearTimeout(timeoutId); | |
| }, []); | |
| // Switch mode and navigate to appropriate route | |
| const switchMode = (newMode: DocDoeMode) => { | |
| setMode(newMode); | |
| setModeState(newMode); | |
| // Navigate to the appropriate route | |
| switch (newMode) { | |
| case "tuition": | |
| router.push("/tuition"); | |
| break; | |
| case "first-hour": | |
| router.push("/first-hour"); | |
| break; | |
| case "study-chat": | |
| router.push("/study-chat"); | |
| break; | |
| case "learn-anything": | |
| router.push("/learn-anything"); | |
| break; | |
| case "exam-coach": | |
| router.push("/exam-coach"); | |
| break; | |
| case "home": | |
| router.push("/study-chat"); | |
| break; | |
| } | |
| }; | |
| const updateMode = (newMode: DocDoeMode) => { | |
| setMode(newMode); | |
| setModeState(newMode); | |
| }; | |
| const updateLastQuery = (query: string) => { | |
| setLastQuery(query); | |
| setLastQueryState(query); | |
| }; | |
| const updateLastExamDate = (date: string) => { | |
| setLastExamDate(date); | |
| setLastExamDateState(date); | |
| }; | |
| const updateLastDuration = (duration: number) => { | |
| setLastDuration(duration); | |
| setLastDurationState(duration); | |
| }; | |
| return { | |
| mode, | |
| setMode: updateMode, | |
| switchMode, | |
| lastQuery, | |
| setLastQuery: updateLastQuery, | |
| lastExamDate, | |
| setLastExamDate: updateLastExamDate, | |
| lastDuration, | |
| setLastDuration: updateLastDuration, | |
| isLoaded, | |
| }; | |
| } | |