Skip to content

Commit

Permalink
Code review + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
klippx committed Jun 4, 2024
1 parent 4aff24d commit ef9e787
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
Original file line number Diff line number Diff line change
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;
}
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();
}
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
Original file line number Diff line number Diff line change
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 ef9e787

Please sign in to comment.