Skip to content

Commit

Permalink
refactor(ui/code-editor): use object map
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaim Lev-Ari committed May 21, 2024
1 parent efb9578 commit 432f4b0
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions app/react/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import styles from './CodeEditor.module.css';
import { TextTip } from './Tip/TextTip';
import { StackVersionSelector } from './StackVersionSelector';

type Type = 'yaml' | 'shell' | 'dockerfile';
interface Props extends AutomationTestingProps {
id: string;
placeholder?: string;
type?: 'yaml' | 'shell' | 'dockerfile';
type?: Type;
readonly?: boolean;
onChange: (value: string) => void;
value: string;
Expand Down Expand Up @@ -60,6 +61,12 @@ const dockerFileLanguage = new LanguageSupport(
);
const shellLanguage = new LanguageSupport(StreamLanguage.define(shell));

const docTypeExtensionMap: Record<Type, LanguageSupport> = {
yaml: yamlLanguage,
dockerfile: dockerFileLanguage,
shell: shellLanguage,
};

export function CodeEditor({
id,
onChange,
Expand All @@ -76,14 +83,8 @@ export function CodeEditor({

const extensions = useMemo(() => {
const extensions = [];
if (type === 'yaml') {
extensions.push(yamlLanguage);
}
if (type === 'dockerfile') {
extensions.push(dockerFileLanguage);
}
if (type === 'shell') {
extensions.push(shellLanguage);
if (type && docTypeExtensionMap[type]) {
extensions.push(docTypeExtensionMap[type]);
}
return extensions;
}, [type]);
Expand Down

0 comments on commit 432f4b0

Please sign in to comment.