Open History to see the last commit
1import * as React from 'react'
2import type { Query } from '@tanstack/react-query'
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({
29 queryState,
30 observerCount,
31 isStale,
32 theme,
33}: {
34 queryState: Query['state']
35 observerCount: number
36 isStale: boolean
37 theme: Theme
38}) {
39 return queryState.fetchStatus === 'fetching'
40 ? theme.active
41 : !observerCount
42 ? theme.gray
43 : queryState.fetchStatus === 'paused'
44 ? theme.paused
45 : isStale
46 ? theme.warning
47 : theme.success
48}
49β
50export function getQueryStatusLabel(query: Query) {
51 return query.state.fetchStatus === 'fetching'
52 ? 'fetching'
53 : !query.getObserversCount()
54 ? 'inactive'
55 : query.state.fetchStatus === 'paused'
56 ? 'paused'
57 : query.isStale()
58 ? 'stale'
59 : 'fresh'
60}
61β
62type Styles =
63 | React.CSSProperties
64 | ((props: Record<string, any>, theme: Theme) => React.CSSProperties)
65β
66export function styled<T extends keyof HTMLElementTagNameMap>(
67 type: T,
68 newStyles: Styles,
69 queries: Record<string, Styles> = {},
70) {
71 return React.forwardRef<HTMLElementTagNameMap[T], StyledComponent<T>>(
72 ({ style, ...rest }, ref) => {
73 const theme = useTheme()
74β
75 const mediaStyles = Object.entries(queries).reduce(
76 (current, [key, value]) => {
77 // eslint-disable-next-line react-hooks/rules-of-hooks
78 return useMediaQuery(key)
79 ? {
80 ...current,
81 ...(typeof value === 'function' ? value(rest, theme) : value),
82 }
83 : current
84 },
85 {},
86 )
87β
88 return React.createElement(type, {
89 ...rest,
90 style: {
91 ...(typeof newStyles === 'function'
92 ? newStyles(rest, theme)
93 : newStyles),
94 ...style,
95 ...mediaStyles,
96 },
97 ref,
98 })
99 },
100 )
101}
102β
103export function useIsMounted() {
104 const mountedRef = React.useRef(false)
105 const isMounted = React.useCallback(() => mountedRef.current, [])
106β
107 React[isServer ? 'useEffect' : 'useLayoutEffect'](() => {
108 mountedRef.current = true
109 return () => {
110 mountedRef.current = false
111 }
112 }, [])
113β
114 return isMounted
115}
116β
117/**
118 * Displays a string regardless the type of the data
119 * @param {unknown} value Value to be stringified
120 */
121export const displayValue = (value: unknown) => {
122 const name = Object.getOwnPropertyNames(Object(value))
123 const newValue = typeof value === 'bigint' ? `${value.toString()}n` : value
124β
125 return JSON.stringify(newValue, name)
126}