@tanstack/react-query-devtools 5.0.0-alpha.2 → 5.0.0-alpha.21

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 (88) hide show
  1. package/build/lib/CachePanel/ActiveQuery.d.ts +18 -0
  2. package/build/lib/CachePanel/ActiveQuery.esm.js +271 -0
  3. package/build/lib/CachePanel/ActiveQuery.esm.js.map +1 -0
  4. package/build/lib/CachePanel/ActiveQuery.js +275 -0
  5. package/build/lib/CachePanel/ActiveQuery.js.map +1 -0
  6. package/build/lib/CachePanel/ActiveQuery.mjs +257 -0
  7. package/build/lib/CachePanel/ActiveQuery.mjs.map +1 -0
  8. package/build/lib/CachePanel/CachePanel.d.ts +57 -0
  9. package/build/lib/CachePanel/CachePanel.esm.js +310 -0
  10. package/build/lib/CachePanel/CachePanel.esm.js.map +1 -0
  11. package/build/lib/CachePanel/CachePanel.js +315 -0
  12. package/build/lib/CachePanel/CachePanel.js.map +1 -0
  13. package/build/lib/CachePanel/CachePanel.mjs +310 -0
  14. package/build/lib/CachePanel/CachePanel.mjs.map +1 -0
  15. package/build/lib/CachePanel/Header/QueryStatusCount.d.ts +7 -0
  16. package/build/lib/CachePanel/Header/QueryStatusCount.esm.js +49 -0
  17. package/build/lib/CachePanel/Header/QueryStatusCount.esm.js.map +1 -0
  18. package/build/lib/CachePanel/Header/QueryStatusCount.js +53 -0
  19. package/build/lib/CachePanel/Header/QueryStatusCount.js.map +1 -0
  20. package/build/lib/CachePanel/Header/QueryStatusCount.mjs +49 -0
  21. package/build/lib/CachePanel/Header/QueryStatusCount.mjs.map +1 -0
  22. package/build/lib/CachePanel/QueryRow.d.ts +13 -0
  23. package/build/lib/CachePanel/QueryRow.esm.js +98 -0
  24. package/build/lib/CachePanel/QueryRow.esm.js.map +1 -0
  25. package/build/lib/CachePanel/QueryRow.js +102 -0
  26. package/build/lib/CachePanel/QueryRow.js.map +1 -0
  27. package/build/lib/CachePanel/QueryRow.mjs +82 -0
  28. package/build/lib/CachePanel/QueryRow.mjs.map +1 -0
  29. package/build/lib/Explorer.esm.js +2 -1
  30. package/build/lib/Explorer.esm.js.map +1 -1
  31. package/build/lib/Explorer.js +2 -1
  32. package/build/lib/Explorer.js.map +1 -1
  33. package/build/lib/Explorer.mjs +1 -0
  34. package/build/lib/Explorer.mjs.map +1 -1
  35. package/build/lib/devtools.d.ts +5 -48
  36. package/build/lib/devtools.esm.js +11 -550
  37. package/build/lib/devtools.esm.js.map +1 -1
  38. package/build/lib/devtools.js +12 -551
  39. package/build/lib/devtools.js.map +1 -1
  40. package/build/lib/devtools.mjs +7 -547
  41. package/build/lib/devtools.mjs.map +1 -1
  42. package/build/lib/index.esm.js +3 -1
  43. package/build/lib/index.esm.js.map +1 -1
  44. package/build/lib/index.js +3 -1
  45. package/build/lib/index.js.map +1 -1
  46. package/build/lib/index.mjs +3 -1
  47. package/build/lib/index.mjs.map +1 -1
  48. package/build/lib/index.prod.esm.js +821 -651
  49. package/build/lib/index.prod.esm.js.map +1 -1
  50. package/build/lib/index.prod.js +819 -650
  51. package/build/lib/index.prod.js.map +1 -1
  52. package/build/lib/index.prod.mjs +764 -625
  53. package/build/lib/index.prod.mjs.map +1 -1
  54. package/build/lib/styledComponents.d.ts +0 -1
  55. package/build/lib/styledComponents.esm.js +11 -1
  56. package/build/lib/styledComponents.esm.js.map +1 -1
  57. package/build/lib/styledComponents.js +11 -1
  58. package/build/lib/styledComponents.js.map +1 -1
  59. package/build/lib/styledComponents.mjs +11 -1
  60. package/build/lib/styledComponents.mjs.map +1 -1
  61. package/build/lib/theme.esm.js +1 -0
  62. package/build/lib/theme.esm.js.map +1 -1
  63. package/build/lib/theme.js +1 -0
  64. package/build/lib/theme.js.map +1 -1
  65. package/build/lib/theme.mjs +1 -0
  66. package/build/lib/theme.mjs.map +1 -1
  67. package/build/lib/types.d.ts +11 -0
  68. package/build/lib/useSubscribeToQueryCache.d.ts +3 -0
  69. package/build/lib/useSubscribeToQueryCache.esm.js +15 -0
  70. package/build/lib/useSubscribeToQueryCache.esm.js.map +1 -0
  71. package/build/lib/useSubscribeToQueryCache.js +19 -0
  72. package/build/lib/useSubscribeToQueryCache.js.map +1 -0
  73. package/build/lib/useSubscribeToQueryCache.mjs +15 -0
  74. package/build/lib/useSubscribeToQueryCache.mjs.map +1 -0
  75. package/build/umd/index.development.js +1667 -1529
  76. package/build/umd/index.development.js.map +1 -1
  77. package/build/umd/index.production.js.map +1 -1
  78. package/package.json +4 -4
  79. package/src/CachePanel/ActiveQuery.tsx +380 -0
  80. package/src/CachePanel/CachePanel.tsx +439 -0
  81. package/src/CachePanel/Header/QueryStatusCount.tsx +93 -0
  82. package/src/CachePanel/QueryRow.tsx +125 -0
  83. package/src/__tests__/Explorer.test.tsx +3 -2
  84. package/src/__tests__/devtools.test.tsx +229 -9
  85. package/src/devtools.tsx +10 -865
  86. package/src/styledComponents.ts +19 -1
  87. package/src/types.ts +12 -0
  88. package/src/useSubscribeToQueryCache.ts +26 -0
