@@ -7,15 +7,15 @@ import {
77 visit ,
88} from 'graphql' ;
99import { VariableToType } from 'graphql-language-service' ;
10- import { ReactNode , useCallback , useMemo , useRef , useState } from 'react' ;
10+ import { ReactNode , useCallback , useMemo , useState } from 'react' ;
1111
1212import { useStorageContext } from '../storage' ;
1313import { createContextHook , createNullableContext } from '../utility/context' ;
1414import { STORAGE_KEY as STORAGE_KEY_HEADERS } from './header-editor' ;
1515import { useSynchronizeValue } from './hooks' ;
1616import { STORAGE_KEY_QUERY } from './query-editor' ;
1717import {
18- emptyTab ,
18+ createTab ,
1919 getDefaultTabState ,
2020 setPropertiesInActiveTab ,
2121 TabsState ,
@@ -239,22 +239,43 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
239239 useSynchronizeValue ( responseEditor , props . response ) ;
240240 useSynchronizeValue ( variableEditor , props . variables ) ;
241241
242- // We store this in state but never update it. By passing a function we only
243- // need to compute it lazily during the initial render.
244- const [ storedEditorValues ] = useState ( ( ) => ( {
245- headers : props . headers ?? storage ?. get ( STORAGE_KEY_HEADERS ) ?? null ,
246- query : props . query ?? storage ?. get ( STORAGE_KEY_QUERY ) ?? null ,
247- variables : props . variables ?? storage ?. get ( STORAGE_KEY_VARIABLES ) ?? null ,
248- } ) ) ;
249-
250- const [ tabState , setTabState ] = useState < TabsState > ( ( ) =>
251- getDefaultTabState ( { ...storedEditorValues , storage } ) ,
252- ) ;
253-
254242 const storeTabs = useStoreTabs ( {
255243 storage,
256244 shouldPersistHeaders : props . shouldPersistHeaders ,
257245 } ) ;
246+
247+ // We store this in state but never update it. By passing a function we only
248+ // need to compute it lazily during the initial render.
249+ const [ initialState ] = useState ( ( ) => {
250+ const query = props . query ?? storage ?. get ( STORAGE_KEY_QUERY ) ?? null ;
251+ const variables =
252+ props . variables ?? storage ?. get ( STORAGE_KEY_VARIABLES ) ?? null ;
253+ const headers = props . headers ?? storage ?. get ( STORAGE_KEY_HEADERS ) ?? null ;
254+ const response = props . response ?? '' ;
255+
256+ const tabState = getDefaultTabState ( {
257+ query,
258+ variables,
259+ headers,
260+ defaultQuery : props . defaultQuery || DEFAULT_QUERY ,
261+ storage,
262+ } ) ;
263+ storeTabs ( tabState ) ;
264+
265+ return {
266+ query :
267+ query ??
268+ ( tabState . activeTabIndex === 0 ? tabState . tabs [ 0 ] . query : null ) ??
269+ '' ,
270+ variables : variables ?? '' ,
271+ headers : headers ?? '' ,
272+ response,
273+ tabState,
274+ } ;
275+ } ) ;
276+
277+ const [ tabState , setTabState ] = useState < TabsState > ( initialState . tabState ) ;
278+
258279 const synchronizeActiveTabValues = useSynchronizeActiveTabValues ( {
259280 queryEditor,
260281 variableEditor,
@@ -274,7 +295,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
274295 // Make sure the current tab stores the latest values
275296 const updatedValues = synchronizeActiveTabValues ( current ) ;
276297 const updated = {
277- tabs : [ ...updatedValues . tabs , emptyTab ( ) ] ,
298+ tabs : [ ...updatedValues . tabs , createTab ( ) ] ,
278299 activeTabIndex : updatedValues . tabs . length ,
279300 } ;
280301 storeTabs ( updated ) ;
@@ -344,15 +365,6 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
344365 [ onEditOperationName , queryEditor , updateActiveTabValues ] ,
345366 ) ;
346367
347- const defaultQuery =
348- tabState . activeTabIndex > 0 ? '' : props . defaultQuery ?? DEFAULT_QUERY ;
349- const initialValues = useRef ( {
350- initialHeaders : storedEditorValues . headers ?? '' ,
351- initialQuery : storedEditorValues . query ?? defaultQuery ,
352- initialResponse : props . response ?? '' ,
353- initialVariables : storedEditorValues . variables ?? '' ,
354- } ) ;
355-
356368 const externalFragments = useMemo ( ( ) => {
357369 const map = new Map < string , FragmentDefinitionNode > ( ) ;
358370 if ( Array . isArray ( props . externalFragments ) ) {
@@ -397,7 +409,10 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
397409
398410 setOperationName,
399411
400- ...initialValues . current ,
412+ initialQuery : initialState . query ,
413+ initialVariables : initialState . variables ,
414+ initialHeaders : initialState . headers ,
415+ initialResponse : initialState . response ,
401416
402417 externalFragments,
403418 validationRules,
@@ -418,6 +433,8 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
418433
419434 setOperationName ,
420435
436+ initialState ,
437+
421438 externalFragments ,
422439 validationRules ,
423440
0 commit comments