Skip to content

Commit 16e70e1

Browse files
authored
fix(devtools): do not show disabled label for inactive queries and make statusColor match statusLabel (TanStack#2687)
1 parent b88cda4 commit 16e70e1

File tree

3 files changed

+96
-53
lines changed

3 files changed

+96
-53
lines changed

src/devtools/devtools.tsx

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -595,72 +595,78 @@ export const ReactQueryDevtoolsPanel = React.forwardRef(
595595
flex: '1',
596596
}}
597597
>
598-
{queries.map((query, i) => (
599-
<div
600-
key={query.queryHash || i}
601-
role="button"
602-
aria-label={`Open query details for ${query.queryHash}`}
603-
onClick={() =>
604-
setActiveQueryHash(
605-
activeQueryHash === query.queryHash ? '' : query.queryHash
606-
)
607-
}
608-
style={{
609-
display: 'flex',
610-
borderBottom: `solid 1px ${theme.grayAlt}`,
611-
cursor: 'pointer',
612-
background:
613-
query === activeQuery
614-
? 'rgba(255,255,255,.1)'
615-
: undefined,
616-
}}
617-
>
598+
{queries.map((query, i) => {
599+
const isDisabled =
600+
query.getObserversCount() > 0 && !query.isActive()
601+
return (
618602
<div
603+
key={query.queryHash || i}
604+
role="button"
605+
aria-label={`Open query details for ${query.queryHash}`}
606+
onClick={() =>
607+
setActiveQueryHash(
608+
activeQueryHash === query.queryHash
609+
? ''
610+
: query.queryHash
611+
)
612+
}
619613
style={{
620-
flex: '0 0 auto',
621-
width: '2rem',
622-
height: '2rem',
623-
background: getQueryStatusColor(query, theme),
624614
display: 'flex',
625-
alignItems: 'center',
626-
justifyContent: 'center',
627-
fontWeight: 'bold',
628-
textShadow:
629-
getQueryStatusLabel(query) === 'stale'
630-
? '0'
631-
: '0 0 10px black',
632-
color:
633-
getQueryStatusLabel(query) === 'stale'
634-
? 'black'
635-
: 'white',
615+
borderBottom: `solid 1px ${theme.grayAlt}`,
616+
cursor: 'pointer',
617+
background:
618+
query === activeQuery
619+
? 'rgba(255,255,255,.1)'
620+
: undefined,
636621
}}
637622
>
638-
{query.getObserversCount()}
639-
</div>
640-
{query.isActive() ? null : (
641623
<div
642624
style={{
643625
flex: '0 0 auto',
626+
width: '2rem',
644627
height: '2rem',
645-
background: theme.gray,
628+
background: getQueryStatusColor(query, theme),
646629
display: 'flex',
647630
alignItems: 'center',
631+
justifyContent: 'center',
648632
fontWeight: 'bold',
649-
padding: '0 0.5rem',
633+
textShadow:
634+
getQueryStatusLabel(query) === 'stale'
635+
? '0'
636+
: '0 0 10px black',
637+
color:
638+
getQueryStatusLabel(query) === 'stale'
639+
? 'black'
640+
: 'white',
650641
}}
651642
>
652-
disabled
643+
{query.getObserversCount()}
653644
</div>
654-
)}
655-
<Code
656-
style={{
657-
padding: '.5rem',
658-
}}
659-
>
660-
{`${query.queryHash}`}
661-
</Code>
662-
</div>
663-
))}
645+
{isDisabled ? (
646+
<div
647+
style={{
648+
flex: '0 0 auto',
649+
height: '2rem',
650+
background: theme.gray,
651+
display: 'flex',
652+
alignItems: 'center',
653+
fontWeight: 'bold',
654+
padding: '0 0.5rem',
655+
}}
656+
>
657+
disabled
658+
</div>
659+
) : null}
660+
<Code
661+
style={{
662+
padding: '.5rem',
663+
}}
664+
>
665+
{`${query.queryHash}`}
666+
</Code>
667+
</div>
668+
)
669+
})}
664670
</div>
665671
</div>
666672
{activeQuery ? (

src/devtools/tests/devtools.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,43 @@ describe('ReactQueryDevtools', () => {
260260
expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
261261
})
262262

263+
it('should not show a disabled label for inactive queries', async () => {
264+
const { queryClient } = createQueryClient()
265+
266+
function Page() {
267+
const { data } = useQuery('key', () => Promise.resolve('test'), {
268+
enabled: false,
269+
})
270+
271+
return (
272+
<div>
273+
<h1>{data}</h1>
274+
</div>
275+
)
276+
}
277+
278+
function App() {
279+
const [visible, setVisible] = React.useState(true)
280+
281+
return (
282+
<div>
283+
{visible ? <Page /> : null}
284+
<button onClick={() => setVisible(false)}>Hide Query</button>
285+
</div>
286+
)
287+
}
288+
289+
renderWithClient(queryClient, <App />, { initialIsOpen: true })
290+
291+
await screen.findByText(/disabled/i)
292+
293+
await act(async () => {
294+
fireEvent.click(await screen.findByText(/hide query/i))
295+
})
296+
297+
expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
298+
})
299+
263300
it('should sort the queries according to the sorting filter', async () => {
264301
const { queryClient, queryCache } = createQueryClient()
265302

src/devtools/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export const isServer = typeof window === 'undefined'
1010
export function getQueryStatusColor(query, theme) {
1111
return query.state.isFetching
1212
? theme.active
13-
: query.isStale()
14-
? theme.warning
1513
: !query.getObserversCount()
1614
? theme.gray
15+
: query.isStale()
16+
? theme.warning
1717
: theme.success
1818
}
1919

0 commit comments

Comments
 (0)