Skip to content

Commit

Permalink
improve the state editor hooks, allow initialState
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Oct 31, 2023
1 parent 6d36412 commit f6afd22
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-planes-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/react': minor
---

Add useHeadersEditorState and generic useEditorState hooks
79 changes: 47 additions & 32 deletions packages/graphiql-react/src/editor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,46 +333,61 @@ export function useAutoCompleteLeafs({
}, [getDefaultFieldNames, queryEditor, schema]);
}

export type InitialState = string | (() => string);

// https://react.dev/learn/you-might-not-need-an-effect

/**
* useState-like hook for current tab operations editor state
*/
export function useOperationsEditorState(): [
opString: string,
handleEditOperations: (content: string) => void,
] {
const { queryEditor } = useEditorContext({
export const useEditorState = (
editor: 'query' | 'variable' | 'header',
initialState?: InitialState,
) => {
const context = useEditorContext({
nonNull: true,
});
const opString = queryEditor?.getValue() ?? '';
const handleEditOperations = useCallback(
(value: string) => queryEditor?.setValue(value),
[queryEditor],
const initialValue =
typeof initialState === 'function' ? initialState() : initialState;
const editorInstance = context[`${editor}Editor` as const];
let valueString = '';
const editorValue = editorInstance?.getValue();
if (editorValue) {
valueString = editorValue;
} else {
valueString = initialValue || '';
}

const handleEditorValue = useCallback(
(value: string) => editorInstance?.setValue(value),
[editorInstance],
);
return useMemo(
() => [opString, handleEditOperations],
[opString, handleEditOperations],
return useMemo<[string, (val: string) => void]>(
() => [valueString, handleEditorValue],
[valueString, handleEditorValue],
);
};

/**
* useState-like hook for current tab operations editor state
*/
export function useOperationsEditorState(
initialState?: InitialState,
): [operations: string, setOperations: (content: string) => void] {
return useEditorState('query', initialState);
}

/**
* useState-like hook for variables tab operations editor state
* useState-like hook for current tab variables editor state
*/
export function useVariablesEditorState(): [
varsString: string,
handleEditVariables: (content: string) => void,
] {
const { variableEditor } = useEditorContext({
nonNull: true,
});
const varsString = variableEditor?.getValue() ?? '';
const handleEditVariables = useCallback(
(value: string) => variableEditor?.setValue(value),
[variableEditor],
);
return useMemo(
() => [varsString, handleEditVariables],
[varsString, handleEditVariables],
);
export function useVariablesEditorState(
initialState?: InitialState,
): [variables: string, setVariables: (content: string) => void] {
return useEditorState('variable', initialState);
}

/**
* useState-like hook for current tab variables editor state
*/
export function useHeadersEditorState(
initialState?: InitialState,
): [headers: string, setHeaders: (content: string) => void] {
return useEditorState('header', initialState);
}

0 comments on commit f6afd22

Please sign in to comment.