@llamaindex/ui
Version:
A comprehensive UI component library built with React, TypeScript, and Tailwind CSS for LlamaIndex applications
138 lines (131 loc) • 4.29 kB
JavaScript
;
var chunkSJGLR4O2_js = require('./chunk-SJGLR4O2.js');
var chunk4E3IDRQJ_js = require('./chunk-4E3IDRQJ.js');
var react = require('react');
var zustand = require('zustand');
var api = require('llama-cloud-services/api');
async function fetchPipelines(params) {
var _a, _b, _c;
const resp = await api.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 api.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) => zustand.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: chunk4E3IDRQJ_js.__spreadProps(chunk4E3IDRQJ_js.__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 = chunkSJGLR4O2_js.useCloudApiClient();
const project = chunkSJGLR4O2_js.useProject();
const store = react.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] = react.useState(true);
const [error, setError] = react.useState(null);
const store = useIndexStore();
const record = store.indexes;
const syncStore = store.sync;
react.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 = react.useMemo(() => Object.values(record), [record]);
return {
indexes,
loading,
error,
sync: syncStore
};
}
function useIndex(id) {
const [loading, setLoading] = react.useState(false);
const [error, setError] = react.useState(null);
const get = useIndexStore();
const record = get.indexes;
const refreshStore = get.refresh;
const index = react.useMemo(() => {
var _a;
return (_a = record[id]) != null ? _a : null;
}, [record, id]);
const refresh = react.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]);
react.useEffect(() => {
if (!index) {
void refresh();
}
}, [index, refresh]);
return { index, loading, error, refresh };
}
exports.useIndex = useIndex;
exports.useIndexList = useIndexList;
exports.useIndexStore = useIndexStore;