Repository: tannerlinsley/react-query
Pull request #2687 (2021-09-18 17:21:00)

fix(devtools): do not show disabled label for inactive queries

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/tannerlinsley/react-query/3vzwXVk2PTv6QTs5moX75vRpYgLu](https://vercel.com/tannerlinsley/react-query/3vzwXVk2PTv6QTs5moX75vRpYgLu)  
✅ Preview: *In Progress*


codesandbox[bot]: This pull request is automatically built and testable in [CodeSandbox](https://codesandbox.io).

  To see build info of the built libraries, click [here](https://ci.codesandbox.io/status/tannerlinsley/react-query/pr/2687/builds/170863) or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b1449be9d7cd46d0b7ca8d0773745f9dbbf6d159:

|Sandbox| Source |
|--|--|
|[tannerlinsley/react-query: basic](https://codesandbox.io/s/tannerlinsley-react-query-basic-r36cp)| Configuration |
|[tannerlinsley/react-query: basic-typescript](https://codesandbox.io/s/tannerlinsley-react-query-basic-typescript-rcnky)| Configuration |


Patch:
diff --git a/src/devtools/devtools.tsx b/src/devtools/devtools.tsx
index ad1f3eadb..d657c4710 100644
--- a/src/devtools/devtools.tsx
+++ b/src/devtools/devtools.tsx
@@ -595,72 +595,78 @@ export const ReactQueryDevtoolsPanel = React.forwardRef(
                 flex: '1',
               }}
             >
-              {queries.map((query, i) => (
-                <div
-                  key={query.queryHash || i}
-                  role="button"
-                  aria-label={`Open query details for ${query.queryHash}`}
-                  onClick={() =>
-                    setActiveQueryHash(
-                      activeQueryHash === query.queryHash ? '' : query.queryHash
-                    )
-                  }
-                  style={{
-                    display: 'flex',
-                    borderBottom: `solid 1px ${theme.grayAlt}`,
-                    cursor: 'pointer',
-                    background:
-                      query === activeQuery
-                        ? 'rgba(255,255,255,.1)'
-                        : undefined,
-                  }}
-                >
+              {queries.map((query, i) => {
+                const isDisabled =
+                  query.getObserversCount() > 0 && !query.isActive()
+                return (
                   <div
+                    key={query.queryHash || i}
+                    role="button"
+                    aria-label={`Open query details for ${query.queryHash}`}
+                    onClick={() =>
+                      setActiveQueryHash(
+                        activeQueryHash === query.queryHash
+                          ? ''
+                          : query.queryHash
+                      )
+                    }
                     style={{
-                      flex: '0 0 auto',
-                      width: '2rem',
-                      height: '2rem',
-                      background: getQueryStatusColor(query, theme),
                       display: 'flex',
-                      alignItems: 'center',
-                      justifyContent: 'center',
-                      fontWeight: 'bold',
-                      textShadow:
-                        getQueryStatusLabel(query) === 'stale'
-                          ? '0'
-                          : '0 0 10px black',
-                      color:
-                        getQueryStatusLabel(query) === 'stale'
-                          ? 'black'
-                          : 'white',
+                      borderBottom: `solid 1px ${theme.grayAlt}`,
+                      cursor: 'pointer',
+                      background:
+                        query === activeQuery
+                          ? 'rgba(255,255,255,.1)'
+                          : undefined,
                     }}
                   >
-                    {query.getObserversCount()}
-                  </div>
-                  {query.isActive() ? null : (
                     <div
                       style={{
                         flex: '0 0 auto',
+                        width: '2rem',
                         height: '2rem',
-                        background: theme.gray,
+                        background: getQueryStatusColor(query, theme),
                         display: 'flex',
                         alignItems: 'center',
+                        justifyContent: 'center',
                         fontWeight: 'bold',
-                        padding: '0 0.5rem',
+                        textShadow:
+                          getQueryStatusLabel(query) === 'stale'
+                            ? '0'
+                            : '0 0 10px black',
+                        color:
+                          getQueryStatusLabel(query) === 'stale'
+                            ? 'black'
+                            : 'white',
                       }}
                     >
-                      disabled
+                      {query.getObserversCount()}
                     </div>
-                  )}
-                  <Code
-                    style={{
-                      padding: '.5rem',
-                    }}
-                  >
-                    {`${query.queryHash}`}
-                  </Code>
-                </div>
-              ))}
+                    {isDisabled ? (
+                      <div
+                        style={{
+                          flex: '0 0 auto',
+                          height: '2rem',
+                          background: theme.gray,
+                          display: 'flex',
+                          alignItems: 'center',
+                          fontWeight: 'bold',
+                          padding: '0 0.5rem',
+                        }}
+                      >
+                        disabled
+                      </div>
+                    ) : null}
+                    <Code
+                      style={{
+                        padding: '.5rem',
+                      }}
+                    >
+                      {`${query.queryHash}`}
+                    </Code>
+                  </div>
+                )
+              })}
             </div>
           </div>
           {activeQuery ? (
diff --git a/src/devtools/utils.ts b/src/devtools/utils.ts
index 8846440bf..06e603506 100644
--- a/src/devtools/utils.ts
+++ b/src/devtools/utils.ts
@@ -10,10 +10,10 @@ export const isServer = typeof window === 'undefined'
 export function getQueryStatusColor(query, theme) {
   return query.state.isFetching
     ? theme.active
-    : query.isStale()
-    ? theme.warning
     : !query.getObserversCount()
     ? theme.gray
+    : query.isStale()
+    ? theme.warning
     : theme.success
 }
 


Test patch:
diff --git a/src/devtools/tests/devtools.test.tsx b/src/devtools/tests/devtools.test.tsx
index a0fc60997..562608890 100644
--- a/src/devtools/tests/devtools.test.tsx
+++ b/src/devtools/tests/devtools.test.tsx
@@ -260,6 +260,43 @@ describe('ReactQueryDevtools', () => {
     expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
   })
 
+  it('should not show a disabled label for inactive queries', async () => {
+    const { queryClient } = createQueryClient()
+
+    function Page() {
+      const { data } = useQuery('key', () => Promise.resolve('test'), {
+        enabled: false,
+      })
+
+      return (
+        <div>
+          <h1>{data}</h1>
+        </div>
+      )
+    }
+
+    function App() {
+      const [visible, setVisible] = React.useState(true)
+
+      return (
+        <div>
+          {visible ? <Page /> : null}
+          <button onClick={() => setVisible(false)}>Hide Query</button>
+        </div>
+      )
+    }
+
+    renderWithClient(queryClient, <App />, { initialIsOpen: true })
+
+    await screen.findByText(/disabled/i)
+
+    await act(async () => {
+      fireEvent.click(await screen.findByText(/hide query/i))
+    })
+
+    expect(screen.queryByText(/disabled/i)).not.toBeInTheDocument()
+  })
+
   it('should sort the queries according to the sorting filter', async () => {
     const { queryClient, queryCache } = createQueryClient()