2023-12-13 00:22:02 +01:00
|
|
|
import useSWR from "swr";
|
|
|
|
import * as monaco from "monaco-editor";
|
|
|
|
import { configureMonacoYaml } from "monaco-yaml";
|
2023-12-16 00:24:50 +01:00
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
2023-12-13 00:22:02 +01:00
|
|
|
import { useApiHost } from "@/api";
|
2023-12-08 14:33:22 +01:00
|
|
|
import Heading from "@/components/ui/heading";
|
2023-12-13 00:22:02 +01:00
|
|
|
import ActivityIndicator from "@/components/ui/activity-indicator";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import axios from "axios";
|
|
|
|
import copy from "copy-to-clipboard";
|
|
|
|
import { useTheme } from "@/context/theme-provider";
|
|
|
|
|
|
|
|
type SaveOptions = "saveonly" | "restart";
|
2023-12-08 14:33:22 +01:00
|
|
|
|
|
|
|
function ConfigEditor() {
|
2023-12-13 00:22:02 +01:00
|
|
|
const apiHost = useApiHost();
|
|
|
|
|
|
|
|
const { data: config } = useSWR<string>("config/raw");
|
|
|
|
|
|
|
|
const { theme } = useTheme();
|
|
|
|
const [success, setSuccess] = useState<string | undefined>();
|
|
|
|
const [error, setError] = useState<string | undefined>();
|
|
|
|
|
2023-12-20 15:33:57 +01:00
|
|
|
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
|
|
|
|
const modelRef = useRef<monaco.editor.IEditorModel | null>(null);
|
|
|
|
const configRef = useRef<HTMLDivElement | null>(null);
|
2023-12-13 00:22:02 +01:00
|
|
|
|
|
|
|
const onHandleSaveConfig = useCallback(
|
|
|
|
async (save_option: SaveOptions) => {
|
|
|
|
if (!editorRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(
|
|
|
|
`config/save?save_option=${save_option}`,
|
|
|
|
editorRef.current.getValue(),
|
|
|
|
{
|
|
|
|
headers: { "Content-Type": "text/plain" },
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
setError("");
|
|
|
|
setSuccess(response.data.message);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setSuccess("");
|
|
|
|
|
|
|
|
if (error.response) {
|
|
|
|
setError(error.response.data.message);
|
|
|
|
} else {
|
|
|
|
setError(error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[editorRef]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleCopyConfig = useCallback(async () => {
|
|
|
|
if (!editorRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(editorRef.current.getValue());
|
|
|
|
}, [editorRef]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!config) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modelRef.current != null) {
|
|
|
|
// we don't need to recreate the editor if it already exists
|
2023-12-20 15:33:57 +01:00
|
|
|
editorRef.current?.layout();
|
2023-12-13 00:22:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const modelUri = monaco.Uri.parse("a://b/api/config/schema.json");
|
|
|
|
|
|
|
|
if (monaco.editor.getModels().length > 0) {
|
|
|
|
modelRef.current = monaco.editor.getModel(modelUri);
|
|
|
|
} else {
|
|
|
|
modelRef.current = monaco.editor.createModel(config, "yaml", modelUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
configureMonacoYaml(monaco, {
|
|
|
|
enableSchemaRequest: true,
|
|
|
|
hover: true,
|
|
|
|
completion: true,
|
|
|
|
validate: true,
|
|
|
|
format: true,
|
|
|
|
schemas: [
|
|
|
|
{
|
|
|
|
uri: `${apiHost}api/config/schema.json`,
|
|
|
|
fileMatch: [String(modelUri)],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2023-12-20 15:33:57 +01:00
|
|
|
const container = configRef.current;
|
2023-12-13 00:22:02 +01:00
|
|
|
|
2023-12-20 15:33:57 +01:00
|
|
|
if (container != null) {
|
2023-12-13 00:22:02 +01:00
|
|
|
editorRef.current = monaco.editor.create(container, {
|
|
|
|
language: "yaml",
|
|
|
|
model: modelRef.current,
|
|
|
|
scrollBeyondLastLine: false,
|
|
|
|
theme: theme == "dark" ? "vs-dark" : "vs-light",
|
|
|
|
});
|
|
|
|
}
|
2023-12-20 15:33:57 +01:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
configRef.current = null;
|
|
|
|
editorRef.current = null;
|
|
|
|
modelRef.current = null;
|
|
|
|
};
|
2023-12-13 00:22:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!config) {
|
|
|
|
return <ActivityIndicator />;
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2024-01-31 13:29:18 +01:00
|
|
|
<div className="absolute top-28 bottom-16 right-0 left-0 md:left-24 lg:left-60">
|
2023-12-13 00:22:02 +01:00
|
|
|
<div className="lg:flex justify-between mr-1">
|
|
|
|
<Heading as="h2">Config</Heading>
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
className="mx-1"
|
|
|
|
onClick={(_) => handleCopyConfig()}
|
|
|
|
>
|
|
|
|
Copy Config
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
className="mx-1"
|
|
|
|
onClick={(_) => onHandleSaveConfig("restart")}
|
|
|
|
>
|
|
|
|
Save & Restart
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
className="mx-1"
|
|
|
|
onClick={(_) => onHandleSaveConfig("saveonly")}
|
|
|
|
>
|
|
|
|
Save Only
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-01-04 00:40:14 +01:00
|
|
|
{success && <div className="max-h-20 text-success">{success}</div>}
|
2023-12-13 00:22:02 +01:00
|
|
|
{error && (
|
2024-01-04 00:40:14 +01:00
|
|
|
<div className="p-4 overflow-scroll text-danger whitespace-pre-wrap">
|
2023-12-13 00:22:02 +01:00
|
|
|
{error}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2023-12-20 15:33:57 +01:00
|
|
|
<div ref={configRef} className="h-full mt-2" />
|
2023-12-13 00:22:02 +01:00
|
|
|
</div>
|
2023-12-08 14:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConfigEditor;
|