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

Function getDefaultTabState

packages/graphiql-react/src/editor/tabs.ts:70–152  ·  view source on GitHub ↗
({
  defaultQuery,
  defaultHeaders,
  headers,
  defaultTabs,
  query,
  variables,
  storage,
  shouldPersistHeaders,
}: {
  defaultQuery: string;
  defaultHeaders?: string;
  headers: string | null;
  defaultTabs?: TabDefinition[];
  query: string | null;
  variables: string | null;
  storage: StorageAPI | null;
  shouldPersistHeaders?: boolean;
})

Source from the content-addressed store, hash-verified

68};
69
70export function getDefaultTabState({
71 defaultQuery,
72 defaultHeaders,
73 headers,
74 defaultTabs,
75 query,
76 variables,
77 storage,
78 shouldPersistHeaders,
79}: {
80 defaultQuery: string;
81 defaultHeaders?: string;
82 headers: string | null;
83 defaultTabs?: TabDefinition[];
84 query: string | null;
85 variables: string | null;
86 storage: StorageAPI | null;
87 shouldPersistHeaders?: boolean;
88}) {
89 const storedState = storage?.get(STORAGE_KEY);
90 try {
91 if (!storedState) {
92 throw new Error('Storage for tabs is empty');
93 }
94 const parsed = JSON.parse(storedState);
95 // if headers are not persisted, do not derive the hash using default headers state
96 // or else you will get new tabs on every refresh
97 const headersForHash = shouldPersistHeaders ? headers : undefined;
98 if (isTabsState(parsed)) {
99 const expectedHash = hashFromTabContents({
100 query,
101 variables,
102 headers: headersForHash,
103 });
104 let matchingTabIndex = -1;
105
106 for (let index = 0; index < parsed.tabs.length; index++) {
107 const tab = parsed.tabs[index];
108 tab.hash = hashFromTabContents({
109 query: tab.query,
110 variables: tab.variables,
111 headers: tab.headers,
112 });
113 if (tab.hash === expectedHash) {
114 matchingTabIndex = index;
115 }
116 }
117
118 if (matchingTabIndex >= 0) {
119 parsed.activeTabIndex = matchingTabIndex;
120 } else {
121 const operationName = query ? fuzzyExtractOperationName(query) : null;
122 parsed.tabs.push({
123 id: guid(),
124 hash: expectedHash,
125 title: operationName || DEFAULT_TITLE,
126 query,
127 variables,

Callers 2

EditorContextProviderFunction · 0.90
tabs.spec.tsFile · 0.90

Calls 7

isTabsStateFunction · 0.85
hashFromTabContentsFunction · 0.85
guidFunction · 0.85
parseMethod · 0.80
pushMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected