Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
110 lines (95 loc) · 2.45 KB
/
Copy pathutils.ts
File metadata and controls
110 lines (95 loc) · 2.45 KB
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
// @ts-nocheck
import React from 'react'
import { useTheme } from './theme'
import useMediaQuery from './useMediaQuery'
export const isServer = typeof window === 'undefined'
export function getQueryStatusColor(query, theme) {
return query.state.isFetching
? theme.active
: query.isStale()
? theme.warning
: !query.observers.length
? theme.gray
: theme.success
}
export function getQueryStatusLabel(query) {
return query.state.isFetching
? 'fetching'
: !query.observers.length
? 'inactive'
: query.isStale()
? 'stale'
: 'fresh'
}
export function styled(type, newStyles, queries = {}) {
return React.forwardRef(({ 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,
})
})
}
function useIsMounted() {
const mountedRef = React.useRef(false)
const isMounted = React.useCallback(() => mountedRef.current, [])
React[isServer ? 'useEffect' : 'useLayoutEffect'](() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
return isMounted
}
/**
* This hook is a safe useState version which schedules state updates in microtasks
* to prevent updating a component state while React is rendering different components
* or when the component is not mounted anymore.
*/
export function useSafeState(initialState) {
const isMounted = useIsMounted()
const [state, setState] = React.useState(initialState)
const safeSetState = React.useCallback(
value => {
scheduleMicrotask(() => {
if (isMounted()) {
setState(value)
}
})
},
[isMounted]
)
return [state, safeSetState]
}
/**
* Schedules a microtask.
* This can be useful to schedule state updates after rendering.
*/
function scheduleMicrotask(callback) {
Promise.resolve()
.then(callback)
.catch(error =>
setTimeout(() => {
throw error
})
)
}