-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrequest-headers-editor.tsx
More file actions
69 lines (66 loc) · 2.03 KB
/
request-headers-editor.tsx
File metadata and controls
69 lines (66 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { FC, useEffect, useRef } from 'react';
import { useGraphiQL, useGraphiQLActions } from './provider';
import type { EditorProps } from '../types';
import { URI_NAME, KEY_BINDINGS, STORAGE_KEY } from '../constants';
import {
getOrCreateModel,
createEditor,
useChangeHandler,
onEditorContainerKeyDown,
pick,
cleanupDisposables,
cn,
} from '../utility';
import { useMonaco } from '../stores';
interface RequestHeadersEditorProps extends EditorProps {
/**
* Invoked when the contents of the request headers editor change.
* @param value - The new contents of the editor.
*/
onEdit?(value: string): void;
}
export const RequestHeadersEditor: FC<RequestHeadersEditorProps> = ({
onEdit,
editorOverrides,
...props
}) => {
const { setEditor, run, prettifyEditors, mergeQuery } = useGraphiQLActions();
const { initialHeaders, shouldPersistHeaders, uriInstanceId } = useGraphiQL(
pick('initialHeaders', 'shouldPersistHeaders', 'uriInstanceId'),
);
const ref = useRef<HTMLDivElement>(null!);
const monaco = useMonaco(state => state.monaco);
useChangeHandler(
onEdit,
shouldPersistHeaders ? STORAGE_KEY.headers : null,
'headers',
);
useEffect(() => {
if (!monaco) {
return;
}
const model = getOrCreateModel({
uri: `${uriInstanceId}${URI_NAME.requestHeaders}`,
value: initialHeaders,
});
const editor = createEditor(ref, { model, ...editorOverrides });
setEditor({ headerEditor: editor });
const disposables = [
editor.addAction({ ...KEY_BINDINGS.runQuery, run }),
editor.addAction({ ...KEY_BINDINGS.prettify, run: prettifyEditors }),
editor.addAction({ ...KEY_BINDINGS.mergeFragments, run: mergeQuery }),
editor,
model,
];
return cleanupDisposables(disposables);
}, [monaco]); // eslint-disable-line react-hooks/exhaustive-deps -- only on mount
return (
<div
ref={ref}
tabIndex={0}
onKeyDown={onEditorContainerKeyDown}
{...props}
className={cn('graphiql-editor', props.className)}
/>
);
};