Open History to see the last commit
1import React from 'react'
2import type {
3 QueryCache,
4 QueryKey as QueryKeyType,
5} from '@tanstack/react-query'
6β
7import { Code } from '../styledComponents'
8β
9import useSubscribeToQueryCache from '../useSubscribeToQueryCache'
10import { getQueryStatusColor } from '../utils'
11import { defaultTheme as theme } from '../theme'
12β
13interface QueryRowProps {
14 queryKey: QueryKeyType
15 setActiveQueryHash: (hash: string) => void
16 activeQueryHash?: string
17 queryCache: QueryCache
18}
19β
20/**
21 * Row for a query in the query list
22 */
23const QueryRow = React.memo(
24 ({
25 queryKey,
26 setActiveQueryHash,
27 activeQueryHash,
28 queryCache,
29 }: QueryRowProps) => {
30 const queryHash =
31 useSubscribeToQueryCache(
32 queryCache,
33 () => queryCache.find({ queryKey })?.queryHash,
34 ) ?? ''
35β
36 const queryState = useSubscribeToQueryCache(
37 queryCache,
38 () => queryCache.find({ queryKey })?.state,
39 )
40β
41 const isStale =
42 useSubscribeToQueryCache(queryCache, () =>
43 queryCache.find({ queryKey })?.isStale(),
44 ) ?? false
45β
46 const isDisabled =
47 useSubscribeToQueryCache(queryCache, () =>
48 queryCache.find({ queryKey })?.isDisabled(),
49 ) ?? false
50β
51 const observerCount =
52 useSubscribeToQueryCache(queryCache, () =>
53 queryCache.find({ queryKey })?.getObserversCount(),
54 ) ?? 0
55β
56 if (!queryState) {
57 return null
58 }
59β
60 return (
61 <div
62 role="button"
63 aria-label={`Open query details for ${queryHash}`}
64 onClick={() =>
65 setActiveQueryHash(activeQueryHash === queryHash ? '' : queryHash)
66 }
67 style={{
68 display: 'flex',
69 borderBottom: `solid 1px ${theme.grayAlt}`,
70 cursor: 'pointer',
71 background:
72 queryHash === activeQueryHash ? 'rgba(255,255,255,.1)' : undefined,
73 }}
74 >
75 <div
76 style={{
77 flex: '0 0 auto',
78 width: '2em',
79 height: '2em',
80 background: getQueryStatusColor({
81 queryState,
82 isStale,
83 observerCount,
84 theme,
85 }),
86 display: 'flex',
87 alignItems: 'center',
88 justifyContent: 'center',
89 fontWeight: 'bold',
90 textShadow: isStale ? '0' : '0 0 10px black',
91 color: isStale ? 'black' : 'white',
92 }}
93 >
94 {observerCount}
95 </div>
96 {isDisabled ? (
97 <div
98 style={{
99 flex: '0 0 auto',
100 height: '2em',
101 background: theme.gray,
102 display: 'flex',
103 alignItems: 'center',
104 fontWeight: 'bold',
105 padding: '0 0.5em',
106 }}
107 >
108 disabled
109 </div>
110 ) : null}
111 <Code
112 style={{
113 padding: '.5em',
114 }}
115 >
116 {`${queryHash}`}
117 </Code>
118 </div>
119 )
120 },
121)
122β
123QueryRow.displayName = 'QueryRow'
124β
125export default QueryRow