10
import * as monaco from 'monaco-editor';
11
import React, {useState, useCallback} from 'react';
12
import {Resizable} from 're-resizable';
13
-import {useSnackbar} from 'notistack';
13
import {useStore, useStoreDispatch} from '../StoreContext';
14
import {monacoOptions} from './monacoOptions';
16
-import {
17
- ConfigError,
18
- generateOverridePragmaFromConfig,
19
- updateSourceWithOverridePragma,
20
-} from '../../lib/configUtils';
15
16
// @ts-expect-error - webpack asset/source loader handles .d.ts files as strings
17
import compilerTypeDefs from 'babel-plugin-react-compiler/dist/index.d.ts';
22
const [isExpanded, setIsExpanded] = useState(false);
23
const store = useStore();
24
const dispatchStore = useStoreDispatch();
31
- const {enqueueSnackbar} = useSnackbar();
25
26
const toggleExpanded = useCallback(() => {
27
setIsExpanded(prev => !prev);
28
}, []);
29
37
- const handleApplyConfig: () => Promise<void> = async () => {
38
- try {
39
- const config = store.config || '';
40
-
41
- if (!config.trim()) {
42
- enqueueSnackbar(
43
- 'Config is empty. Please add configuration options first.',
44
- {
45
- variant: 'warning',
46
- },
47
- );
48
- return;
49
- }
50
- const newPragma = await generateOverridePragmaFromConfig(config);
51
- const updatedSource = updateSourceWithOverridePragma(
52
- store.source,
53
- newPragma,
54
- );
55
-
56
- dispatchStore({
57
- type: 'updateFile',
58
- payload: {
59
- source: updatedSource,
60
- config: config,
61
- },
62
- });
63
- } catch (error) {
64
- console.error('Failed to apply config:', error);
65
-
66
- if (error instanceof ConfigError && error.message.trim()) {
67
- enqueueSnackbar(error.message, {
68
- variant: 'error',
69
- });
70
- } else {
71
- enqueueSnackbar('Unexpected error: failed to apply config.', {
72
- variant: 'error',
73
- });
74
- }
75
- }
76
- };
77
-
30
const handleChange: (value: string | undefined) => void = value => {
31
if (value === undefined) return;
32
81
- // Only update the config
33
dispatchStore({
83
- type: 'updateFile',
34
+ type: 'updateConfig',
35
payload: {
85
- source: store.source,
36
config: value,
37
},
38
});
70
return (
71
<div className="flex flex-row relative">
72
{isExpanded ? (
123
- <>
124
- <Resizable
125
- className="border-r"
126
- minWidth={300}
127
- maxWidth={600}
128
- defaultSize={{width: 350, height: 'auto'}}
129
- enable={{right: true}}>
130
- <h2
131
- title="Minimize config editor"
132
- aria-label="Minimize config editor"
133
- onClick={toggleExpanded}
134
- className="p-4 duration-150 ease-in border-b cursor-pointer border-grey-200 font-light text-secondary hover:text-link">
135
- - Config Overrides
136
- </h2>
137
- <div className="h-[calc(100vh_-_3.5rem_-_4rem)]">
138
- <MonacoEditor
139
- path={'config.ts'}
140
- language={'typescript'}
141
- value={store.config}
142
- onMount={handleMount}
143
- onChange={handleChange}
144
- options={{
145
- ...monacoOptions,
146
- lineNumbers: 'off',
147
- folding: false,
148
- renderLineHighlight: 'none',
149
- scrollBeyondLastLine: false,
150
- hideCursorInOverviewRuler: true,
151
- overviewRulerBorder: false,
152
- overviewRulerLanes: 0,
153
- fontSize: 12,
154
- }}
155
- />
156
- </div>
157
- </Resizable>
158
- <button
159
- onClick={handleApplyConfig}
160
- title="Apply config overrides to input"
161
- aria-label="Apply config overrides to input"
162
- className="absolute right-0 top-1/2 transform -translate-y-1/2 translate-x-1/2 z-10 w-8 h-8 bg-blue-500 hover:bg-blue-600 text-white rounded-full border-2 border-white shadow-lg flex items-center justify-center text-sm font-medium transition-colors duration-150">
163
- →
164
- </button>
165
- </>
73
+ <Resizable
74
+ className="border-r"
75
+ minWidth={300}
76
+ maxWidth={600}
77
+ defaultSize={{width: 350, height: 'auto'}}
78
+ enable={{right: true}}>
79
+ <h2
80
+ title="Minimize config editor"
81
+ aria-label="Minimize config editor"
82
+ onClick={toggleExpanded}
83
+ className="p-4 duration-150 ease-in border-b cursor-pointer border-grey-200 font-light text-secondary hover:text-link">
84
+ - Config Overrides
85
+ </h2>
86
+ <div className="h-[calc(100vh_-_3.5rem_-_4rem)]">
87
+ <MonacoEditor
88
+ path={'config.ts'}
89
+ language={'typescript'}
90
+ value={store.config}
91
+ onMount={handleMount}
92
+ onChange={handleChange}
93
+ options={{
94
+ ...monacoOptions,
95
+ lineNumbers: 'off',
96
+ folding: false,
97
+ renderLineHighlight: 'none',
98
+ scrollBeyondLastLine: false,
99
+ hideCursorInOverviewRuler: true,
100
+ overviewRulerBorder: false,
101
+ overviewRulerLanes: 0,
102
+ fontSize: 12,
103
+ }}
104
+ />
105
+ </div>
106
+ </Resizable>
107
) : (
108
<div className="relative items-center h-full px-1 py-6 align-middle border-r border-grey-200">
109
<button