@btst/stack 2.7.0 → 2.8.0

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.
Files changed (181) hide show
  1. package/README.md +1 -0
  2. package/dist/packages/stack/src/plugins/blog/client/components/loading/post-navigation-skeleton.cjs +13 -0
  3. package/dist/packages/stack/src/plugins/blog/client/components/loading/post-navigation-skeleton.mjs +11 -0
  4. package/dist/packages/stack/src/plugins/blog/client/components/loading/recent-posts-carousel-skeleton.cjs +17 -0
  5. package/dist/packages/stack/src/plugins/blog/client/components/loading/recent-posts-carousel-skeleton.mjs +15 -0
  6. package/dist/packages/stack/src/plugins/blog/client/components/pages/post-page.internal.cjs +18 -7
  7. package/dist/packages/stack/src/plugins/blog/client/components/pages/post-page.internal.mjs +18 -7
  8. package/dist/packages/stack/src/plugins/blog/client/components/shared/post-navigation.cjs +48 -52
  9. package/dist/packages/stack/src/plugins/blog/client/components/shared/post-navigation.mjs +49 -53
  10. package/dist/packages/stack/src/plugins/blog/client/components/shared/recent-posts-carousel.cjs +34 -37
  11. package/dist/packages/stack/src/plugins/blog/client/components/shared/recent-posts-carousel.mjs +35 -38
  12. package/dist/packages/stack/src/plugins/blog/client/hooks/blog-hooks.cjs +4 -21
  13. package/dist/packages/stack/src/plugins/blog/client/hooks/blog-hooks.mjs +4 -21
  14. package/dist/packages/stack/src/plugins/comments/api/getters.cjs +284 -0
  15. package/dist/packages/stack/src/plugins/comments/api/getters.mjs +280 -0
  16. package/dist/packages/stack/src/plugins/comments/api/mutations.cjs +118 -0
  17. package/dist/packages/stack/src/plugins/comments/api/mutations.mjs +112 -0
  18. package/dist/packages/stack/src/plugins/comments/api/plugin.cjs +335 -0
  19. package/dist/packages/stack/src/plugins/comments/api/plugin.mjs +333 -0
  20. package/dist/packages/stack/src/plugins/comments/api/query-key-defs.cjs +60 -0
  21. package/dist/packages/stack/src/plugins/comments/api/query-key-defs.mjs +55 -0
  22. package/dist/packages/stack/src/plugins/comments/api/serializers.cjs +23 -0
  23. package/dist/packages/stack/src/plugins/comments/api/serializers.mjs +21 -0
  24. package/dist/packages/stack/src/plugins/comments/client/components/comment-count.cjs +46 -0
  25. package/dist/packages/stack/src/plugins/comments/client/components/comment-count.mjs +44 -0
  26. package/dist/packages/stack/src/plugins/comments/client/components/comment-form.cjs +86 -0
  27. package/dist/packages/stack/src/plugins/comments/client/components/comment-form.mjs +84 -0
  28. package/dist/packages/stack/src/plugins/comments/client/components/comment-thread.cjs +540 -0
  29. package/dist/packages/stack/src/plugins/comments/client/components/comment-thread.mjs +538 -0
  30. package/dist/packages/stack/src/plugins/comments/client/components/pages/moderation-page.cjs +64 -0
  31. package/dist/packages/stack/src/plugins/comments/client/components/pages/moderation-page.internal.cjs +426 -0
  32. package/dist/packages/stack/src/plugins/comments/client/components/pages/moderation-page.internal.mjs +424 -0
  33. package/dist/packages/stack/src/plugins/comments/client/components/pages/moderation-page.mjs +62 -0
  34. package/dist/packages/stack/src/plugins/comments/client/components/pages/my-comments-page.cjs +66 -0
  35. package/dist/packages/stack/src/plugins/comments/client/components/pages/my-comments-page.internal.cjs +256 -0
  36. package/dist/packages/stack/src/plugins/comments/client/components/pages/my-comments-page.internal.mjs +254 -0
  37. package/dist/packages/stack/src/plugins/comments/client/components/pages/my-comments-page.mjs +64 -0
  38. package/dist/packages/stack/src/plugins/comments/client/components/pages/resource-comments-page.cjs +86 -0
  39. package/dist/packages/stack/src/plugins/comments/client/components/pages/resource-comments-page.internal.cjs +191 -0
  40. package/dist/packages/stack/src/plugins/comments/client/components/pages/resource-comments-page.internal.mjs +189 -0
  41. package/dist/packages/stack/src/plugins/comments/client/components/pages/resource-comments-page.mjs +84 -0
  42. package/dist/packages/stack/src/plugins/comments/client/components/shared/page-wrapper.cjs +27 -0
  43. package/dist/packages/stack/src/plugins/comments/client/components/shared/page-wrapper.mjs +25 -0
  44. package/dist/packages/stack/src/plugins/comments/client/components/shared/pagination.cjs +37 -0
  45. package/dist/packages/stack/src/plugins/comments/client/components/shared/pagination.mjs +35 -0
  46. package/dist/packages/stack/src/plugins/comments/client/hooks/use-comments.cjs +476 -0
  47. package/dist/packages/stack/src/plugins/comments/client/hooks/use-comments.mjs +464 -0
  48. package/dist/packages/stack/src/plugins/comments/client/localization/comments-moderation.cjs +67 -0
  49. package/dist/packages/stack/src/plugins/comments/client/localization/comments-moderation.mjs +65 -0
  50. package/dist/packages/stack/src/plugins/comments/client/localization/comments-my.cjs +27 -0
  51. package/dist/packages/stack/src/plugins/comments/client/localization/comments-my.mjs +25 -0
  52. package/dist/packages/stack/src/plugins/comments/client/localization/comments-thread.cjs +30 -0
  53. package/dist/packages/stack/src/plugins/comments/client/localization/comments-thread.mjs +28 -0
  54. package/dist/packages/stack/src/plugins/comments/client/localization/index.cjs +13 -0
  55. package/dist/packages/stack/src/plugins/comments/client/localization/index.mjs +11 -0
  56. package/dist/packages/stack/src/plugins/comments/client/plugin.cjs +116 -0
  57. package/dist/packages/stack/src/plugins/comments/client/plugin.mjs +114 -0
  58. package/dist/packages/stack/src/plugins/comments/client/utils.cjs +41 -0
  59. package/dist/packages/stack/src/plugins/comments/client/utils.mjs +37 -0
  60. package/dist/packages/stack/src/plugins/comments/db.cjs +75 -0
  61. package/dist/packages/stack/src/plugins/comments/db.mjs +73 -0
  62. package/dist/packages/stack/src/plugins/comments/schemas.cjs +45 -0
  63. package/dist/packages/stack/src/plugins/comments/schemas.mjs +38 -0
  64. package/dist/packages/stack/src/plugins/kanban/client/components/forms/task-form.cjs +0 -1
  65. package/dist/packages/stack/src/plugins/kanban/client/components/forms/task-form.mjs +0 -1
  66. package/dist/packages/stack/src/plugins/kanban/client/components/pages/board-page.internal.cjs +39 -22
  67. package/dist/packages/stack/src/plugins/kanban/client/components/pages/board-page.internal.mjs +40 -23
  68. package/dist/packages/ui/src/components/avatar.mjs +1 -1
  69. package/dist/packages/ui/src/components/pagination-controls.cjs +64 -0
  70. package/dist/packages/ui/src/components/pagination-controls.mjs +62 -0
  71. package/dist/packages/ui/src/components/when-visible.cjs +39 -0
  72. package/dist/packages/ui/src/components/when-visible.mjs +37 -0
  73. package/dist/plugins/blog/client/hooks/index.d.cts +1 -1
  74. package/dist/plugins/blog/client/hooks/index.d.mts +1 -1
  75. package/dist/plugins/blog/client/hooks/index.d.ts +1 -1
  76. package/dist/plugins/blog/client/index.d.cts +24 -2
  77. package/dist/plugins/blog/client/index.d.mts +24 -2
  78. package/dist/plugins/blog/client/index.d.ts +24 -2
  79. package/dist/plugins/comments/api/index.cjs +21 -0
  80. package/dist/plugins/comments/api/index.d.cts +126 -0
  81. package/dist/plugins/comments/api/index.d.mts +126 -0
  82. package/dist/plugins/comments/api/index.d.ts +126 -0
  83. package/dist/plugins/comments/api/index.mjs +5 -0
  84. package/dist/plugins/comments/client/components/index.cjs +15 -0
  85. package/dist/plugins/comments/client/components/index.d.cts +125 -0
  86. package/dist/plugins/comments/client/components/index.d.mts +125 -0
  87. package/dist/plugins/comments/client/components/index.d.ts +125 -0
  88. package/dist/plugins/comments/client/components/index.mjs +5 -0
  89. package/dist/plugins/comments/client/hooks/index.cjs +17 -0
  90. package/dist/plugins/comments/client/hooks/index.d.cts +200 -0
  91. package/dist/plugins/comments/client/hooks/index.d.mts +200 -0
  92. package/dist/plugins/comments/client/hooks/index.d.ts +200 -0
  93. package/dist/plugins/comments/client/hooks/index.mjs +1 -0
  94. package/dist/plugins/comments/client/index.cjs +9 -0
  95. package/dist/plugins/comments/client/index.d.cts +262 -0
  96. package/dist/plugins/comments/client/index.d.mts +262 -0
  97. package/dist/plugins/comments/client/index.d.ts +262 -0
  98. package/dist/plugins/comments/client/index.mjs +2 -0
  99. package/dist/plugins/comments/client.css +2 -0
  100. package/dist/plugins/comments/query-keys.cjs +113 -0
  101. package/dist/plugins/comments/query-keys.d.cts +71 -0
  102. package/dist/plugins/comments/query-keys.d.mts +71 -0
  103. package/dist/plugins/comments/query-keys.d.ts +71 -0
  104. package/dist/plugins/comments/query-keys.mjs +111 -0
  105. package/dist/plugins/comments/style.css +15 -0
  106. package/dist/plugins/kanban/api/index.d.cts +1 -1
  107. package/dist/plugins/kanban/api/index.d.mts +1 -1
  108. package/dist/plugins/kanban/api/index.d.ts +1 -1
  109. package/dist/plugins/kanban/client/hooks/index.d.cts +1 -1
  110. package/dist/plugins/kanban/client/hooks/index.d.mts +1 -1
  111. package/dist/plugins/kanban/client/hooks/index.d.ts +1 -1
  112. package/dist/plugins/kanban/client/index.d.cts +1 -1
  113. package/dist/plugins/kanban/client/index.d.mts +1 -1
  114. package/dist/plugins/kanban/client/index.d.ts +1 -1
  115. package/dist/plugins/kanban/query-keys.d.cts +1 -1
  116. package/dist/plugins/kanban/query-keys.d.mts +1 -1
  117. package/dist/plugins/kanban/query-keys.d.ts +1 -1
  118. package/dist/shared/{stack.FeaWkglm.d.ts → stack.BxFl46lB.d.cts} +24 -1
  119. package/dist/shared/stack.C-b3Sn8j.d.cts +142 -0
  120. package/dist/shared/stack.C-b3Sn8j.d.mts +142 -0
  121. package/dist/shared/stack.C-b3Sn8j.d.ts +142 -0
  122. package/dist/shared/stack.CJE9sAjV.d.ts +335 -0
  123. package/dist/shared/stack.CmHRdhl8.d.cts +335 -0
  124. package/dist/shared/{stack.CNLHlv7r.d.mts → stack.DOZ1EXjM.d.mts} +6 -12
  125. package/dist/shared/{stack.FeaWkglm.d.mts → stack.DRpeDS6X.d.ts} +24 -1
  126. package/dist/shared/{stack.CQAZwXhV.d.cts → stack.DX-tQ93o.d.cts} +6 -12
  127. package/dist/shared/stack.Dcz6636A.d.mts +335 -0
  128. package/dist/shared/{stack.FeaWkglm.d.cts → stack.Jb0kQDJC.d.mts} +24 -1
  129. package/dist/shared/stack.Ldfkr5b2.d.cts +112 -0
  130. package/dist/shared/stack.Ldfkr5b2.d.mts +112 -0
  131. package/dist/shared/stack.Ldfkr5b2.d.ts +112 -0
  132. package/dist/shared/{stack.D3BsrpAz.d.ts → stack.VF6FhyZw.d.ts} +6 -12
  133. package/package.json +67 -2
  134. package/src/plugins/blog/client/components/loading/post-navigation-skeleton.tsx +10 -0
  135. package/src/plugins/blog/client/components/loading/recent-posts-carousel-skeleton.tsx +18 -0
  136. package/src/plugins/blog/client/components/pages/post-page.internal.tsx +23 -8
  137. package/src/plugins/blog/client/components/shared/post-navigation.tsx +0 -5
  138. package/src/plugins/blog/client/components/shared/recent-posts-carousel.tsx +1 -5
  139. package/src/plugins/blog/client/hooks/blog-hooks.tsx +8 -33
  140. package/src/plugins/blog/client/overrides.ts +26 -1
  141. package/src/plugins/cms/client/components/shared/pagination.tsx +14 -42
  142. package/src/plugins/comments/api/getters.ts +444 -0
  143. package/src/plugins/comments/api/index.ts +21 -0
  144. package/src/plugins/comments/api/mutations.ts +206 -0
  145. package/src/plugins/comments/api/plugin.ts +628 -0
  146. package/src/plugins/comments/api/query-key-defs.ts +143 -0
  147. package/src/plugins/comments/api/serializers.ts +37 -0
  148. package/src/plugins/comments/client/components/comment-count.tsx +66 -0
  149. package/src/plugins/comments/client/components/comment-form.tsx +112 -0
  150. package/src/plugins/comments/client/components/comment-thread.tsx +799 -0
  151. package/src/plugins/comments/client/components/index.tsx +11 -0
  152. package/src/plugins/comments/client/components/pages/moderation-page.internal.tsx +550 -0
  153. package/src/plugins/comments/client/components/pages/moderation-page.tsx +70 -0
  154. package/src/plugins/comments/client/components/pages/my-comments-page.internal.tsx +367 -0
  155. package/src/plugins/comments/client/components/pages/my-comments-page.tsx +72 -0
  156. package/src/plugins/comments/client/components/pages/resource-comments-page.internal.tsx +225 -0
  157. package/src/plugins/comments/client/components/pages/resource-comments-page.tsx +97 -0
  158. package/src/plugins/comments/client/components/shared/page-wrapper.tsx +32 -0
  159. package/src/plugins/comments/client/components/shared/pagination.tsx +44 -0
  160. package/src/plugins/comments/client/hooks/index.tsx +13 -0
  161. package/src/plugins/comments/client/hooks/use-comments.tsx +717 -0
  162. package/src/plugins/comments/client/index.ts +14 -0
  163. package/src/plugins/comments/client/localization/comments-moderation.ts +75 -0
  164. package/src/plugins/comments/client/localization/comments-my.ts +32 -0
  165. package/src/plugins/comments/client/localization/comments-thread.ts +32 -0
  166. package/src/plugins/comments/client/localization/index.ts +11 -0
  167. package/src/plugins/comments/client/overrides.ts +164 -0
  168. package/src/plugins/comments/client/plugin.tsx +195 -0
  169. package/src/plugins/comments/client/utils.ts +67 -0
  170. package/src/plugins/comments/client.css +2 -0
  171. package/src/plugins/comments/db.ts +77 -0
  172. package/src/plugins/comments/query-keys.ts +189 -0
  173. package/src/plugins/comments/schemas.ts +72 -0
  174. package/src/plugins/comments/style.css +15 -0
  175. package/src/plugins/comments/types.ts +73 -0
  176. package/src/plugins/kanban/client/components/forms/task-form.tsx +0 -1
  177. package/src/plugins/kanban/client/components/pages/board-page.internal.tsx +46 -27
  178. package/src/plugins/kanban/client/overrides.ts +27 -1
  179. package/dist/shared/{stack.Rtcvl8sS.d.cts → stack.BOokfhZD.d.cts} +3 -3
  180. package/dist/shared/{stack.D4Cea8II.d.ts → stack.BvCR4-9H.d.ts} +3 -3
  181. package/dist/shared/{stack.HE_IvqV5.d.mts → stack.CWxAl9K3.d.mts} +3 -3
