UNPKG

@progress/kendo-react-grid

Version:

React Data Grid (Table) provides 100+ ready-to-use data grid features. KendoReact Grid package

158 lines (157 loc) 4.37 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2026 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ import * as a from "react"; import { handleAIResponse as S } from "../utils/handleAIResponse.mjs"; const B = (t) => { t !== void 0 && typeof console != "undefined" && typeof console.warn == "function" && console.warn( "Grid AI Assistant requestOptions.cancelToken is not supported by the default fetch transport. Use requestOptions.signal or provide a custom httpClient." ); }, D = (t, s) => { if (!s || s === t) return t; const r = new AbortController(), e = (l) => { const i = l.reason; r.signal.aborted || r.abort(i); }; return t.aborted && e(t), s.aborted && e(s), r.signal.aborted || (t.addEventListener("abort", () => e(t), { once: !0 }), s.addEventListener("abort", () => e(s), { once: !0 })), r.signal; }, F = (t) => { const s = {}; return t.forEach((r, e) => { s[e] = r; }), s; }; function z(t) { return { request: async (s, r) => { const e = await t(s, r); return { data: e.data, status: e.status, statusText: e.statusText, headers: e.headers }; } }; } function K(t) { const { requestUrl: s, requestOptions: r, httpClient: e, role: l = "user", columns: i = [], gridState: h, gridRef: m, onPromptRequest: g, onResponseSuccess: b, onResponseError: q, onStateChange: C, onMessages: T, onExportPdf: R } = t, [H, f] = a.useState(!1), [j, p] = a.useState(!1), w = a.useRef(null), A = a.useCallback( (c, u, d) => { if (p(!1), f(!1), h && m) { const o = S(c, h, m); C && C(o.state), o.shouldExportPdf && R && R(), o.messages.length > 0 && T && T(o.messages, u, d); } b && b(c, u, d); }, [h, m, C, R, T, b] ), O = a.useCallback( (c) => { p(!1), f(!1), q && q(c); }, [q] ), L = a.useCallback( (c, u) => { const d = new AbortController(), o = { role: l || "user", columns: i || [], headers: { Accept: "*/*", "Content-Type": "application/json" }, promptMessage: c, requestOptions: { url: s, method: "POST", withCredentials: !1, responseType: "json", signal: d.signal, ...r } }; if (g && g(o, u), o.requestOptions.url !== void 0) { p(!0), f(!0); const { url: k, cancelToken: E, signal: U, headers: G, ...y } = o.requestOptions, x = D(d.signal, U), v = { ...G, ...o.headers }; if (k) { const I = { columns: o.columns, contents: [{ text: o.promptMessage }] }; e ? e.request(k, { ...y, cancelToken: E, headers: v, signal: x, data: I }).then( (n) => A(n, o.promptMessage, u) ).catch(O) : (B(E), fetch(k, { method: y.method || "POST", headers: v, credentials: y.withCredentials ? "include" : "same-origin", body: JSON.stringify(I), signal: x }).then(async (n) => { if (!n.ok) { const P = new Error(n.statusText); throw P.response = { status: n.status, statusText: n.statusText }, P; } return { data: await n.json(), status: n.status, statusText: n.statusText, headers: F(n.headers) }; }).then( (n) => A(n, o.promptMessage, u) ).catch(O)), w.current = d; } } }, [ l, i, s, r, e, g, A, O ] ), M = a.useCallback(() => { w.current && w.current.abort("Request cancelled by user."), p(!1), f(!1); }, []); return { loading: H, streaming: j, sendRequest: L, cancelRequest: M }; } export { z as createAxiosAIClient, K as useGridAIRequest };