config-viewer.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { useEffect, useRef, useState } from "react";
  2. import { useTranslation } from "react-i18next";
  3. import { useRecoilValue } from "recoil";
  4. import {
  5. Button,
  6. Chip,
  7. Dialog,
  8. DialogActions,
  9. DialogContent,
  10. DialogTitle,
  11. } from "@mui/material";
  12. import { InfoRounded } from "@mui/icons-material";
  13. import { atomThemeMode } from "../../../services/states";
  14. import { getRunningConfig } from "../../../services/cmds";
  15. import "monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution.js";
  16. import "monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution.js";
  17. import "monaco-editor/esm/vs/editor/contrib/folding/browser/folding.js";
  18. import { editor } from "monaco-editor/esm/vs/editor/editor.api";
  19. const ConfigViewer = () => {
  20. const { t } = useTranslation();
  21. const [open, setOpen] = useState(false);
  22. const editorRef = useRef<any>();
  23. const instanceRef = useRef<editor.IStandaloneCodeEditor | null>(null);
  24. const themeMode = useRecoilValue(atomThemeMode);
  25. useEffect(() => {
  26. if (!open) return;
  27. getRunningConfig().then((data) => {
  28. const dom = editorRef.current;
  29. if (!dom) return;
  30. if (instanceRef.current) instanceRef.current.dispose();
  31. instanceRef.current = editor.create(editorRef.current, {
  32. value: data ?? "# Error\n",
  33. language: "yaml",
  34. theme: themeMode === "light" ? "vs" : "vs-dark",
  35. minimap: { enabled: false },
  36. readOnly: true,
  37. });
  38. });
  39. return () => {
  40. if (instanceRef.current) {
  41. instanceRef.current.dispose();
  42. instanceRef.current = null;
  43. }
  44. };
  45. }, [open]);
  46. return (
  47. <>
  48. <Dialog open={open} onClose={() => setOpen(false)}>
  49. <DialogTitle>
  50. {t("Running Config")} <Chip label="ReadOnly" size="small" />
  51. </DialogTitle>
  52. <DialogContent sx={{ width: 520, pb: 1 }}>
  53. <div style={{ width: "100%", height: "420px" }} ref={editorRef} />
  54. </DialogContent>
  55. <DialogActions>
  56. <Button onClick={() => setOpen(false)}>{t("Back")}</Button>
  57. </DialogActions>
  58. </Dialog>
  59. <InfoRounded
  60. fontSize="small"
  61. style={{ cursor: "pointer", opacity: 0.75 }}
  62. onClick={() => setOpen(true)}
  63. />
  64. </>
  65. );
  66. };
  67. export default ConfigViewer;