Spaces:
Sleeping
Sleeping
| /** | |
| * Custom hook — wraps the /api/recommend call with loading + error state. | |
| */ | |
| import { useState, useCallback } from "react"; | |
| export function useRecommend() { | |
| const [data, setData] = useState(null); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState(null); | |
| const fetchRecommendations = useCallback(async (profile) => { | |
| setLoading(true); | |
| setError(null); | |
| setData(null); | |
| try { | |
| const res = await fetch("/api/recommend", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify(profile), | |
| }); | |
| if (!res.ok) { | |
| const body = await res.json().catch(() => ({})); | |
| throw new Error(body.error || `Server returned ${res.status}`); | |
| } | |
| const result = await res.json(); | |
| setData(result); | |
| return result; | |
| } catch (err) { | |
| setError(err.message); | |
| return null; | |
| } finally { | |
| setLoading(false); | |
| } | |
| }, []); | |
| return { data, loading, error, fetchRecommendations }; | |
| } | |