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

stacklok / codegate-ui / 13261729647

11 Feb 2025 11:19AM CUT coverage: 68.226% (-0.5%) from 68.676%
13261729647

Pull #276

github

web-flow
Merge 5f42a30de into 7529f7310
Pull Request #276: feat: implement "Revert" button for workspace name

401 of 671 branches covered (59.76%)

Branch coverage included in aggregate %.

57 of 76 new or added lines in 11 files covered. (75.0%)

868 of 1189 relevant lines covered (73.0%)

73.49 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: true,
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