@@ -1 +1 @@
1
- {"version":3,"file":"index.production.js","sources":["../../src/index.ts"],"sourcesContent":["'use client'\n\nimport * as devtools from './devtools'\n\nexport const ReactQueryDevtools: (typeof devtools)['ReactQueryDevtools'] =\n process.env.NODE_ENV !== 'development'\n ? function () {\n return null\n }\n : devtools.ReactQueryDevtools\n\nexport const ReactQueryDevtoolsPanel: (typeof devtools)['ReactQueryDevtoolsPanel'] =\n process.env.NODE_ENV !== 'development'\n ? (function () {\n return null\n } as any)\n : devtools.ReactQueryDevtoolsPanel\n"],"names":[],"mappings":"+QAMM,WACE,OAAO,gCAMR,WACC,OAAO"}
1
+ {"version":3,"file":"index.production.js","sources":["../../src/index.ts"],"sourcesContent":["'use client'\n\nimport * as devtools from './devtools'\n\nexport const ReactQueryDevtools: (typeof devtools)['ReactQueryDevtools'] =\n process.env.NODE_ENV !== 'development'\n ? function () {\n return null\n }\n : devtools.ReactQueryDevtools\n\nexport const ReactQueryDevtoolsPanel: (typeof devtools)['ReactQueryDevtoolsPanel'] =\n process.env.NODE_ENV !== 'development'\n ? (function () {\n return null\n } as any)\n : devtools.ReactQueryDevtoolsPanel\n"],"names":[],"mappings":"+QAMM,WACE,OAAO,IACT,4BAKC,WACC,OAAO,IACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-query-devtools",
3
- "version": "5.0.0-alpha.2",
3
+ "version": "5.0.0-alpha.21",
4
4
  "description": "Developer tools to interact with and visualize the TanStack/react-query cache",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  "react": "^18.2.0",
44
44
  "react-dom": "^18.2.0",
45
45
  "react-error-boundary": "^3.1.4",
46
- "@tanstack/react-query": "5.0.0-alpha.2"
46
+ "@tanstack/react-query": "5.0.0-alpha.21"
47
47
  },
