Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.tsx

250 lines5.6 KB

1import { serialize } from 'superjson'
2import type { Query } from '@tanstack/query-core'
3import type { DevtoolsPosition } from './Context'
4​
5export function getQueryStatusLabel(query: Query) {
6 return query.state.fetchStatus === 'fetching'
7 ? 'fetching'
8 : !query.getObserversCount()
9 ? 'inactive'
10 : query.state.fetchStatus === 'paused'
11 ? 'paused'
12 : query.isStale()
13 ? 'stale'
14 : 'fresh'
16​
17export const queryStatusLabels = [
18 'fresh',
19 'stale',
20 'paused',
21 'inactive',
22 'fetching',
23] as const
24export type IQueryStatusLabel = (typeof queryStatusLabels)[number]
25​
26export function getSidedProp<T extends string>(
27 prop: T,
28 side: DevtoolsPosition,
29) {
30 return `${prop}${
31 side.charAt(0).toUpperCase() + side.slice(1)
32 }` as `${T}${Capitalize<DevtoolsPosition>}`
34​
35export function getQueryStatusColor({
36 queryState,
37 observerCount,
38 isStale,
39}: {
40 queryState: Query['state']
41 observerCount: number
42 isStale: boolean
43}) {
44 return queryState.fetchStatus === 'fetching'
45 ? 'blue'
46 : !observerCount
47 ? 'gray'
48 : queryState.fetchStatus === 'paused'
49 ? 'purple'
50 : isStale
51 ? 'yellow'
52 : 'green'
54​
55export function getQueryStatusColorByLabel(label: IQueryStatusLabel) {
56 return label === 'fresh'
57 ? 'green'
58 : label === 'stale'
59 ? 'yellow'
60 : label === 'paused'
61 ? 'purple'
62 : label === 'inactive'
63 ? 'gray'
64 : 'blue'
66​
67/**
68 * Displays a string regardless the type of the data
69 * @param {unknown} value Value to be stringified
70 * @param {boolean} beautify Formats json to multiline
71 */
72export const displayValue = (value: unknown, beautify: boolean = false) => {
73 const { json } = serialize(value)
74​
75 return JSON.stringify(json, null, beautify ? 2 : undefined)
77​
78// Sorting functions
79type SortFn = (a: Query, b: Query) => number
80​
81const getStatusRank = (q: Query) =>
82 q.state.fetchStatus !== 'idle'
83 ? 0
84 : !q.getObserversCount()
85 ? 3
86 : q.isStale()
87 ? 2
88 : 1
89​
90const queryHashSort: SortFn = (a, b) => a.queryHash.localeCompare(b.queryHash)
91​
92const dateSort: SortFn = (a, b) =>
93 a.state.dataUpdatedAt < b.state.dataUpdatedAt ? 1 : -1
94​
95const statusAndDateSort: SortFn = (a, b) => {
96 if (getStatusRank(a) === getStatusRank(b)) {
97 return dateSort(a, b)
98 }
99​
100 return getStatusRank(a) > getStatusRank(b) ? 1 : -1
102​
103export const sortFns: Record<string, SortFn> = {
104 status: statusAndDateSort,
105 'query hash': queryHashSort,
106 'last updated': dateSort,
108​
109export const convertRemToPixels = (rem: number) => {
110 return rem * parseFloat(getComputedStyle(document.documentElement).fontSize)
112​
113export const convertPixelsToRem = (px: number) => px / convertRemToPixels(1)
114​
115/**
116 * updates nested data by path
117 *
118 * @param {unknown} oldData Data to be updated
119 * @param {Array<string>} updatePath Path to the data to be updated
120 * @param {unknown} value New value
121 */
122export const updateNestedDataByPath = (
123 oldData: unknown,
124 updatePath: Array<string>,
125 value: unknown,
126): any => {
127 if (updatePath.length === 0) {
128 return value
129 }
130​
131 if (oldData instanceof Map) {
132 const newData = new Map(oldData)
133​
134 if (updatePath.length === 1) {
135 newData.set(updatePath[0], value)
136 return newData
137 }
138​
139 const [head, ...tail] = updatePath
140 newData.set(head, updateNestedDataByPath(newData.get(head), tail, value))
141 return newData
142 }
143​
144 if (oldData instanceof Set) {
145 const setAsArray = updateNestedDataByPath(
146 Array.from(oldData),
147 updatePath,
148 value,
149 )
150​
151 return new Set(setAsArray)
152 }
153​
154 if (Array.isArray(oldData)) {
155 const newData = [...oldData]
156​
157 if (updatePath.length === 1) {
158 // @ts-expect-error
159 newData[updatePath[0]] = value
160 return newData
161 }
162​
163 const [head, ...tail] = updatePath
164 // @ts-expect-error
165 newData[head] = updateNestedDataByPath(newData[head], tail, value)
166​
167 return newData
168 }
169​
170 if (oldData instanceof Object) {
171 const newData = { ...oldData }
172​
173 if (updatePath.length === 1) {
174 // @ts-expect-error
175 newData[updatePath[0]] = value
176 return newData
177 }
178​
179 const [head, ...tail] = updatePath
180 // @ts-expect-error
181 newData[head] = updateNestedDataByPath(newData[head], tail, value)
182​
183 return newData
184 }
185​
186 return oldData
188​
189/**
190 * Deletes nested data by path
191 *
192 * @param {unknown} oldData Data to be updated
193 * @param {Array<string>} deletePath Path to the data to be deleted
194 * @returns newData without the deleted items by path
195 */
196export const deleteNestedDataByPath = (
197 oldData: unknown,
198 deletePath: Array<string>,
199): any => {
200 if (oldData instanceof Map) {
201 const newData = new Map(oldData)
202​
203 if (deletePath.length === 1) {
204 newData.delete(deletePath[0])
205 return newData
206 }
207​
208 const [head, ...tail] = deletePath
209 newData.set(head, deleteNestedDataByPath(newData.get(head), tail))
210 return newData
211 }
212​
213 if (oldData instanceof Set) {
214 const setAsArray = deleteNestedDataByPath(Array.from(oldData), deletePath)
215 return new Set(setAsArray)
216 }
217​
218 if (Array.isArray(oldData)) {
219 const newData = [...oldData]
220​
221 if (deletePath.length === 1) {
222 return newData.filter((_, idx) => idx.toString() !== deletePath[0])
223 }
224​
225 const [head, ...tail] = deletePath
226​
227 // @ts-expect-error
228 newData[head] = deleteNestedDataByPath(newData[head], tail)
229​
230 return newData
231 }
232​
233 if (oldData instanceof Object) {
234 const newData = { ...oldData }
235​
236 if (deletePath.length === 1) {
237 // @ts-expect-error
238 delete newData[deletePath[0]]
239 return newData
240 }
241​
242 const [head, ...tail] = deletePath
243 // @ts-expect-error
244 newData[head] = deleteNestedDataByPath(newData[head], tail)
245​
246 return newData
247 }
248​
249 return oldData