@epam/ai-dial-chat-hooks 1.3.0-dev.294 → 1.3.0-dev.304

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,182 +1,199 @@
1
- import { useCallback as e, useEffect as t, useRef as n, useState as r } from "react";
1
+ import { useCallback as e, useEffect as t, useMemo as n, useRef as r, useState as i } from "react";
2
2
  //#region src/project/use-project-conversations.ts
3
- var i = {
4
- items: [],
3
+ var a = 20, o = 100, s = {
4
+ rows: [],
5
+ visibleCount: a,
5
6
  isLoading: !0,
6
7
  hasError: !1,
7
- hasMore: !1,
8
8
  isLoadingMore: !1
9
- }, a = {
10
- ...i,
9
+ }, c = {
10
+ ...s,
11
11
  isLoading: !1
12
- }, o = 50, s = 100, c = (e, t) => e.map(({ id: e, title: n, author: r, updatedAt: i, isUnread: a }) => {
13
- let o = a;
12
+ }, l = (e, t) => e.map(({ id: e, name: n, title: r, author: i, updatedAt: a, isUnread: o, isTitlePending: s }) => {
13
+ let c = o;
14
14
  if (t.has(e)) {
15
15
  let n = t.get(e);
16
- i != null && n != null && i > n ? t.delete(e) : o = !1;
16
+ a != null && n != null && a > n ? t.delete(e) : c = !1;
17
17
  }
18
18
  return {
19
19
  id: e,
20
- title: n,
21
- authorName: r,
22
- ...o ? { isUnread: !0 } : {}
20
+ name: n,
21
+ title: r,
22
+ authorName: i,
23
+ isTitlePending: s === !0,
24
+ ...c ? { isUnread: !0 } : {}
23
25
  };
24
- }), l = (l, u) => {
25
- let [d, f] = r(i), [p, m] = r(0), h = n(null), g = n(null), _ = n(0), v = n(/* @__PURE__ */ new Map()), y = n(/* @__PURE__ */ new Map()), b = n(d);
26
+ }), u = (e, t) => {
27
+ if (t.length === 0) return e;
28
+ let n = new Map(t.map((e) => [e.id, e]));
29
+ return e.map((e) => n.get(e.id) ?? e);
30
+ }, d = ({ id: e, title: t, authorName: n, isUnread: r }) => ({
31
+ id: e,
32
+ title: t,
33
+ authorName: n,
34
+ ...r ? { isUnread: !0 } : {}
35
+ }), f = (f, p) => {
36
+ let [m, h] = i(s), [g, _] = i(0), v = r(null), y = r(null), b = r(!1), x = r(/* @__PURE__ */ new Map()), S = r(/* @__PURE__ */ new Map()), C = r(m);
26
37
  t(() => {
27
- b.current = d;
38
+ C.current = m;
28
39
  });
29
- let x = e((e) => {
30
- for (let { id: t, updatedAt: n } of e) y.current.set(t, n);
31
- }, []);
40
+ let w = e((e) => {
41
+ for (let { id: t, updatedAt: n } of e) S.current.set(t, n);
42
+ }, []), T = e(async (e, t) => {
43
+ if (p == null) return [];
44
+ let n = e.filter((e) => e.isTitlePending).map((e) => e.name), r = [];
45
+ for (let e = 0; e < n.length; e += o) {
46
+ let { items: i } = await f.getProjectConversationTitles(p, n.slice(e, e + o), t);
47
+ r.push(...i);
48
+ }
49
+ return w(r), l(r, x.current);
50
+ }, [
51
+ f,
52
+ p,
53
+ w
54
+ ]), E = e(async (e, t, n) => {
55
+ try {
56
+ let r = await T(e.slice(0, t), n);
57
+ if (n.aborted) return;
58
+ h((e) => ({
59
+ ...e,
60
+ rows: u(e.rows, r)
61
+ }));
62
+ } catch {}
63
+ }, [T]);
32
64
  t(() => {
33
- if (v.current = /* @__PURE__ */ new Map(), y.current = /* @__PURE__ */ new Map(), u == null) {
34
- f(a);
65
+ if (x.current = /* @__PURE__ */ new Map(), S.current = /* @__PURE__ */ new Map(), p == null) {
66
+ h(c);
35
67
  return;
36
68
  }
37
69
  let e = new AbortController();
38
- return h.current = e, (async () => {
39
- f((e) => ({
70
+ return v.current = e, (async () => {
71
+ h((e) => ({
40
72
  ...e,
41
73
  isLoading: !0,
42
74
  hasError: !1,
43
75
  isLoadingMore: !1
44
76
  }));
45
77
  try {
46
- let t = await l.listProjectConversations(u, e.signal);
78
+ let { items: t } = await f.listProjectConversations(p, e.signal);
47
79
  if (e.signal.aborted) return;
48
- x(t.items), f({
49
- items: c(t.items, v.current),
50
- isLoading: !1,
51
- hasError: !1,
52
- hasMore: t.hasMore,
53
- isLoadingMore: !1
80
+ w(t), h({
81
+ ...c,
82
+ rows: l(t, x.current)
54
83
  });
55
84
  } catch {
56
85
  if (e.signal.aborted) return;
57
- f({
58
- ...a,
86
+ h({
87
+ ...c,
59
88
  hasError: !0
60
89
  });
61
90
  }
62
91
  })(), () => {
63
- e.abort(), g.current?.abort();
92
+ e.abort(), y.current?.abort();
64
93
  };
65
94
  }, [
66
- l,
67
- u,
95
+ f,
68
96
  p,
69
- x
97
+ g,
98
+ w
70
99
  ]);
71
- let S = e(() => m((e) => e + 1), []), C = e(async () => {
72
- let e = h.current;
73
- if (u == null || e == null) return;
74
- let t = _.current;
75
- f((e) => ({
100
+ let D = e(() => _((e) => e + 1), []), O = e(async () => {
101
+ let e = v.current, t = C.current;
102
+ if (p == null || e == null || b.current) return;
103
+ b.current = !0;
104
+ let n = t.visibleCount + a, r = t.rows.slice(t.visibleCount, n);
105
+ h((e) => ({
76
106
  ...e,
77
107
  isLoadingMore: !0
78
108
  }));
79
109
  try {
80
- let n = await l.listProjectConversations(u, e.signal, { offset: d.items.length });
110
+ let t = await T(r, e.signal);
81
111
  if (e.signal.aborted) return;
82
- if (t !== _.current) {
83
- f((e) => ({
84
- ...e,
85
- isLoadingMore: !1
86
- }));
87
- return;
88
- }
89
- x(n.items), f((e) => {
90
- let t = new Set(e.items.map((e) => e.id));
91
- return {
92
- ...e,
93
- items: [...e.items, ...c(n.items, v.current).filter((e) => !t.has(e.id))],
94
- hasMore: n.hasMore,
95
- isLoadingMore: !1
96
- };
97
- });
112
+ h((e) => ({
113
+ ...e,
114
+ rows: u(e.rows, t),
115
+ visibleCount: Math.max(e.visibleCount, n),
116
+ isLoadingMore: !1
117
+ }));
98
118
  } catch (t) {
99
119
  if (e.signal.aborted) return;
100
- throw f((e) => ({
120
+ throw h((e) => ({
101
121
  ...e,
102
122
  isLoadingMore: !1
103
123
  })), t;
124
+ } finally {
125
+ b.current = !1;
104
126
  }
105
- }, [
106
- l,
107
- u,
108
- x,
109
- d.items.length
110
- ]), w = e(async () => {
111
- let e = b.current;
112
- if (u == null || e.isLoading || e.hasError) return;
113
- g.current?.abort();
127
+ }, [p, T]), k = e(async () => {
128
+ let e = C.current;
129
+ if (p == null || e.isLoading || e.hasError) return;
130
+ y.current?.abort();
114
131
  let t = new AbortController();
115
- g.current = t, _.current += 1;
116
- let n = Math.max(e.items.length, o), r = [], i = !0;
132
+ y.current = t;
133
+ let n;
117
134
  try {
118
- for (; r.length < n && i;) {
119
- let e = await l.listProjectConversations(u, t.signal, {
120
- offset: r.length,
121
- limit: Math.min(s, n - r.length)
122
- });
123
- if (t.signal.aborted) return;
124
- if (r.push(...e.items), i = e.hasMore, e.items.length === 0) break;
125
- }
135
+ let { items: e } = await f.listProjectConversations(p, t.signal);
136
+ if (t.signal.aborted) return;
137
+ w(e), n = l(e, x.current);
126
138
  } catch {
127
139
  return;
128
140
  }
129
- if (t.signal.aborted) return;
130
- x(r);
131
- let a = /* @__PURE__ */ new Set(), d = c(r, v.current).filter((e) => !a.has(e.id) && (a.add(e.id), !0));
132
- f((e) => ({
141
+ h((e) => ({
133
142
  ...e,
134
- items: d,
135
- hasMore: i,
136
- isLoadingMore: !1
137
- }));
143
+ rows: n
144
+ })), await E(n, C.current.visibleCount, t.signal);
138
145
  }, [
139
- l,
140
- u,
141
- x
146
+ f,
147
+ E,
148
+ p,
149
+ w
142
150
  ]);
143
- t(() => () => g.current?.abort(), []);
144
- let T = e((e) => {
145
- v.current.set(e, y.current.get(e)), f((t) => ({
151
+ t(() => () => y.current?.abort(), []);
152
+ let A = e((e) => {
153
+ x.current.set(e, S.current.get(e)), h((t) => ({
146
154
  ...t,
147
- items: t.items.map((t) => t.id === e && t.isUnread ? {
155
+ rows: t.rows.map((t) => t.id === e && t.isUnread ? {
148
156
  ...t,
149
157
  isUnread: !1
150
158
  } : t)
151
159
  })), (async () => {
152
160
  try {
153
- await l.markProjectConversationViewed?.(e);
161
+ await f.markProjectConversationViewed?.(e);
154
162
  } catch {}
155
163
  })();
156
- }, [l]), E = e(async (e, t) => {
157
- let { name: n } = await l.renameProjectConversation(e, t);
158
- f((t) => ({
164
+ }, [f]), j = e(async (e, t) => {
165
+ let { name: n } = await f.renameProjectConversation(e, t);
166
+ h((t) => ({
159
167
  ...t,
160
- items: t.items.map((t) => t.id === e ? {
168
+ rows: t.rows.map((t) => t.id === e ? {
161
169
  ...t,
162
- title: n
170
+ title: n,
171
+ isTitlePending: !1
163
172
  } : t)
164
173
  }));
165
- }, [l]), D = e(async (e) => {
166
- await l.deleteProjectConversation(e), f((t) => ({
174
+ }, [f]), M = e(async (e) => {
175
+ await f.deleteProjectConversation(e);
176
+ let { rows: t, visibleCount: n } = C.current, r = t.filter((t) => t.id !== e);
177
+ h((t) => ({
167
178
  ...t,
168
- items: t.items.filter((t) => t.id !== e)
179
+ rows: t.rows.filter((t) => t.id !== e)
169
180
  }));
170
- }, [l]);
181
+ let i = v.current;
182
+ i != null && E(r, n, i.signal);
183
+ }, [f, E]), { rows: N, visibleCount: P, isLoading: F, hasError: I, isLoadingMore: L } = m;
171
184
  return {
172
- ...d,
173
- retry: S,
174
- loadMore: C,
175
- rename: E,
176
- remove: D,
177
- refresh: w,
178
- markViewed: T
185
+ items: n(() => N.slice(0, P).map(d), [N, P]),
186
+ isLoading: F,
187
+ hasError: I,
188
+ hasMore: P < N.length,
189
+ isLoadingMore: L,
190
+ retry: D,
191
+ loadMore: O,
192
+ rename: j,
193
+ remove: M,
194
+ refresh: k,
195
+ markViewed: A
179
196
  };
180
197
  };
181
198
  //#endregion
182
- export { l as useProjectConversations };
199
+ export { f as useProjectConversations };