Open History to see the last commit
1import { createContext, createSignal, useContext } from 'solid-js'
2import type { Accessor, JSX, Setter } from 'solid-js'
3β
4export interface DevtoolsState {
5 selectedQueryHash: Accessor<string | null>
6 setSelectedQueryHash: Setter<string | null>
7 selectedMutationId: Accessor<number | null>
8 setSelectedMutationId: Setter<number | null>
9 panelWidth: Accessor<number>
10 setPanelWidth: Setter<number>
11 offline: Accessor<boolean>
12 setOffline: Setter<boolean>
13}
14β
15const DevtoolsStateContext = createContext<DevtoolsState | undefined>(undefined)
16β
17export function useDevtoolsState(): DevtoolsState {
18 const state = useContext(DevtoolsStateContext)
19 if (!state) {
20 throw new Error(
21 'useDevtoolsState must be used within a DevtoolsStateProvider',
22 )
23 }
24 return state
25}
26β
27export function DevtoolsStateProvider(props: { children: JSX.Element }) {
28 // This state is intentionally created inside the provider component so that
29 // every mounted devtools instance gets its own isolated copy. Previously
30 // these signals lived at module scope, which meant that interacting with
31 // one devtools panel (e.g. selecting a query) leaked into every other
32 // panel on the page. See https://github.com/TanStack/query/issues/9681
33 const [selectedQueryHash, setSelectedQueryHash] = createSignal<string | null>(
34 null,
35 )
36 const [selectedMutationId, setSelectedMutationId] = createSignal<
37 number | null
38 >(null)
39 const [panelWidth, setPanelWidth] = createSignal(0)
40 const [offline, setOffline] = createSignal(false)
41β
42 return (
43 <DevtoolsStateContext.Provider
44 value={{
45 selectedQueryHash,
46 setSelectedQueryHash,
47 selectedMutationId,
48 setSelectedMutationId,
49 panelWidth,
50 setPanelWidth,
51 offline,
52 setOffline,
53 }}
54 >
55 {props.children}
56 </DevtoolsStateContext.Provider>
57 )
58}