Skip to content

Commit 766e30f

Browse files
committed
Refactor editor mode handling to use InputMode constants
1 parent 34eeedc commit 766e30f

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

workspaces/ballerina/ballerina-side-panel/src/components/editors/ExpandedEditor/ExpandedEditor.tsx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ const TitleWrapper = styled.div`
147147
* Map of mode components - add new modes here
148148
*/
149149
const MODE_COMPONENTS: Record<EditorMode, React.ComponentType<any>> = {
150-
text: TextMode,
151-
prompt: PromptMode,
152-
expression: ExpressionMode,
153-
template: TemplateMode
150+
[InputMode.TEXT]: TextMode,
151+
[InputMode.PROMPT]: PromptMode,
152+
[InputMode.EXP]: ExpressionMode,
153+
[InputMode.TEMPLATE]: TemplateMode
154154
};
155155

156156
export const ExpandedEditor: React.FC<ExpandedPromptEditorProps> = ({
@@ -176,24 +176,16 @@ export const ExpandedEditor: React.FC<ExpandedPromptEditorProps> = ({
176176

177177
// Determine mode - use prop if provided, otherwise auto-detect
178178
const defaultMode: EditorMode = propMode ?? (
179-
promptFields.includes(field.key) ? "prompt" : "text"
179+
promptFields.includes(field.key) ? InputMode.PROMPT : InputMode.TEXT
180180
);
181181

182182
const [mode, setMode] = useState<EditorMode>(defaultMode);
183-
const [showPreview, setShowPreview] = useState(false);
184183
const [mouseDownTarget, setMouseDownTarget] = useState<EventTarget | null>(null);
185184

186185
useEffect(() => {
187186
setMode(defaultMode);
188187
}, [defaultMode]);
189188

190-
useEffect(() => {
191-
// Text mode and template mode don't support preview (simplified)
192-
if (mode === "text" || mode === "template") {
193-
setShowPreview(false);
194-
}
195-
}, [mode]);
196-
197189
const handleMinimize = () => {
198190
onClose();
199191
};
@@ -220,8 +212,8 @@ export const ExpandedEditor: React.FC<ExpandedPromptEditorProps> = ({
220212
value: value,
221213
onChange: onChange,
222214
field,
223-
// Props for prompt mode (now uses EditorModeExpressionProps)
224-
...(mode === "prompt" && {
215+
// Props for prompt mode
216+
...(mode === InputMode.PROMPT && {
225217
completions,
226218
fileName,
227219
targetLineRange,
@@ -234,7 +226,7 @@ export const ExpandedEditor: React.FC<ExpandedPromptEditorProps> = ({
234226
inputMode
235227
}),
236228
// Props for expression mode
237-
...(mode === "expression" && {
229+
...(mode === InputMode.EXP && {
238230
completions,
239231
fileName,
240232
targetLineRange,
@@ -245,8 +237,8 @@ export const ExpandedEditor: React.FC<ExpandedPromptEditorProps> = ({
245237
error,
246238
formDiagnostics
247239
}),
248-
// Props for template mode (simplified - same as expression but with inputMode)
249-
...(mode === "template" && {
240+
// Props for template mode
241+
...(mode === InputMode.TEMPLATE && {
250242
completions,
251243
fileName,
252244
targetLineRange,

workspaces/ballerina/ballerina-side-panel/src/components/editors/ExpandedEditor/modes/types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ export interface EditorModeExpressionProps extends EditorModeProps {
8383
inputMode?: InputMode;
8484
}
8585

86-
/**
87-
* Mode type identifier
88-
*/
89-
export type EditorMode = "text" | "prompt" | "expression" | "template";
86+
export const EXPANDABLE_MODES = [
87+
InputMode.TEXT,
88+
InputMode.PROMPT,
89+
InputMode.EXP,
90+
InputMode.TEMPLATE
91+
] as const;
92+
93+
export type EditorMode = typeof EXPANDABLE_MODES[number];

workspaces/ballerina/ballerina-side-panel/src/components/editors/ExpressionEditor.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
RequiredFormInput,
2929
ThemeColors
3030
} from '@wso2/ui-toolkit';
31-
import { getPropertyFromFormField, sanitizeType } from './utils';
31+
import { getPropertyFromFormField, isExpandableMode, sanitizeType, toEditorMode } from './utils';
3232
import { FormField, FormExpressionEditorProps, HelperpaneOnChangeOptions } from '../Form/types';
3333
import { useFormContext } from '../../context';
3434
import {
@@ -619,7 +619,7 @@ export const ExpressionEditor = (props: ExpressionEditorProps) => {
619619
}
620620
};
621621

622-
const onOpenExpandedMode = !props.isInExpandedMode
622+
const onOpenExpandedMode = !props.isInExpandedMode && isExpandableMode(inputMode)
623623
? handleOpenExpandedMode
624624
: undefined;
625625

@@ -771,7 +771,7 @@ export const ExpressionEditor = (props: ExpressionEditorProps) => {
771771
formDiagnostics && formDiagnostics.length > 0 &&
772772
<ErrorBanner errorMsg={formDiagnostics.map(d => d.message).join(', ')} />
773773
}
774-
{onOpenExpandedMode && (
774+
{onOpenExpandedMode && toEditorMode(inputModeRef.current) && (
775775
<ExpandedEditor
776776
isOpen={isExpandedModalOpen}
777777
field={field}
@@ -818,15 +818,7 @@ export const ExpressionEditor = (props: ExpressionEditorProps) => {
818818
setIsExpandedModalOpen(false)
819819
}}
820820
onSave={handleSaveExpandedMode}
821-
mode={
822-
inputModeRef.current === InputMode.EXP
823-
? "expression"
824-
: inputModeRef.current === InputMode.PROMPT
825-
? "prompt"
826-
: inputModeRef.current === InputMode.TEMPLATE
827-
? "template"
828-
: undefined
829-
}
821+
mode={toEditorMode(inputModeRef.current)!}
830822
completions={completions}
831823
fileName={effectiveFileName}
832824
targetLineRange={effectiveTargetLineRange}

workspaces/ballerina/ballerina-side-panel/src/components/editors/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import { startCase } from "lodash";
2020
import { FormField } from "../Form/types";
2121
import { ExpressionProperty } from "@wso2/ballerina-core";
22+
import { InputMode } from "../..";
23+
import { EditorMode } from "./ExpandedEditor";
24+
import { EXPANDABLE_MODES } from "./ExpandedEditor/modes/types";
2225

2326
export function isDropdownField(field: FormField) {
2427
return field.type === "MULTIPLE_SELECT" || field.type === "SINGLE_SELECT" || field.type?.toUpperCase() === "ENUM";
@@ -105,3 +108,11 @@ export const getValueForTextModeEditor = (value: string) => {
105108
}
106109
return value;
107110
}
111+
112+
export function isExpandableMode(mode: InputMode): mode is EditorMode {
113+
return EXPANDABLE_MODES.includes(mode as EditorMode);
114+
}
115+
116+
export function toEditorMode(mode: InputMode): EditorMode | undefined {
117+
return isExpandableMode(mode) ? mode : undefined;
118+
}

0 commit comments

Comments
 (0)