|
| 1 | +import { useAtomValue } from "jotai"; |
| 2 | +import { useEffect, useRef } from "react"; |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import useSWR from "swr"; |
| 5 | +import { monaco } from "@/services/monaco"; |
| 6 | +import { themeMode } from "@/store"; |
| 7 | +import { getRuntimeYaml, useClash } from "@nyanpasu/interface"; |
| 8 | +import { BaseDialog } from "@nyanpasu/ui"; |
| 9 | + |
| 10 | +export type RuntimeConfigDiffDialogProps = { |
| 11 | + open: boolean; |
| 12 | + onClose: () => void; |
| 13 | +}; |
| 14 | + |
| 15 | +export default function RuntimeConfigDiffDialog({ |
| 16 | + open, |
| 17 | + onClose, |
| 18 | +}: RuntimeConfigDiffDialogProps) { |
| 19 | + const { t } = useTranslation(); |
| 20 | + const { getProfiles, getProfileFile } = useClash(); |
| 21 | + const currentProfileUid = getProfiles.data?.current; |
| 22 | + const mode = useAtomValue(themeMode); |
| 23 | + const { |
| 24 | + data: runtimeConfig, |
| 25 | + isLoading: isLoadingRuntimeConfig, |
| 26 | + error: errorRuntimeConfig, |
| 27 | + } = useSWR(open ? "/getRuntimeConfigYaml" : null, getRuntimeYaml); |
| 28 | + const { |
| 29 | + data: profileConfig, |
| 30 | + isLoading: isLoadingProfileConfig, |
| 31 | + error: errorProfileConfig, |
| 32 | + } = useSWR( |
| 33 | + open ? `/readProfileFile?uid=${currentProfileUid}` : null, |
| 34 | + async (key) => { |
| 35 | + const url = new URL(key, window.location.origin); |
| 36 | + return await getProfileFile(url.searchParams.get("uid")!); |
| 37 | + }, |
| 38 | + { |
| 39 | + revalidateOnFocus: true, |
| 40 | + refreshInterval: 0, |
| 41 | + }, |
| 42 | + ); |
| 43 | + const monacoRef = useRef<typeof monaco | null>(null); |
| 44 | + const editorRef = useRef<monaco.editor.IStandaloneDiffEditor | null>(null); |
| 45 | + const domRef = useRef<HTMLDivElement>(null); |
| 46 | + useEffect(() => { |
| 47 | + if (open && runtimeConfig && profileConfig) { |
| 48 | + console.log("init monaco"); |
| 49 | + const run = async () => { |
| 50 | + const { monaco } = await import("@/services/monaco"); |
| 51 | + monacoRef.current = monaco; |
| 52 | + console.log(domRef.current); |
| 53 | + editorRef.current = monaco.editor.createDiffEditor(domRef.current!, { |
| 54 | + theme: mode === "light" ? "vs" : "vs-dark", |
| 55 | + minimap: { enabled: false }, |
| 56 | + automaticLayout: true, |
| 57 | + readOnly: true, |
| 58 | + }); |
| 59 | + editorRef.current.setModel({ |
| 60 | + original: monaco.editor.createModel(profileConfig, "yaml"), |
| 61 | + modified: monaco.editor.createModel(runtimeConfig, "yaml"), |
| 62 | + }); |
| 63 | + }; |
| 64 | + run().catch(console.error); |
| 65 | + } |
| 66 | + return () => { |
| 67 | + monacoRef.current = null; |
| 68 | + editorRef.current?.dispose(); |
| 69 | + }; |
| 70 | + }, [mode, open, runtimeConfig, profileConfig]); |
| 71 | + console.log(currentProfileUid); |
| 72 | + if (!currentProfileUid) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <BaseDialog title={t("Runtime Config")} open={open} onClose={onClose}> |
| 78 | + <div className="xs:w-[95vw] h-full w-[80vw] px-4"> |
| 79 | + <div ref={domRef} className="h-[75vh] w-full" /> |
| 80 | + </div> |
| 81 | + </BaseDialog> |
| 82 | + ); |
| 83 | +} |
0 commit comments