Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

DevtoolsUiContext.tsx

91 lines2.8 KB

1import { createContext, createSignal, useContext } from 'solid-js'
2import type { Accessor, JSX, Setter } from 'solid-js'
3import type {
4 MutationCache,
5 QueryCache,
6 QueryCacheNotifyEvent,
7} from '@tanstack/query-core'
8​
9interface DevtoolsUiProviderProps {
10 children: JSX.Element
12​
13interface QueryCacheBatcherEntry {
14 setter: Setter<any>
15 shouldUpdate: (event: QueryCacheNotifyEvent) => boolean
17​
18export interface DevtoolsUiContextValue {
19 selectedQueryHash: Accessor<string | null>
20 setSelectedQueryHash: Setter<string | null>
21 selectedMutationId: Accessor<number | null>
22 setSelectedMutationId: Setter<number | null>
23 panelWidth: Accessor<number>
24 setPanelWidth: Setter<number>
25 offline: Accessor<boolean>
26 setOffline: Setter<boolean>
27 /**
28 * Registry of active query-row subscriptions for this Devtools instance.
29 * Keeping this per-instance (instead of module-level) prevents two mounted
30 * Devtools panels from cross-notifying each other's rows when their
31 * underlying query caches differ.
32 */
33 queryCacheMap: Map<(q: Accessor<QueryCache>) => any, QueryCacheBatcherEntry>
34 mutationCacheMap: Map<(q: Accessor<MutationCache>) => any, Setter<any>>
36​
37const DevtoolsUiContext = createContext<DevtoolsUiContextValue | undefined>(
38 undefined,
40​
41/**
42 * Owns the UI/selection state and cache-subscription registries for a single
43 * mounted Devtools instance (button + panel, or standalone panel).
44 *
45 * This state used to live in module-level signals/maps in `Devtools.tsx`,
46 * which meant every `TanstackQueryDevtools` instance mounted on a page
47 * (e.g. two panels pointing at two different `QueryClient`s) shared the same
48 * selected query/mutation, panel width, and cache-subscription registry.
49 * That caused actions in one panel (selecting a query, resizing, etc.) to
50 * leak into every other panel. See:
51 * https://github.com/TanStack/query/issues/9681
52 */
53export function DevtoolsUiProvider(props: DevtoolsUiProviderProps) {
54 const [selectedQueryHash, setSelectedQueryHash] = createSignal<string | null>(
55 null,
56 )
57 const [selectedMutationId, setSelectedMutationId] = createSignal<
58 number | null
59 >(null)
60 const [panelWidth, setPanelWidth] = createSignal(0)
61 const [offline, setOffline] = createSignal(false)
62​
63 const value: DevtoolsUiContextValue = {
64 selectedQueryHash,
65 setSelectedQueryHash,
66 selectedMutationId,
67 setSelectedMutationId,
68 panelWidth,
69 setPanelWidth,
70 offline,
71 setOffline,
72 queryCacheMap: new Map(),
73 mutationCacheMap: new Map(),
74 }
75​
76 return (
77 <DevtoolsUiContext.Provider value={value}>
78 {props.children}
79 </DevtoolsUiContext.Provider>
80 )
82​
83export function useDevtoolsUiContext() {
84 const context = useContext(DevtoolsUiContext)
85 if (!context) {
86 throw new Error(
87 'useDevtoolsUiContext must be used within a DevtoolsUiProvider',
88 )
89 }
90 return context