@@ -0,0 +1,476 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ const reactQuery = require('@tanstack/react-query');
5
+ const client = require('@btst/stack/plugins/client');
6
+ const plugins_comments_queryKeys = require('../../../../../../../plugins/comments/query-keys.cjs');
7
+ const utils = require('../utils.cjs');
8
+
9
+ function getClient(config) {
10
+ return client.createApiClient({
11
+ baseURL: config.apiBaseURL,
12
+ basePath: config.apiBasePath
13
+ });
14
+ }
15
+ function useComments(config, params, options) {
16
+ const client = getClient(config);
17
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
18
+ const query = reactQuery.useQuery({
19
+ ...queries.comments.list(params),
20
+ staleTime: 3e4,
21
+ retry: false,
22
+ enabled: options?.enabled ?? true
23
+ });
24
+ return {
25
+ data: query.data,
26
+ comments: query.data?.items ?? [],
27
+ total: query.data?.total ?? 0,
28
+ isLoading: query.isLoading,
29
+ isFetching: query.isFetching,
30
+ error: query.error,
31
+ refetch: query.refetch
32
+ };
33
+ }
34
+ function useSuspenseComments(config, params) {
35
+ const client = getClient(config);
36
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
37
+ const { data, refetch, error, isFetching } = reactQuery.useSuspenseQuery({
38
+ ...queries.comments.list(params),
39
+ staleTime: 3e4,
40
+ retry: false
41
+ });
42
+ if (error && !isFetching) {
43
+ throw error;
44
+ }
45
+ return {
46
+ comments: data?.items ?? [],
47
+ total: data?.total ?? 0,
48
+ refetch
49
+ };
50
+ }
51
+ function useSuspenseModerationComments(config, params) {
52
+ const limit = params.limit ?? 20;
53
+ const page = params.page ?? 1;
54
+ const offset = (page - 1) * limit;
55
+ const client = getClient(config);
56
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
57
+ const { data, refetch, error, isFetching } = reactQuery.useSuspenseQuery({
58
+ ...queries.comments.list({ status: params.status, limit, offset }),
59
+ staleTime: 3e4,
60
+ retry: false
61
+ });
62
+ if (error && !isFetching) {
63
+ throw error;
64
+ }
65
+ const comments = data?.items ?? [];
66
+ const total = data?.total ?? 0;
67
+ const totalPages = Math.max(1, Math.ceil(total / limit));
68
+ return {
69
+ comments,
70
+ total,
71
+ limit,
72
+ offset,
73
+ totalPages,
74
+ refetch
75
+ };
76
+ }
77
+ function useInfiniteComments(config, params, options) {
78
+ const pageSize = params.pageSize ?? 10;
79
+ const client = getClient(config);
80
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
81
+ const baseQuery = queries.commentsThread.list({
82
+ resourceId: params.resourceId,
83
+ resourceType: params.resourceType,
84
+ parentId: params.parentId ?? null,
85
+ status: params.status,
86
+ currentUserId: params.currentUserId,
87
+ limit: pageSize
88
+ });
89
+ const query = reactQuery.useInfiniteQuery({
90
+ ...baseQuery,
91
+ initialPageParam: 0,
92
+ getNextPageParam: (lastPage) => {
93
+ const nextOffset = lastPage.offset + lastPage.limit;
94
+ return nextOffset < lastPage.total ? nextOffset : void 0;
95
+ },
96
+ staleTime: 3e4,
97
+ retry: false,
98
+ enabled: options?.enabled ?? true
99
+ });
100
+ const comments = query.data?.pages.flatMap((p) => p.items) ?? [];
101
+ const total = query.data?.pages[0]?.total ?? 0;
102
+ return {
103
+ comments,
104
+ total,
105
+ queryKey: baseQuery.queryKey,
106
+ isLoading: query.isLoading,
107
+ isFetching: query.isFetching,
108
+ loadMore: query.fetchNextPage,
109
+ hasMore: !!query.hasNextPage,
110
+ isLoadingMore: query.isFetchingNextPage,
111
+ error: query.error
112
+ };
113
+ }
114
+ function useCommentCount(config, params) {
115
+ const client = getClient(config);
116
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
117
+ const query = reactQuery.useQuery({
118
+ ...queries.commentCount.byResource(params),
119
+ staleTime: 6e4,
120
+ retry: false
121
+ });
122
+ return {
123
+ count: query.data ?? 0,
124
+ isLoading: query.isLoading,
125
+ error: query.error
126
+ };
127
+ }
128
+ function usePostComment(config, params) {
129
+ const queryClient = reactQuery.useQueryClient();
130
+ const client = getClient(config);
131
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
132
+ const getListKey = (parentId, offset, limit) => {
133
+ if (params.infiniteKey && (parentId ?? null) === null) {
134
+ return params.infiniteKey;
135
+ }
136
+ return queries.comments.list({
137
+ resourceId: params.resourceId,
138
+ resourceType: params.resourceType,
139
+ parentId: parentId ?? null,
140
+ status: "approved",
141
+ currentUserId: params.currentUserId,
142
+ limit,
143
+ offset
144
+ }).queryKey;
145
+ };
146
+ const isInfinitePost = (parentId) => !!params.infiniteKey && (parentId ?? null) === null;
147
+ return reactQuery.useMutation({
148
+ mutationFn: async (input) => {
149
+ const response = await client("@post/comments", {
150
+ method: "POST",
151
+ body: {
152
+ resourceId: params.resourceId,
153
+ resourceType: params.resourceType,
154
+ parentId: input.parentId ?? null,
155
+ body: input.body
156
+ },
157
+ headers: config.headers
158
+ });
159
+ const data = response.data;
160
+ if (!data) throw utils.toError(response.error);
161
+ return data;
162
+ },
163
+ onMutate: async (input) => {
164
+ const listKey = getListKey(input.parentId, input.offset, input.limit);
165
+ await queryClient.cancelQueries({ queryKey: listKey });
166
+ const optimisticId = `optimistic-${Date.now()}`;
167
+ const optimistic = {
168
+ id: optimisticId,
169
+ resourceId: params.resourceId,
170
+ resourceType: params.resourceType,
171
+ parentId: input.parentId ?? null,
172
+ authorId: params.currentUserId ?? "",
173
+ resolvedAuthorName: "You",
174
+ resolvedAvatarUrl: null,
175
+ body: input.body,
176
+ status: "pending",
177
+ likes: 0,
178
+ isLikedByCurrentUser: false,
179
+ editedAt: null,
180
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
181
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
182
+ replyCount: 0
183
+ };
184
+ if (isInfinitePost(input.parentId)) {
185
+ const previous2 = queryClient.getQueryData(listKey);
186
+ queryClient.setQueryData(
187
+ listKey,
188
+ (old) => {
189
+ if (!old) {
190
+ return {
191
+ pages: [
192
+ {
193
+ items: [optimistic],
194
+ total: 1,
195
+ limit: params.pageSize ?? 10,
196
+ offset: 0
197
+ }
198
+ ],
199
+ pageParams: [0]
200
+ };
201
+ }
202
+ const lastIdx = old.pages.length - 1;
203
+ return {
204
+ ...old,
205
+ // Increment `total` on every page so the header count (which reads
206
+ // pages[0].total) stays in sync even after multiple pages are loaded.
207
+ pages: old.pages.map(
208
+ (page, idx) => idx === lastIdx ? {
209
+ ...page,
210
+ items: [...page.items, optimistic],
211
+ total: page.total + 1
212
+ } : { ...page, total: page.total + 1 }
213
+ )
214
+ };
215
+ }
216
+ );
217
+ return { previous: previous2, isInfinite: true, listKey, optimisticId };
218
+ }
219
+ const previous = queryClient.getQueryData(listKey);
220
+ queryClient.setQueryData(listKey, (old) => {
221
+ if (!old) {
222
+ return { items: [optimistic], total: 1, limit: 20, offset: 0 };
223
+ }
224
+ return {
225
+ ...old,
226
+ items: [...old.items, optimistic],
227
+ total: old.total + 1
228
+ };
229
+ });
230
+ return { previous, isInfinite: false, listKey, optimisticId };
231
+ },
232
+ onSuccess: (data, _input, context) => {
233
+ if (!context) return;
234
+ if (context.isInfinite) {
235
+ queryClient.setQueryData(
236
+ context.listKey,
237
+ (old) => {
238
+ if (!old) {
239
+ return {
240
+ pages: [
241
+ {
242
+ items: [data],
243
+ total: 1,
244
+ limit: _input.limit ?? params.pageSize ?? 10,
245
+ offset: _input.offset ?? 0
246
+ }
247
+ ],
248
+ pageParams: [_input.offset ?? 0]
249
+ };
250
+ }
251
+ return {
252
+ ...old,
253
+ pages: old.pages.map((page) => ({
254
+ ...page,
255
+ items: page.items.map(
256
+ (item) => item.id === context.optimisticId ? data : item
257
+ )
258
+ }))
259
+ };
260
+ }
261
+ );
262
+ } else {
263
+ queryClient.setQueryData(context.listKey, (old) => {
264
+ if (!old) {
265
+ return {
266
+ items: [data],
267
+ total: 1,
268
+ limit: _input.limit ?? params.pageSize ?? 20,
269
+ offset: _input.offset ?? 0
270
+ };
271
+ }
272
+ return {
273
+ ...old,
274
+ items: old.items.map(
275
+ (item) => item.id === context.optimisticId ? data : item
276
+ )
277
+ };
278
+ });
279
+ }
280
+ },
281
+ onError: (_err, _input, context) => {
282
+ if (!context) return;
283
+ queryClient.setQueryData(context.listKey, context.previous);
284
+ }
285
+ });
286
+ }
287
+ function useUpdateComment(config) {
288
+ const queryClient = reactQuery.useQueryClient();
289
+ const client = getClient(config);
290
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
291
+ return reactQuery.useMutation({
292
+ mutationFn: async (input) => {
293
+ const response = await client("@patch/comments/:id", {
294
+ method: "PATCH",
295
+ params: { id: input.id },
296
+ body: { body: input.body },
297
+ headers: config.headers
298
+ });
299
+ const data = response.data;
300
+ if (!data) throw utils.toError(response.error);
301
+ return data;
302
+ },
303
+ onSettled: () => {
304
+ queryClient.invalidateQueries({
305
+ queryKey: queries.comments.list._def
306
+ });
307
+ queryClient.invalidateQueries({ queryKey: ["commentsThread"] });
308
+ }
309
+ });
310
+ }
311
+ function useApproveComment(config) {
312
+ const queryClient = reactQuery.useQueryClient();
313
+ const client = getClient(config);
314
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
315
+ return reactQuery.useMutation({
316
+ mutationFn: async (id) => {
317
+ const response = await client("@patch/comments/:id/status", {
318
+ method: "PATCH",
319
+ params: { id },
320
+ body: { status: "approved" },
321
+ headers: config.headers
322
+ });
323
+ const data = response.data;
324
+ if (!data) throw utils.toError(response.error);
325
+ return data;
326
+ },
327
+ onSettled: () => {
328
+ queryClient.invalidateQueries({
329
+ queryKey: queries.comments.list._def
330
+ });
331
+ queryClient.invalidateQueries({
332
+ queryKey: queries.commentCount.byResource._def
333
+ });
334
+ queryClient.invalidateQueries({ queryKey: ["commentsThread"] });
335
+ }
336
+ });
337
+ }
338
+ function useUpdateCommentStatus(config) {
339
+ const queryClient = reactQuery.useQueryClient();
340
+ const client = getClient(config);
341
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
342
+ return reactQuery.useMutation({
343
+ mutationFn: async (input) => {
344
+ const response = await client("@patch/comments/:id/status", {
345
+ method: "PATCH",
346
+ params: { id: input.id },
347
+ body: { status: input.status },
348
+ headers: config.headers
349
+ });
350
+ const data = response.data;
351
+ if (!data) throw utils.toError(response.error);
352
+ return data;
353
+ },
354
+ onSettled: () => {
355
+ queryClient.invalidateQueries({
356
+ queryKey: queries.comments.list._def
357
+ });
358
+ queryClient.invalidateQueries({
359
+ queryKey: queries.commentCount.byResource._def
360
+ });
361
+ queryClient.invalidateQueries({ queryKey: ["commentsThread"] });
362
+ }
363
+ });
364
+ }
365
+ function useDeleteComment(config) {
366
+ const queryClient = reactQuery.useQueryClient();
367
+ const client = getClient(config);
368
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
369
+ return reactQuery.useMutation({
370
+ mutationFn: async (id) => {
371
+ const response = await client("@delete/comments/:id", {
372
+ method: "DELETE",
373
+ params: { id },
374
+ headers: config.headers
375
+ });
376
+ const data = response.data;
377
+ if (!data) throw utils.toError(response.error);
378
+ return data;
379
+ },
380
+ onSettled: () => {
381
+ queryClient.invalidateQueries({
382
+ queryKey: queries.comments.list._def
383
+ });
384
+ queryClient.invalidateQueries({
385
+ queryKey: queries.commentCount.byResource._def
386
+ });
387
+ queryClient.invalidateQueries({ queryKey: ["commentsThread"] });
388
+ }
389
+ });
390
+ }
391
+ function useToggleLike(config, params) {
392
+ const queryClient = reactQuery.useQueryClient();
393
+ const client = getClient(config);
394
+ const queries = plugins_comments_queryKeys.createCommentsQueryKeys(client, config.headers);
395
+ const isInfinite = !!params.infiniteKey && (params.parentId ?? null) === null;
396
+ const listKey = isInfinite ? params.infiniteKey : queries.comments.list({
397
+ resourceId: params.resourceId,
398
+ resourceType: params.resourceType,
399
+ parentId: params.parentId ?? null,
400
+ status: "approved",
401
+ currentUserId: params.currentUserId
402
+ }).queryKey;
403
+ function applyLikeUpdate(commentId, updater) {
404
+ if (isInfinite) {
405
+ queryClient.setQueryData(
406
+ listKey,
407
+ (old) => {
408
+ if (!old) return old;
409
+ return {
410
+ ...old,
411
+ pages: old.pages.map((page) => ({
412
+ ...page,
413
+ items: page.items.map(
414
+ (c) => c.id === commentId ? updater(c) : c
415
+ )
416
+ }))
417
+ };
418
+ }
419
+ );
420
+ } else {
421
+ queryClient.setQueryData(listKey, (old) => {
422
+ if (!old) return old;
423
+ return {
424
+ ...old,
425
+ items: old.items.map((c) => c.id === commentId ? updater(c) : c)
426
+ };
427
+ });
428
+ }
429
+ }
430
+ return reactQuery.useMutation({
431
+ mutationFn: async (input) => {
432
+ const response = await client("@post/comments/:id/like", {
433
+ method: "POST",
434
+ params: { id: input.commentId },
435
+ body: { authorId: input.authorId },
436
+ headers: config.headers
437
+ });
438
+ const data = response.data;
439
+ if (!data) throw utils.toError(response.error);
440
+ return data;
441
+ },
442
+ onMutate: async (input) => {
443
+ await queryClient.cancelQueries({ queryKey: listKey });
444
+ const previous = isInfinite ? queryClient.getQueryData(listKey) : queryClient.getQueryData(listKey);
445
+ applyLikeUpdate(input.commentId, (c) => {
446
+ const wasLiked = c.isLikedByCurrentUser;
447
+ return {
448
+ ...c,
449
+ isLikedByCurrentUser: !wasLiked,
450
+ likes: wasLiked ? Math.max(0, c.likes - 1) : c.likes + 1
451
+ };
452
+ });
453
+ return { previous };
454
+ },
455
+ onError: (_err, _input, context) => {
456
+ if (context?.previous !== void 0) {
457
+ queryClient.setQueryData(listKey, context.previous);
458
+ }
459
+ },
460
+ onSettled: () => {
461
+ queryClient.invalidateQueries({ queryKey: listKey });
462
+ }
463
+ });
464
+ }
465
+
466
+ exports.useApproveComment = useApproveComment;
467
+ exports.useCommentCount = useCommentCount;
468
+ exports.useComments = useComments;
469
+ exports.useDeleteComment = useDeleteComment;
470
+ exports.useInfiniteComments = useInfiniteComments;
471
+ exports.usePostComment = usePostComment;
472
+ exports.useSuspenseComments = useSuspenseComments;
473
+ exports.useSuspenseModerationComments = useSuspenseModerationComments;
474
+ exports.useToggleLike = useToggleLike;
475
+ exports.useUpdateComment = useUpdateComment;
476
+ exports.useUpdateCommentStatus = useUpdateCommentStatus;