diff --git a/components/HooksEditor.tsx b/components/HooksEditor.tsx index 70c4cc4..4f0fac4 100644 --- a/components/HooksEditor.tsx +++ b/components/HooksEditor.tsx @@ -11,6 +11,7 @@ import Container from "./Container"; import dark from "../theme/editor/amy.json"; import light from "../theme/editor/xcode_default.json"; import { saveFile } from "../state/actions"; +import { apiHeaderFiles } from "../state/constants"; import state from "../state"; import EditorNavigation from "./EditorNavigation"; @@ -26,12 +27,26 @@ loader.config({ }, }); +const validateWritability = (editor: monaco.editor.IStandaloneCodeEditor) => { + const currPath = editor.getModel()?.uri.path; + if (apiHeaderFiles.find(h => currPath?.endsWith(h))) { + editor.updateOptions({ readOnly: true }); + } else { + editor.updateOptions({ readOnly: false }); + } +}; + const HooksEditor = () => { const editorRef = useRef(); const subscriptionRef = useRef(null); const snap = useSnapshot(state); const router = useRouter(); const { theme } = useTheme(); + + useEffect(() => { + if (editorRef.current) validateWritability(editorRef.current); + }, [snap.active]); + useEffect(() => { return () => { subscriptionRef?.current?.close(); @@ -58,9 +73,9 @@ const HooksEditor = () => { language={snap.files?.[snap.active]?.language} path={`file://work/c/${snap.files?.[snap.active]?.name}`} defaultValue={snap.files?.[snap.active]?.content} - beforeMount={(monaco) => { + beforeMount={monaco => { if (!snap.editorCtx) { - snap.files.forEach((file) => + snap.files.forEach(file => monaco.editor.createModel( file.content, file.language, @@ -85,7 +100,7 @@ const HooksEditor = () => { // listen when the web socket is opened listen({ webSocket: webSocket as WebSocket, - onConnection: (connection) => { + onConnection: connection => { // create and start the language client const languageClient = createLanguageClient(connection); const disposable = languageClient.start(); @@ -124,12 +139,10 @@ const HooksEditor = () => { enabled: true, }, }); - editor.addCommand( - monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, - () => { - saveFile(); - } - ); + editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => { + saveFile(); + }); + validateWritability(editor) }} theme={theme === "dark" ? "dark" : "light"} /> @@ -147,9 +160,7 @@ const HooksEditor = () => { - + { const router = useRouter(); @@ -82,12 +83,8 @@ const Navigation = () => { ) : ( <> - - {snap.files?.[0]?.name || "XRPL Hooks"} - - + {snap.files?.[0]?.name || "XRPL Hooks"} + {snap.files.length > 0 ? "Gist: " : "Playground"} {snap.files.length > 0 && @@ -99,10 +96,7 @@ const Navigation = () => { {router.isReady && ( - (state.mainModalOpen = open)} - > + (state.mainModalOpen = open)}> - - - - diff --git a/state/actions/fetchFiles.ts b/state/actions/fetchFiles.ts index a97b5c4..e219b96 100644 --- a/state/actions/fetchFiles.ts +++ b/state/actions/fetchFiles.ts @@ -1,6 +1,8 @@ import { Octokit } from "@octokit/core"; import Router from "next/router"; import state from '../index'; +import { templateFileIds } from '../constants'; + const octokit = new Octokit(); @@ -17,6 +19,17 @@ export const fetchFiles = (gistId: string) => { octokit .request("GET /gists/{gist_id}", { gist_id: gistId }) + .then(res => { + if (!Object.values(templateFileIds).includes(gistId)) { + return res + } + // in case of templates, fetch header file(s) and append to res + return octokit.request("GET /gists/{gist_id}", { gist_id: templateFileIds.headers }).then(({ data: { files: headerFiles } }) => { + const files = { ...res.data.files, ...headerFiles } + res.data.files = files + return res + }) + }) .then((res) => { if (res.data.files && Object.keys(res.data.files).length > 0) { const files = Object.keys(res.data.files).map((filename) => ({ @@ -44,6 +57,7 @@ export const fetchFiles = (gistId: string) => { state.loading = false; }) .catch((err) => { + // console.error(err) state.loading = false; state.logs.push({ type: "error", diff --git a/state/constants/index.ts b/state/constants/index.ts new file mode 100644 index 0000000..671fb33 --- /dev/null +++ b/state/constants/index.ts @@ -0,0 +1 @@ +export * from './templates' \ No newline at end of file diff --git a/state/constants/templates.ts b/state/constants/templates.ts new file mode 100644 index 0000000..0bdc2ae --- /dev/null +++ b/state/constants/templates.ts @@ -0,0 +1,11 @@ +export const templateFileIds = { + 'starter': '1d14e51e2e02dc0a508cb0733767a914', // TODO currently same as accept + 'accept': '1d14e51e2e02dc0a508cb0733767a914', + 'firewall': 'bcd6d0c0fcbe52545ddb802481ff9d26', + 'notary': 'a789c75f591eeab7932fd702ed8cf9ea', + 'carbon': '43925143fa19735d8c6505c34d3a6a47', + 'peggy': 'ceaf352e2a65741341033ab7ef05c448', + 'headers': '9b448e8a55fab11ef5d1274cb59f9cf3' +} + +export const apiHeaderFiles = ['hookapi.h', 'sfcodes.h', 'hookmacro.h']