Skip to content

Commit

Permalink
restore tabs store if toggling on
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMightyPenguin committed Nov 25, 2022
1 parent d42c497 commit b477f32
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
5 changes: 3 additions & 2 deletions packages/graphiql-react/src/editor/__tests__/tabs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ describe('clearHeadersFromTabs', () => {
it('preserves tab state except for headers', () => {
const storage = createMockStorage();
const stateWithoutHeaders = {
hash: 123,
response: {
operationName: 'test',
query: 'query test {\n test {\n id\n }\n}',
test: {
a: 'test',
},
};
Expand Down
36 changes: 21 additions & 15 deletions packages/graphiql-react/src/editor/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
useStoreTabs,
useSynchronizeActiveTabValues,
clearHeadersFromTabs,
serializeTabState,
STORAGE_KEY as STORAGE_KEY_TABS,
} from './tabs';
import { CodeMirrorEditor } from './types';
import { STORAGE_KEY as STORAGE_KEY_VARIABLES } from './variable-editor';
Expand Down Expand Up @@ -274,25 +276,13 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
const [shouldPersistHeaders, setShouldPersistHeadersInternal] = useState(
() => {
const propValue = Boolean(props.shouldPersistHeaders);
return userControlledShouldPersistHeaders
? storage?.get(PERSIST_HEADERS_STORAGE_KEY) === 'true' || propValue
const isStored = storage?.get(PERSIST_HEADERS_STORAGE_KEY) !== null;
return userControlledShouldPersistHeaders && isStored
? storage?.get(PERSIST_HEADERS_STORAGE_KEY) === 'true'
: propValue;
},
);

const setShouldPersistHeaders = useCallback(
(persist: boolean) => {
// clean up when setting to false
if (!persist) {
storage?.set(STORAGE_KEY_HEADERS, '');
clearHeadersFromTabs(storage);
}
setShouldPersistHeadersInternal(persist);
storage?.set(PERSIST_HEADERS_STORAGE_KEY, persist.toString());
},
[storage],
);

useSynchronizeValue(headerEditor, props.headers);
useSynchronizeValue(queryEditor, props.query);
useSynchronizeValue(responseEditor, props.response);
Expand Down Expand Up @@ -337,6 +327,22 @@ export function EditorContextProvider(props: EditorContextProviderProps) {

const [tabState, setTabState] = useState<TabsState>(initialState.tabState);

const setShouldPersistHeaders = useCallback(
(persist: boolean) => {
if (persist) {
storage?.set(STORAGE_KEY_HEADERS, headerEditor?.getValue() ?? '');
const serializedTabs = serializeTabState(tabState, true);
storage?.set(STORAGE_KEY_TABS, serializedTabs);
} else {
storage?.set(STORAGE_KEY_HEADERS, '');
clearHeadersFromTabs(storage);
}
setShouldPersistHeadersInternal(persist);
storage?.set(PERSIST_HEADERS_STORAGE_KEY, persist.toString());
},
[storage, tabState, headerEditor],
);

const synchronizeActiveTabValues = useSynchronizeActiveTabValues({
queryEditor,
variableEditor,
Expand Down
23 changes: 14 additions & 9 deletions packages/graphiql-react/src/editor/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ export function useSynchronizeActiveTabValues({
);
}

export function serializeTabState(
tabState: TabsState,
shouldPersistHeaders = false,
) {
return JSON.stringify(tabState, (key, value) =>
key === 'hash' ||
key === 'response' ||
(!shouldPersistHeaders && key === 'headers')
? null
: value,
);
}

export function useStoreTabs({
storage,
shouldPersistHeaders,
Expand All @@ -226,15 +239,7 @@ export function useStoreTabs({
);
return useCallback(
(currentState: TabsState) => {
store(
JSON.stringify(currentState, (key, value) =>
key === 'hash' ||
key === 'response' ||
(!shouldPersistHeaders && key === 'headers')
? null
: value,
),
);
store(serializeTabState(currentState, shouldPersistHeaders));
},
[shouldPersistHeaders, store],
);
Expand Down

0 comments on commit b477f32

Please sign in to comment.