MCPcopy
hub / github.com/graphql/graphiql / EditorContextProvider

Function EditorContextProvider

packages/graphiql-react/src/editor/context.tsx:257–502  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

255};
256
257export const EditorContextProvider: FC<EditorContextProviderProps> = props => {
258 const storage = useStorageContext();
259 const [headerEditor, setHeaderEditor] = useState<CodeMirrorEditor | null>(
260 null,
261 );
262 const [queryEditor, setQueryEditor] =
263 useState<CodeMirrorEditorWithOperationFacts | null>(null);
264 const [responseEditor, setResponseEditor] = useState<CodeMirrorEditor | null>(
265 null,
266 );
267 const [variableEditor, setVariableEditor] = useState<CodeMirrorEditor | null>(
268 null,
269 );
270
271 const [shouldPersistHeaders, setShouldPersistHeadersInternal] = useState(
272 () => {
273 const isStored = storage?.get(PERSIST_HEADERS_STORAGE_KEY) !== null;
274 return props.shouldPersistHeaders !== false && isStored
275 ? storage?.get(PERSIST_HEADERS_STORAGE_KEY) === 'true'
276 : Boolean(props.shouldPersistHeaders);
277 },
278 );
279
280 useSynchronizeValue(headerEditor, props.headers);
281 useSynchronizeValue(queryEditor, props.query);
282 useSynchronizeValue(responseEditor, props.response);
283 useSynchronizeValue(variableEditor, props.variables);
284
285 const storeTabs = useStoreTabs({
286 storage,
287 shouldPersistHeaders,
288 });
289
290 // We store this in state but never update it. By passing a function we only
291 // need to compute it lazily during the initial render.
292 const [initialState] = useState(() => {
293 const query = props.query ?? storage?.get(STORAGE_KEY_QUERY) ?? null;
294 const variables =
295 props.variables ?? storage?.get(STORAGE_KEY_VARIABLES) ?? null;
296 const headers = props.headers ?? storage?.get(STORAGE_KEY_HEADERS) ?? null;
297 const response = props.response ?? '';
298
299 const tabState = getDefaultTabState({
300 query,
301 variables,
302 headers,
303 defaultTabs: props.defaultTabs,
304 defaultQuery: props.defaultQuery || DEFAULT_QUERY,
305 defaultHeaders: props.defaultHeaders,
306 storage,
307 shouldPersistHeaders,
308 });
309 storeTabs(tabState);
310
311 return {
312 query:
313 query ??
314 (tabState.activeTabIndex === 0 ? tabState.tabs[0].query : null) ??

Callers

nothing calls this directly

Calls 9

useSynchronizeValueFunction · 0.90
useStoreTabsFunction · 0.90
getDefaultTabStateFunction · 0.90
useSetEditorValuesFunction · 0.90
visitFunction · 0.90
setShouldPersistHeadersFunction · 0.85
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected