forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
319 lines (292 loc) · 7.75 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import * as React from 'react'
import type { Query } from '@tanstack/react-query'
import SuperJSON from 'superjson'
import type { Theme } from './theme'
import { useTheme } from './theme'
import useMediaQuery from './useMediaQuery'
type StyledComponent<T> = T extends 'button'
? React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>
: T extends 'input'
? React.DetailedHTMLProps<
React.InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement
>
: T extends 'select'
? React.DetailedHTMLProps<
React.SelectHTMLAttributes<HTMLSelectElement>,
HTMLSelectElement
>
: T extends keyof HTMLElementTagNameMap
? React.HTMLAttributes<HTMLElementTagNameMap[T]>
: never
export function getQueryStatusColor({
queryState,
observerCount,
isStale,
theme,
}: {
queryState: Query['state']
observerCount: number
isStale: boolean
theme: Theme
}) {
return queryState.fetchStatus === 'fetching'
? theme.active
: !observerCount
? theme.gray
: queryState.fetchStatus === 'paused'
? theme.paused
: isStale
? theme.warning
: theme.success
}
export function getQueryStatusLabel(query: Query) {
return query.state.fetchStatus === 'fetching'
? 'fetching'
: !query.getObserversCount()
? 'inactive'
: query.state.fetchStatus === 'paused'
? 'paused'
: query.isStale()
? 'stale'
: 'fresh'
}
type Styles =
| React.CSSProperties
| ((props: Record<string, any>, theme: Theme) => React.CSSProperties)
export function styled<T extends keyof HTMLElementTagNameMap>(
type: T,
newStyles: Styles,
queries: Record<string, Styles> = {},
) {
return React.forwardRef<HTMLElementTagNameMap[T], StyledComponent<T>>(
({ style, ...rest }, ref) => {
const theme = useTheme()
const mediaStyles = Object.entries(queries).reduce(
(current, [key, value]) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
return useMediaQuery(key)
? {
...current,
...(typeof value === 'function' ? value(rest, theme) : value),
}
: current
},
{},
)
return React.createElement(type, {
...rest,
style: {
...(typeof newStyles === 'function'
? newStyles(rest, theme)
: newStyles),
...style,
...mediaStyles,
},
ref,
})
},
)
}
export function useIsMounted() {
const mountedRef = React.useRef(false)
const isMounted = React.useCallback(() => mountedRef.current, [])
React.useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
return isMounted
}
/**
* Displays a string regardless the type of the data
* @param {unknown} value Value to be stringified
* @param {boolean} beautify Formats json to multiline
*/
export const displayValue = (value: unknown, beautify: boolean = false) => {
const { json } = SuperJSON.serialize(value)
return JSON.stringify(json, null, beautify ? 2 : undefined)
}
// Sorting functions
type SortFn = (a: Query, b: Query) => number
const getStatusRank = (q: Query) =>
q.state.fetchStatus !== 'idle'
? 0
: !q.getObserversCount()
? 3
: q.isStale()
? 2
: 1
const queryHashSort: SortFn = (a, b) => a.queryHash.localeCompare(b.queryHash)
const dateSort: SortFn = (a, b) =>
a.state.dataUpdatedAt < b.state.dataUpdatedAt ? 1 : -1
const statusAndDateSort: SortFn = (a, b) => {
if (getStatusRank(a) === getStatusRank(b)) {
return dateSort(a, b)
}
return getStatusRank(a) > getStatusRank(b) ? 1 : -1
}
export const sortFns: Record<string, SortFn> = {
'Status > Last Updated': statusAndDateSort,
'Query Hash': queryHashSort,
'Last Updated': dateSort,
}
export const minPanelSize = 70
export const defaultPanelSize = 500
export const sides: Record<Side, Side> = {
top: 'bottom',
bottom: 'top',
left: 'right',
right: 'left',
}
export type Corner = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
export type Side = 'left' | 'right' | 'top' | 'bottom'
/**
* Check if the given side is vertical (left/right)
*/
export function isVerticalSide(side: Side) {
return ['left', 'right'].includes(side)
}
/**
* Get the opposite side, eg 'left' => 'right'. 'top' => 'bottom', etc
*/
export function getOppositeSide(side: Side): Side {
return sides[side]
}
/**
* Given as css prop it will return a sided css prop based on a given side
* Example given `border` and `right` it return `borderRight`
*/
export function getSidedProp<T extends string>(prop: T, side: Side) {
return `${prop}${
side.charAt(0).toUpperCase() + side.slice(1)
}` as `${T}${Capitalize<Side>}`
}
export interface SidePanelStyleOptions {
/**
* Position of the panel
* Defaults to 'bottom'
*/
position?: Side
/**
* Staring height for the panel, it is set if the position is horizontal eg 'top' or 'bottom'
* Defaults to 500
*/
height?: number
/**
* Staring width for the panel, it is set if the position is vertical eg 'left' or 'right'
* Defaults to 500
*/
width?: number
/**
* RQ devtools theme
*/
devtoolsTheme: Theme
/**
* Sets the correct transition and visibility styles
*/
isOpen?: boolean
/**
* If the panel is resizing set to true to apply the correct transition styles
*/
isResizing?: boolean
/**
* Extra panel style passed by the user
*/
panelStyle?: React.CSSProperties
}
export function getSidePanelStyle({
position = 'bottom',
height,
width,
devtoolsTheme,
isOpen,
isResizing,
panelStyle,
}: SidePanelStyleOptions): React.CSSProperties {
const oppositeSide = getOppositeSide(position)
const borderSide = getSidedProp('border', oppositeSide)
const isVertical = isVerticalSide(position)
return {
...panelStyle,
direction: 'ltr',
position: 'fixed',
[position]: 0,
[borderSide]: `1px solid ${devtoolsTheme.gray}`,
transformOrigin: oppositeSide,
boxShadow: '0 0 20px rgba(0,0,0,.3)',
zIndex: 99999,
// visibility will be toggled after transitions, but set initial state here
visibility: isOpen ? 'visible' : 'hidden',
...(isResizing
? {
transition: `none`,
}
: { transition: `all .2s ease` }),
...(isOpen
? {
opacity: 1,
pointerEvents: 'all',
transform: isVertical
? `translateX(0) scale(1)`
: `translateY(0) scale(1)`,
}
: {
opacity: 0,
pointerEvents: 'none',
transform: isVertical
? `translateX(15px) scale(1.02)`
: `translateY(15px) scale(1.02)`,
}),
...(isVertical
? {
top: 0,
height: '100vh',
maxWidth: '90%',
width:
typeof width === 'number' && width >= minPanelSize
? width
: defaultPanelSize,
}
: {
left: 0,
width: '100%',
maxHeight: '90%',
height:
typeof height === 'number' && height >= minPanelSize
? height
: defaultPanelSize,
}),
}
}
/**
* Get resize handle style based on a given side
*/
export function getResizeHandleStyle(
position: Side = 'bottom',
): React.CSSProperties {
const isVertical = isVerticalSide(position)
const oppositeSide = getOppositeSide(position)
const marginSide = getSidedProp('margin', oppositeSide)
return {
position: 'absolute',
cursor: isVertical ? 'col-resize' : 'row-resize',
zIndex: 100000,
[oppositeSide]: 0,
[marginSide]: `-4px`,
...(isVertical
? {
top: 0,
height: '100%',
width: '4px',
}
: {
width: '100%',
height: '4px',
}),
}
}