UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

30 lines (29 loc) 903 B
import { useCallback, useEffect, useState } from "react"; import { elkConverter } from "../converters/eklConverter"; export const useElk = (config, elk, args) => { const [result, setResult] = useState(null); const [isLoading, setIsLoading] = useState(true); const layout = useCallback(() => { return elk.layout(config, args); }, [elk, config, args]); useEffect(() => { let isCancelled = false; layout() .then((data) => { if (isCancelled) return; setResult(elkConverter(data)); setIsLoading(false); }) .catch((error) => { if (!isCancelled) { args?.onError(error); setIsLoading(false); } }); return () => { isCancelled = true; }; }, [layout]); return { result, isLoading }; };