UNPKG

@spacelift-io/backstage-integration-frontend

Version:

Backstage plugin for integrating Spacelift.io with Backstage

43 lines (40 loc) 1.12 kB
import { useApi } from '@backstage/core-plugin-api'; import { useState, useCallback, useEffect } from 'react'; import { spaceliftApiRef } from '../api/SpaceliftApiClient.esm.js'; import { POLL_INTERVAL } from '../constants.esm.js'; const useFetchStacks = () => { const spaceliftApi = useApi(spaceliftApiRef); const [stacks, setStacks] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const fetchStacks = useCallback(async () => { try { const response = await spaceliftApi.getStacks(); setStacks(response); setError(null); } catch (err) { setError(err); setStacks([]); } finally { setLoading(false); } }, [spaceliftApi]); useEffect(() => { const poll = setInterval(() => { fetchStacks(); }, POLL_INTERVAL); fetchStacks(); return () => { clearInterval(poll); }; }, [fetchStacks]); return { stacks, loading, error, retry: fetchStacks, clear: () => setError(null) }; }; export { useFetchStacks }; //# sourceMappingURL=useFetchStacks.esm.js.map