saleor-apps-redis_apl/apps/emails-and-messages/src/modules/ui/code-editor.tsx

45 lines
1,000 B
TypeScript
Raw Normal View History

2023-03-07 18:58:02 +00:00
import React, { useCallback, useRef } from "react";
2023-03-06 13:01:03 +00:00
import Editor from "@monaco-editor/react";
import { useTheme } from "@saleor/macaw-ui";
type Props = {
onChange(value: string): void;
initialTemplate: string;
value: string;
language: string;
};
export const CodeEditor = ({ initialTemplate, onChange, value, language }: Props) => {
const { themeType } = useTheme();
const editorRef = useRef(null);
// @ts-ignore
function handleEditorDidMount(editor, monaco) {
editorRef.current = editor;
}
2023-03-07 18:58:02 +00:00
const handleOnChange = useCallback(
(value?: string) => {
console.log("ON CHANGE");
onChange(value ?? "");
},
[value]
);
2023-03-06 13:01:03 +00:00
return (
<>
<Editor
height="40vh"
width="100%"
value={value}
theme={themeType === "dark" ? "vs-dark" : "vs-light"}
defaultLanguage={language}
defaultValue={initialTemplate}
onMount={handleEditorDidMount}
2023-03-07 18:58:02 +00:00
onChange={handleOnChange}
2023-03-06 13:01:03 +00:00
/>
</>
);
};