Spaces:
Sleeping
Sleeping
File size: 1,775 Bytes
3786a3f feca495 3786a3f feca495 3786a3f f14eb69 feca495 f14eb69 feca495 f14eb69 feca495 3786a3f feca495 3786a3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import { useEffect, useState } from 'react';
import api from '@/lib/axios';
import { useGraphStore, GraphData } from '@/store/graph';
import { useDocumentsStore } from '@/store/documents';
function mapResponse(data: any): GraphData {
return {
nodes: (data.nodes || []).map((n: any) => ({
id: String(n.id ?? n.name),
name: n.name,
type: n.type,
description: n.description,
sourceDoc: n.source_doc,
val: n.val ?? n.connections ?? 1,
community: n.community,
})),
links: (data.links || data.edges || []).map((l: any) => ({
source: String(l.source),
target: String(l.target),
type: l.type,
confidence: l.confidence,
description: l.description,
sourceDoc: l.source_doc,
})),
};
}
export function useGraphData() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const setData = useGraphStore((s) => s.setData);
const selectedIds = useDocumentsStore((s) => s.selectedDocumentIds);
const selectedIdsStr = selectedIds.join(',');
useEffect(() => {
let cancelled = false;
setLoading(true);
const url = selectedIdsStr ? `/graph/?document_ids=${selectedIdsStr}` : '/graph/';
api.get(url)
.then(({ data }) => {
if (!cancelled) {
setData(mapResponse(data));
setError(null);
}
})
.catch(() => {
if (!cancelled) {
setError('Backend unavailable. Please check your connection.');
setData({ nodes: [], links: [] });
}
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [setData, selectedIdsStr]);
return { loading, error };
}
|