|
| 1 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import { ChevronDownIcon, ChevronUpIcon } from '../../icons'; |
| 4 | +import { UnStyledButton } from '../../ui'; |
| 5 | +import { |
| 6 | + commonKeys, |
| 7 | + DEFAULT_EDITOR_THEME, |
| 8 | + DEFAULT_KEY_MAP, |
| 9 | + importCodeMirror, |
| 10 | +} from '../common'; |
| 11 | +import { useSynchronizeOption } from '../hooks'; |
| 12 | +import { IncrementalPayload } from '../tabs'; |
| 13 | +import { CodeMirrorEditor, CommonEditorProps } from '../types'; |
| 14 | + |
| 15 | +import '../style/codemirror.css'; |
| 16 | +import '../style/fold.css'; |
| 17 | +import '../style/lint.css'; |
| 18 | +import '../style/hint.css'; |
| 19 | +import '../style/info.css'; |
| 20 | +import '../style/jump.css'; |
| 21 | +import '../style/auto-insertion.css'; |
| 22 | +import '../style/editor.css'; |
| 23 | +import '../style/increments-editors.css'; |
| 24 | + |
| 25 | +type UseIncrementsEditorArgs = CommonEditorProps & { |
| 26 | + increment: IncrementalPayload; |
| 27 | +}; |
| 28 | + |
| 29 | +function useIncrementsEditor({ |
| 30 | + editorTheme = DEFAULT_EDITOR_THEME, |
| 31 | + keyMap = DEFAULT_KEY_MAP, |
| 32 | + increment, |
| 33 | +}: UseIncrementsEditorArgs) { |
| 34 | + const [editor, setEditor] = useState<CodeMirrorEditor | null>(null); |
| 35 | + |
| 36 | + const ref = useRef<HTMLDivElement>(null); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + let isActive = true; |
| 40 | + void importCodeMirror( |
| 41 | + [ |
| 42 | + import('codemirror/addon/fold/foldgutter'), |
| 43 | + import('codemirror/addon/fold/brace-fold'), |
| 44 | + import('codemirror/addon/dialog/dialog'), |
| 45 | + import('codemirror/addon/search/search'), |
| 46 | + import('codemirror/addon/search/searchcursor'), |
| 47 | + import('codemirror/addon/search/jump-to-line'), |
| 48 | + // @ts-expect-error |
| 49 | + import('codemirror/keymap/sublime'), |
| 50 | + import('codemirror-graphql/esm/results/mode'), |
| 51 | + import('codemirror-graphql/esm/utils/info-addon'), |
| 52 | + ], |
| 53 | + { useCommonAddons: false }, |
| 54 | + ).then(CodeMirror => { |
| 55 | + // Don't continue if the effect has already been cleaned up |
| 56 | + if (!isActive) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const container = ref.current; |
| 61 | + if (!container) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const newEditor = CodeMirror(container, { |
| 66 | + value: JSON.stringify(increment.payload, null, 2), |
| 67 | + lineWrapping: true, |
| 68 | + readOnly: true, |
| 69 | + theme: editorTheme, |
| 70 | + mode: 'graphql-results', |
| 71 | + foldGutter: true, |
| 72 | + gutters: ['CodeMirror-foldgutter'], |
| 73 | + // @ts-expect-error |
| 74 | + info: true, |
| 75 | + extraKeys: commonKeys, |
| 76 | + }); |
| 77 | + |
| 78 | + setEditor(newEditor); |
| 79 | + }); |
| 80 | + |
| 81 | + return () => { |
| 82 | + isActive = false; |
| 83 | + }; |
| 84 | + }, [editorTheme]); |
| 85 | + |
| 86 | + useSynchronizeOption(editor, 'keyMap', keyMap); |
| 87 | + |
| 88 | + return ref; |
| 89 | +} |
| 90 | + |
| 91 | +function IncrementEditor( |
| 92 | + props: UseIncrementsEditorArgs & { isInitial: boolean }, |
| 93 | +) { |
| 94 | + const [isOpen, setIsOpen] = useState(false); |
| 95 | + const incrementEditor = useIncrementsEditor(props); |
| 96 | + |
| 97 | + const toggleEditor = useCallback(() => setIsOpen(current => !current), []); |
| 98 | + |
| 99 | + return ( |
| 100 | + <div |
| 101 | + className="graphiql-increment-editor" |
| 102 | + style={isOpen ? { height: '30vh' } : {}} |
| 103 | + > |
| 104 | + <UnStyledButton |
| 105 | + className="graphiql-increment-editor-toggle" |
| 106 | + onClick={toggleEditor} |
| 107 | + > |
| 108 | + {props.isInitial ? 'Initial payload' : 'Increment'} (after{' '} |
| 109 | + {props.increment.timing / 1000}s) |
| 110 | + {isOpen ? ( |
| 111 | + <ChevronUpIcon className="graphiql-increment-editor-chevron" /> |
| 112 | + ) : ( |
| 113 | + <ChevronDownIcon className="graphiql-increment-editor-chevron" /> |
| 114 | + )} |
| 115 | + </UnStyledButton> |
| 116 | + <div |
| 117 | + ref={incrementEditor} |
| 118 | + className={`graphiql-editor ${isOpen ? '' : 'hidden'}`} |
| 119 | + /> |
| 120 | + </div> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +export type IncrementsEditorsProps = CommonEditorProps & { |
| 125 | + incrementalPayloads: IncrementalPayload[]; |
| 126 | +}; |
| 127 | + |
| 128 | +export function IncrementsEditors(props: IncrementsEditorsProps) { |
| 129 | + return ( |
| 130 | + <div className="graphiql-increments-editors"> |
| 131 | + {props.incrementalPayloads.map((increment, index) => ( |
| 132 | + <IncrementEditor |
| 133 | + key={increment.timing} |
| 134 | + isInitial={index === 0} |
| 135 | + increment={increment} |
| 136 | + {...props} |
| 137 | + /> |
| 138 | + ))} |
| 139 | + </div> |
| 140 | + ); |
| 141 | +} |
0 commit comments