Open History to see the last commit
1import React from 'react'
2import { Query } from '../core'
3β
4import { Theme, useTheme } from './theme'
5import useMediaQuery from './useMediaQuery'
6β
7export const isServer = typeof window === 'undefined'
8β
9type StyledComponent<T> = T extends 'button'
10 ? React.DetailedHTMLProps<
11 React.ButtonHTMLAttributes<HTMLButtonElement>,
12 HTMLButtonElement
13 >
14 : T extends 'input'
15 ? React.DetailedHTMLProps<
16 React.InputHTMLAttributes<HTMLInputElement>,
17 HTMLInputElement
18 >
19 : T extends 'select'
20 ? React.DetailedHTMLProps<
21 React.SelectHTMLAttributes<HTMLSelectElement>,
22 HTMLSelectElement
23 >
24 : T extends keyof HTMLElementTagNameMap
25 ? React.HTMLAttributes<HTMLElementTagNameMap[T]>
26 : never
27β
28export function getQueryStatusColor(query: Query, theme: Theme) {
29 return query.state.isFetching
30 ? theme.active
31 : !query.getObserversCount()
32 ? theme.gray
33 : query.isStale()
34 ? theme.warning
35 : theme.success
36}
37β
38export function getQueryStatusLabel(query: Query) {
39 return query.state.isFetching
40 ? 'fetching'
41 : !query.getObserversCount()
42 ? 'inactive'
43 : query.isStale()
44 ? 'stale'
45 : 'fresh'
46}
47β
48type Styles =
49 | React.CSSProperties
50 | ((props: Record<string, any>, theme: Theme) => React.CSSProperties)
51β
52export function styled<T extends keyof HTMLElementTagNameMap>(
53 type: T,
54 newStyles: Styles,
55 queries: Record<string, Styles> = {}
56) {
57 return React.forwardRef<HTMLElementTagNameMap[T], StyledComponent<T>>(
58 ({ style, ...rest }, ref) => {
59 const theme = useTheme()
60β
61 const mediaStyles = Object.entries(queries).reduce(
62 (current, [key, value]) => {
63 // eslint-disable-next-line react-hooks/rules-of-hooks
64 return useMediaQuery(key)
65 ? {
66 ...current,
67 ...(typeof value === 'function' ? value(rest, theme) : value),
68 }
69 : current
70 },
71 {}
72 )
73β
74 return React.createElement(type, {
75 ...rest,
76 style: {
77 ...(typeof newStyles === 'function'
78 ? newStyles(rest, theme)
79 : newStyles),
80 ...style,
81 ...mediaStyles,
82 },
83 ref,
84 })
85 }
86 )
87}
88β
89export function useIsMounted() {
90 const mountedRef = React.useRef(false)
91 const isMounted = React.useCallback(() => mountedRef.current, [])
92β
93 React[isServer ? 'useEffect' : 'useLayoutEffect'](() => {
94 mountedRef.current = true
95 return () => {
96 mountedRef.current = false
97 }
98 }, [])
99β
100 return isMounted
101}
102β
103/**
104 * This hook is a safe useState version which schedules state updates in microtasks
105 * to prevent updating a component state while React is rendering different components
106 * or when the component is not mounted anymore.
107 */
108export function useSafeState<T>(initialState: T): [T, (value: T) => void] {
109 const isMounted = useIsMounted()
110 const [state, setState] = React.useState(initialState)
111β
112 const safeSetState = React.useCallback(
113 (value: T) => {
114 scheduleMicrotask(() => {
115 if (isMounted()) {
116 setState(value)
117 }
118 })
119 },
120 [isMounted]
121 )
122β
123 return [state, safeSetState]
124}
125β
126/**
127 * Displays a string regardless the type of the data
128 * @param {unknown} value Value to be stringified
129 */
130export const displayValue = (value: unknown) => {
131 const name = Object.getOwnPropertyNames(Object(value))
132 const newValue = typeof value === 'bigint' ? `${value.toString()}n` : value
133β
134 return JSON.stringify(newValue, name)
135}
136β
137/**
138 * Schedules a microtask.
139 * This can be useful to schedule state updates after rendering.
140 */
141function scheduleMicrotask(callback: () => void) {
142 Promise.resolve()
143 .then(callback)
144 .catch(error =>
145 setTimeout(() => {
146 throw error
147 })
148 )
149}