• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stacklok / codegate-ui / 13700722011

06 Mar 2025 02:16PM CUT coverage: 69.903%. Remained the same
13700722011

Pull #373

github

web-flow
Merge 06515ee0f into db80e1ec2
Pull Request #373: chore(deps-dev): bump @hey-api/openapi-ts from 0.61.3 to 0.64.10

422 of 669 branches covered (63.08%)

Branch coverage included in aggregate %.

881 of 1195 relevant lines covered (73.72%)

72.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/components/react-query-provider.tsx
1
import { V1ListActiveWorkspacesResponse } from '@/api/generated'
2
import { v1ListActiveWorkspacesQueryKey } from '@/api/generated/@tanstack/react-query.gen'
3
import { getQueryCacheConfig } from '@/lib/react-query-utils'
4
import {
5
  QueryCacheNotifyEvent,
6
  QueryClient,
7
  QueryClientProvider as VendorQueryClientProvider,
8
} from '@tanstack/react-query'
9
import { ReactNode, useState, useEffect } from 'react'
10

11
/**
12
 * Responsible for determining whether a queryKey attached to a queryCache event
13
 * is for the "list active workspaces" query.
14
 */
15
function isActiveWorkspacesQueryKey(queryKey: unknown): boolean {
16
  return (
×
17
    Array.isArray(queryKey) &&
×
18
    queryKey[0]._id === v1ListActiveWorkspacesQueryKey()[0]?._id
19
  )
20
}
21

22
/**
23
 * Responsible for extracting the incoming active workspace name from the deeply
24
 * nested payload attached to a queryCache event.
25
 */
26
function getWorkspaceName(event: QueryCacheNotifyEvent): string | null {
27
  if ('action' in event === false || 'data' in event.action === false)
×
28
    return null
×
29
  return (
×
30
    (event.action.data as V1ListActiveWorkspacesResponse | undefined | null)
×
31
      ?.workspaces[0]?.name ?? null
32
  )
33
}
34

35
export function QueryClientProvider({ children }: { children: ReactNode }) {
36
  const [activeWorkspaceName, setActiveWorkspaceName] = useState<string | null>(
×
37
    null
38
  )
39

40
  const [queryClient] = useState(
×
41
    () =>
42
      new QueryClient({
×
43
        defaultOptions: {
44
          queries: {
45
            ...getQueryCacheConfig('no-cache'),
46
            refetchOnMount: false, // additional instances of a query shouldn't trigger background refetch
47
            refetchOnReconnect: true,
48
            refetchOnWindowFocus: true,
49
          },
50
        },
51
      })
52
  )
53

54
  useEffect(() => {
×
55
    const queryCache = queryClient.getQueryCache()
×
56
    const unsubscribe = queryCache.subscribe((event) => {
×
57
      if (
×
58
        event.type === 'updated' &&
×
59
        event.action.type === 'success' &&
60
        isActiveWorkspacesQueryKey(event.query.options.queryKey)
61
      ) {
62
        const newWorkspaceName: string | null = getWorkspaceName(event)
×
63
        if (
×
64
          newWorkspaceName === activeWorkspaceName ||
×
65
          newWorkspaceName === null
66
        )
67
          return
×
68

69
        setActiveWorkspaceName(newWorkspaceName)
×
70

71
        // eslint-disable-next-line no-restricted-syntax
72
        void queryClient.invalidateQueries({
×
73
          refetchType: 'all',
74
          // Avoid a continuous loop
75
          predicate(query) {
76
            return !isActiveWorkspacesQueryKey(query.queryKey)
×
77
          },
78
        })
79
      }
80
    })
81

82
    return () => {
×
83
      return unsubscribe()
×
84
    }
85
  }, [activeWorkspaceName, queryClient])
86

87
  return (
88
    <VendorQueryClientProvider client={queryClient}>
89
      {children}
90
    </VendorQueryClientProvider>
91
  )
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc