animetix-web / frontend /src /features /graph /useGraphData.ts
MissawB's picture
Upload folder using huggingface_hub (part 4)
df2e14d verified
Raw
History Blame Contribute Delete
1.07 kB
import { useState, useEffect } from 'react';
import { getGraphNeighborhood } from '../../api';
import { GraphData } from '../../types';
export function useGraphData(id: string, type: string, depth: number) {
const [data, setData] = useState<GraphData>({ nodes: [], links: [] });
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
let isMounted = true;
async function fetchData() {
if (!id || !type) return;
setIsLoading(true);
setError(null);
try {
const result = await getGraphNeighborhood(id, type, depth);
if (isMounted) {
setData(result);
}
} catch (err) {
if (isMounted) {
setError(err instanceof Error ? err : new Error(String(err)));
}
} finally {
if (isMounted) {
setIsLoading(false);
}
}
}
fetchData();
return () => {
isMounted = false;
};
}, [id, type, depth]);
return { data, isLoading, error };
}