(slug: string)
| 3 | import type { ProblemDetail } from '../types/problem'; |
| 4 | |
| 5 | export const useProblem = (slug: string) => { |
| 6 | const [problem, setProblem] = useState<ProblemDetail | null>(null); |
| 7 | const [isLoading, setIsLoading] = useState<boolean>(true); |
| 8 | const [error, setError] = useState<string | null>(null); |
| 9 | |
| 10 | useEffect(() => { |
| 11 | const fetchProblem = async () => { |
| 12 | try { |
| 13 | setIsLoading(true); |
| 14 | setError(null); |
| 15 | const data = await getProblemBySlug(slug); |
| 16 | setProblem(data); |
| 17 | } catch (err: any) { |
| 18 | setError(err.message || 'Failed to fetch problem details.'); |
| 19 | } finally { |
| 20 | setIsLoading(false); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | if (slug) { |
| 25 | fetchProblem(); |
| 26 | } |
| 27 | }, [slug]); |
| 28 | |
| 29 | return { problem, isLoading, error }; |
| 30 | }; |
no test coverage detected