Open History to see the last commit
1import { createEffect, createSignal, onCleanup } from 'solid-js'
2import { useQueryDevtoolsContext } from '../contexts/QueryDevtoolsContext'
3import { DevtoolsOfflineContext } from '../contexts/DevtoolsOfflineContext'
4import type { DevtoolsOfflineState } from '../contexts/types'
5import type { ParentProps } from 'solid-js'
6β
7/**
8 * Provides its children with the offline state of the devtools `onlineManager`,
9 * and a function that toggles it.
10 *
11 * @param props - The `children` to render.
12 * @returns The `children`, wrapped in the context provider.
13 */
14export function DevtoolsOfflineProvider(props: ParentProps) {
15 const [offline, setOffline] = createSignal(false)
16 const context = useQueryDevtoolsContext()
17β
18 createEffect(() => {
19 const manager = context.onlineManager
20 setOffline(!manager.isOnline())
21 const unsubscribe = manager.subscribe((online) => setOffline(!online))
22 onCleanup(unsubscribe)
23 })
24β
25 const state: DevtoolsOfflineState = {
26 offline,
27 toggleOffline: () => {
28 const manager = context.onlineManager
29 manager.setOnline(!manager.isOnline())
30 },
31 }
32β
33 return (
34 <DevtoolsOfflineContext.Provider value={state}>
35 {props.children}
36 </DevtoolsOfflineContext.Provider>
37 )
38}