48
48
  "dependencies": {
49
49
  "@tanstack/match-sorter-utils": "^8.7.0",
@@ -52,13 +52,13 @@
52
52
  "peerDependencies": {
53
53
  "react": "^18.0.0",
54
54
  "react-dom": "^18.0.0",
55
- "@tanstack/react-query": "5.0.0-alpha.2"
55
+ "@tanstack/react-query": "5.0.0-alpha.21"
56
56
  },
57
57
  "scripts": {
58
58
  "clean": "rimraf ./build",
59
59
  "test:eslint": "eslint --ext .ts,.tsx ./src",
60
60
  "test:types": "tsc",
61
- "test:lib": "jest --config ./jest.config.ts",
61
+ "test:lib": "vitest run --coverage",
62
62
  "test:lib:dev": "pnpm run test:lib --watch",
63
63
  "build:types": "tsc --build && cp build/lib/index.d.ts build/lib/index.prod.d.ts"
64
64
  }
@@ -0,0 +1,380 @@
1
+ import React from 'react'
2
+ import type { QueryCache, QueryClient } from '@tanstack/react-query'
3
+
4
+ import useSubscribeToQueryCache from '../useSubscribeToQueryCache'
5
+ import { Button, Code, Select, ActiveQueryPanel } from '../styledComponents'
6
+
7
+ import {
8
+ getQueryStatusLabel,
9
+ getQueryStatusColor,
10
+ displayValue,
11
+ } from '../utils'
12
+ import Explorer from '../Explorer'
13
+ import type { DevToolsErrorType } from '../types'
14
+ import { defaultTheme as theme } from '../theme'
15
+
16
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
17
+ function noop() {}
18
+
19
+ /**
20
+ * Panel for the query currently being inspected
21
+ *
22
+ * It displays query details (key, observers...), query actions,
23
+ * the data explorer and the query explorer
24
+ */
25
+ const ActiveQuery = ({
26
+ queryCache,
27
+ activeQueryHash,
28
+ queryClient,
29
+ errorTypes,
30
+ }: {
31
+ queryCache: QueryCache
32
+ activeQueryHash: string
33
+ queryClient: QueryClient
34
+ errorTypes: DevToolsErrorType[]
35
+ }) => {
36
+ const activeQuery = useSubscribeToQueryCache(queryCache, () =>
37
+ queryCache.getAll().find((query) => query.queryHash === activeQueryHash),
38
+ )
39
+
40
+ const activeQueryState = useSubscribeToQueryCache(
41
+ queryCache,
42
+ () =>
43
+ queryCache.getAll().find((query) => query.queryHash === activeQueryHash)
44
+ ?.state,
45
+ )
46
+
47
+ const isStale =
48
+ useSubscribeToQueryCache(queryCache, () =>
49
+ queryCache
50
+ .getAll()
51
+ .find((query) => query.queryHash === activeQueryHash)
52
+ ?.isStale(),
53
+ ) ?? false
54
+
55
+ const observerCount =
56
+ useSubscribeToQueryCache(queryCache, () =>
57
+ queryCache
58
+ .getAll()
59
+ .find((query) => query.queryHash === activeQueryHash)
60
+ ?.getObserversCount(),
61
+ ) ?? 0
62
+
63
+ const handleRefetch = () => {
64
+ const promise = activeQuery?.fetch()
65
+ promise?.catch(noop)
66
+ }
67
+
68
+ const currentErrorTypeName = React.useMemo(() => {
69
+ if (activeQuery && activeQueryState?.error) {
70
+ const errorType = errorTypes.find(
71
+ (type) =>
72
+ type.initializer(activeQuery).toString() ===
73
+ activeQueryState.error?.toString(),
74
+ )
75
+ return errorType?.name
76
+ }
77
+ return undefined
78
+ }, [activeQuery, activeQueryState?.error, errorTypes])
79
+
80
+ if (!activeQuery || !activeQueryState) {
81
+ return null
82
+ }
83
+
84
+ const triggerError = (errorType?: DevToolsErrorType) => {
85
+ const error =
86
+ errorType?.initializer(activeQuery) ??
87
+ new Error('Unknown error from devtools')
88
+
89
+ const __previousQueryOptions = activeQuery.options
90
+
91
+ activeQuery.setState({
92
+ status: 'error',
93
+ error,
94
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
95
+ fetchMeta: {
96
+ ...activeQuery.state.fetchMeta,
97
+ __previousQueryOptions,
98
+ } as any,
99
+ })
100
+ }
101
+
102
+ const restoreQueryAfterLoadingOrError = () => {
103
+ activeQuery.fetch(
104
+ (activeQuery.state.fetchMeta as any).__previousQueryOptions,
105
+ {
106
+ // Make sure this fetch will cancel the previous one
107
+ cancelRefetch: true,
108
+ },
109
+ )
110
+ }
111
+
112
+ return (
113
+ <ActiveQueryPanel>
114
+ <div
115
+ style={{
116
+ padding: '.5em',
117
+ background: theme.backgroundAlt,
118
+ position: 'sticky',
119
+ top: 0,
120
+ zIndex: 1,
121
+ }}
122
+ >
123
+ Query Details
124
+ </div>
125
+ <div
126
+ style={{
127
+ padding: '.5em',
128
+ }}
129
+ >
130
+ <div
131
+ style={{
132
+ marginBottom: '.5em',
133
+ display: 'flex',
134
+ alignItems: 'flex-start',
135
+ justifyContent: 'space-between',
136
+ }}
137
+ >
138
+ <Code
139
+ style={{
140
+ lineHeight: '1.8em',
141
+ }}
142
+ >
143
+ <pre
144
+ style={{
145
+ margin: 0,
146
+ padding: 0,
147
+ overflow: 'auto',
148
+ }}
149
+ >
150
+ {displayValue(activeQuery.queryKey, true)}
151
+ </pre>
152
+ </Code>
153
+ <span
154
+ style={{
155
+ padding: '0.3em .6em',
156
+ borderRadius: '0.4em',
157
+ fontWeight: 'bold',
158
+ textShadow: '0 2px 10px black',
159
+ background: getQueryStatusColor({
160
+ queryState: activeQueryState,
161
+ isStale: isStale,
162
+ observerCount: observerCount,
163
+ theme,
164
+ }),
165
+ flexShrink: 0,
166
+ }}
167
+ >
168
+ {getQueryStatusLabel(activeQuery)}
169
+ </span>
170
+ </div>
171
+ <div
172
+ style={{
173
+ marginBottom: '.5em',
174
+ display: 'flex',
175
+ alignItems: 'center',
176
+ justifyContent: 'space-between',
177
+ }}
178
+ >
179
+ Observers: <Code>{observerCount}</Code>
180
+ </div>
181
+ <div
182
+ style={{
183
+ display: 'flex',
184
+ alignItems: 'center',
185
+ justifyContent: 'space-between',
186
+ }}
187
+ >
188
+ Last Updated:{' '}
189
+ <Code>
190
+ {new Date(activeQueryState.dataUpdatedAt).toLocaleTimeString()}
191
+ </Code>
192
+ </div>
193
+ </div>
194
+ <div
195
+ style={{
196
+ background: theme.backgroundAlt,
197
+ padding: '.5em',
198
+ position: 'sticky',
199
+ top: 0,
200
+ zIndex: 1,
201
+ }}
202
+ >
203
+ Actions
204
+ </div>
205
+ <div
206
+ style={{
207
+ padding: '0.5em',
208
+ display: 'flex',
209
+ flexWrap: 'wrap',
210
+ gap: '0.5em',
211
+ alignItems: 'flex-end',
212
+ }}
213
+ >
214
+ <Button
215
+ type="button"
216
+ onClick={handleRefetch}
217
+ disabled={activeQueryState.fetchStatus === 'fetching'}
218
+ style={{
219
+ background: theme.active,
220
+ }}
221
+ >
222
+ Refetch
223
+ </Button>{' '}
224
+ <Button
225
+ type="button"
226
+ onClick={() => queryClient.invalidateQueries(activeQuery)}
227
+ style={{
228
+ background: theme.warning,
229
+ color: theme.inputTextColor,
230
+ }}
231
+ >
232
+ Invalidate
233
+ </Button>{' '}
234
+ <Button
235
+ type="button"
236
+ onClick={() => queryClient.resetQueries(activeQuery)}
237
+ style={{
238
+ background: theme.gray,
239
+ }}
240
+ >
241
+ Reset
242
+ </Button>{' '}
243
+ <Button
244
+ type="button"
245
+ onClick={() => queryClient.removeQueries(activeQuery)}
246
+ style={{
247
+ background: theme.danger,
248
+ }}
249
+ >
250
+ Remove
251
+ </Button>{' '}
252
+ <Button
253
+ type="button"
254
+ onClick={() => {
255
+ if (activeQuery.state.data === undefined) {
256
+ restoreQueryAfterLoadingOrError()
257
+ } else {
258
+ const __previousQueryOptions = activeQuery.options
259
+ // Trigger a fetch in order to trigger suspense as well.
260
+ activeQuery.fetch({
261
+ ...__previousQueryOptions,
262
+ queryFn: () => {
263
+ return new Promise(() => {
264
+ // Never resolve
265
+ })
266
+ },
267
+ gcTime: -1,
268
+ })
269
+ activeQuery.setState({
270
+ data: undefined,
271
+ status: 'pending',
272
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
273
+ fetchMeta: {
274
+ ...activeQuery.state.fetchMeta,
275
+ __previousQueryOptions,
276
+ } as any,
277
+ })
278
+ }
279
+ }}
280
+ style={{
281
+ background: theme.paused,
282
+ }}
283
+ >
284
+ {activeQuery.state.status === 'pending' ? 'Restore' : 'Trigger'}{' '}
285
+ loading
286
+ </Button>{' '}
287
+ {errorTypes.length === 0 || activeQuery.state.status === 'error' ? (
288
+ <Button
289
+ type="button"
290
+ onClick={() => {
291
+ if (!activeQuery.state.error) {
292
+ triggerError()
293
+ } else {
294
+ queryClient.resetQueries(activeQuery)
295
+ }
296
+ }}
297
+ style={{
298
+ background: theme.danger,
299
+ }}
300
+ >
301
+ {activeQuery.state.status === 'error' ? 'Restore' : 'Trigger'} error
302
+ </Button>
303
+ ) : (
304
+ <label>
305
+ Trigger error:
306
+ <Select
307
+ value={currentErrorTypeName ?? ''}
308
+ style={{ marginInlineStart: '.5em' }}
309
+ onChange={(e) => {
310
+ const errorType = errorTypes.find(
311
+ (t) => t.name === e.target.value,
312
+ )
313
+
314
+ triggerError(errorType)
315
+ }}
316
+ >
317
+ <option key="" value="" />
318
+ {errorTypes.map((errorType) => (
319
+ <option key={errorType.name} value={errorType.name}>
320
+ {errorType.name}
321
+ </option>
322
+ ))}
323
+ </Select>
324
+ </label>
325
+ )}
326
+ </div>
327
+ <div
328
+ style={{
329
+ background: theme.backgroundAlt,
330
+ padding: '.5em',
331
+ position: 'sticky',
332
+ top: 0,
333
+ zIndex: 1,
334
+ }}
335
+ >
336
+ Data Explorer
337
+ </div>
338
+ <div
339
+ style={{
340
+ padding: '.5em',
341
+ }}
342
+ >
343
+ <Explorer
344
+ label="Data"
345
+ value={activeQueryState.data}
346
+ defaultExpanded={{}}
347
+ copyable
348
+ />
349
+ </div>
350
+ <div
351
+ style={{
352
+ background: theme.backgroundAlt,
353
+ padding: '.5em',
354
+ position: 'sticky',
355
+ top: 0,
356
+ zIndex: 1,
357
+ }}
358
+ >
359
+ Query Explorer
360
+ </div>
361
+ <div
362
+ style={{
363
+ padding: '.5em',
364
+ }}
365
+ >
366
+ <Explorer
367
+ label="Query"
368
+ value={activeQuery}
369
+ defaultExpanded={{
370
+ queryKey: true,
371
+ }}
372
+ />
373
+ </div>
374
+ </ActiveQueryPanel>
375
+ )
376
+ }
377
+
378
+ ActiveQuery.displayName = 'ActiveQuery'
379
+
380
+ export default ActiveQuery