UNPKG

@llamaindex/ui

Version:

A comprehensive UI component library built with React, TypeScript, and Tailwind CSS for LlamaIndex applications

134 lines (128 loc) 4.21 kB
import { useCloudApiClient, useProject } from './chunk-FBUZYPY4.mjs'; import { __spreadProps, __spreadValues } from './chunk-FWCSY2DS.mjs'; import { useMemo, useState, useEffect, useCallback } from 'react'; import { create } from 'zustand'; import { getPipelineApiV1PipelinesPipelineIdGet, searchPipelinesApiV1PipelinesGet } from 'llama-cloud-services/api'; async function fetchPipelines(params) { var _a, _b, _c; const resp = await searchPipelinesApiV1PipelinesGet({ query: { organization_id: (_a = params == null ? void 0 : params.organizationId) != null ? _a : void 0, project_id: (_b = params == null ? void 0 : params.projectId) != null ? _b : void 0 } }); if (resp.error) throw resp.error; return (_c = resp.data) != null ? _c : []; } async function getPipeline(id, params) { var _a, _b; const resp = await getPipelineApiV1PipelinesPipelineIdGet({ path: { pipeline_id: id }, query: { organization_id: (_a = params == null ? void 0 : params.organizationId) != null ? _a : void 0, project_id: (_b = params == null ? void 0 : params.projectId) != null ? _b : void 0 } }); if (resp.error) throw resp.error; if (!resp.data) throw new Error("Pipeline not found"); return resp.data; } // src/indexes/store/index-store.ts var createIndexStore = (_client, options) => create()((set) => ({ indexes: {}, sync: async () => { var _a, _b; try { const { id } = (_b = (_a = options == null ? void 0 : options.getProject) == null ? void 0 : _a.call(options)) != null ? _b : {}; const list = await fetchPipelines({ projectId: id }); set({ indexes: Object.fromEntries(list.map((i) => [i.id, i])) }); } catch (error) { console.error("Failed to sync indexes:", error); } }, refresh: async (id) => { var _a, _b; try { const { id: projectId } = (_b = (_a = options == null ? void 0 : options.getProject) == null ? void 0 : _a.call(options)) != null ? _b : {}; const pipeline = await getPipeline(id, { projectId }); set((state) => ({ indexes: __spreadProps(__spreadValues({}, state.indexes), { [id]: pipeline }) })); } catch (error) { console.error("Failed to refresh index:", id, error); } } })); // src/indexes/hooks/use-index-store.ts var globalStore = null; function useIndexStore(selector) { const client = useCloudApiClient(); const project = useProject(); const store = useMemo(() => { if (!globalStore) { globalStore = createIndexStore(client, { getProject: () => project }); } return globalStore; }, [client, project]); return selector ? store(selector) : store(); } // src/indexes/hooks/use-index-list.ts function useIndexList() { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const store = useIndexStore(); const record = store.indexes; const syncStore = store.sync; useEffect(() => { async function run() { setLoading(true); setError(null); try { await syncStore(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to load indexes"); } finally { setLoading(false); } } run(); }, [syncStore]); const indexes = useMemo(() => Object.values(record), [record]); return { indexes, loading, error, sync: syncStore }; } function useIndex(id) { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const get = useIndexStore(); const record = get.indexes; const refreshStore = get.refresh; const index = useMemo(() => { var _a; return (_a = record[id]) != null ? _a : null; }, [record, id]); const refresh = useCallback(async () => { setLoading(true); setError(null); try { await refreshStore(id); } catch (err) { setError(err instanceof Error ? err.message : "Failed to refresh index"); } finally { setLoading(false); } }, [id, refreshStore]); useEffect(() => { if (!index) { void refresh(); } }, [index, refresh]); return { index, loading, error, refresh }; } export { useIndex, useIndexList, useIndexStore };