Skip to content

Commit

Permalink
Code review + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
klippx committed Mar 20, 2024
1 parent 87f1fd1 commit fbb81ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
47 changes: 31 additions & 16 deletions packages/graphiql-react/src/editor/context.tsx
Expand Up @@ -37,6 +37,7 @@ import {
} from './tabs';
import { CodeMirrorEditor } from './types';
import { STORAGE_KEY as STORAGE_KEY_VARIABLES } from './variable-editor';
import { useExecutionContext } from '../execution';

export type CodeMirrorEditorWithOperationFacts = CodeMirrorEditor & {
documentAST: DocumentNode | null;
Expand Down Expand Up @@ -279,6 +280,7 @@ export type EditorContextProviderProps = {

export function EditorContextProvider(props: EditorContextProviderProps) {
const storage = useStorageContext();
const executionContext = useExecutionContext();
const [headerEditor, setHeaderEditor] = useState<CodeMirrorEditor | null>(
null,
);
Expand Down Expand Up @@ -382,7 +384,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
headerEditor,
responseEditor,
});
const { onTabChange, defaultHeaders, children } = props;
const { onTabChange, confirmCloseTab, defaultHeaders, children } = props;

const addTab = useCallback<EditorContextType['addTab']>(() => {
setTabState(current => {
Expand Down Expand Up @@ -442,29 +444,41 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
EditorContextType['closeTabConfirmation']
>(
async index => {
if (props.confirmCloseTab) {
const confirmation = await props.confirmCloseTab(index);
if (confirmCloseTab) {
const confirmation = await confirmCloseTab(index);
return confirmation;

Check warning on line 449 in packages/graphiql-react/src/editor/context.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/editor/context.tsx#L448-L449

Added lines #L448 - L449 were not covered by tests
}
return true;
},
[props.confirmCloseTab],
[confirmCloseTab],
);

const closeTab = useCallback<EditorContextType['closeTab']>(
index => {
setTabState(current => {
const updated = {
tabs: current.tabs.filter((_tab, i) => index !== i),
activeTabIndex: Math.max(current.activeTabIndex - 1, 0),
};
storeTabs(updated);
setEditorValues(updated.tabs[updated.activeTabIndex]);
onTabChange?.(updated);
return updated;
});
async index => {
if (await closeTabConfirmation(index)) {
if (index === tabState.activeTabIndex) {
executionContext?.stop();

Check warning on line 460 in packages/graphiql-react/src/editor/context.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/editor/context.tsx#L460

Added line #L460 was not covered by tests
}
setTabState(current => {
const updated = {
tabs: current.tabs.filter((_tab, i) => index !== i),
activeTabIndex: Math.max(current.activeTabIndex - 1, 0),
};
storeTabs(updated);
setEditorValues(updated.tabs[updated.activeTabIndex]);
onTabChange?.(updated);
return updated;
});
}
},
[onTabChange, setEditorValues, storeTabs],
[
onTabChange,
setEditorValues,
storeTabs,
closeTabConfirmation,
tabState.activeTabIndex,
executionContext,
],
);

const updateActiveTabValues = useCallback<
Expand Down Expand Up @@ -557,6 +571,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
addTab,
changeTab,
moveTab,
closeTabConfirmation,
closeTab,
updateActiveTabValues,

Expand Down
11 changes: 1 addition & 10 deletions packages/graphiql/src/components/GraphiQL.tsx
Expand Up @@ -547,16 +547,7 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
{tab.title}
</Tab.Button>
<Tab.Close
onClick={async () => {
if (
await editorContext.closeTabConfirmation(index)
) {
if (editorContext.activeTabIndex === index) {
executionContext.stop();
}
editorContext.closeTab(index);
}
}}
onClick={() => editorContext.closeTab(index)}
/>
</Tab>
))}
Expand Down

0 comments on commit fbb81ad

Please sign in to comment.