From f739d4da342854be2c318898273a982c3cdaaf3d Mon Sep 17 00:00:00 2001 From: muzam Date: Thu, 16 Dec 2021 18:26:58 +0530 Subject: [PATCH 1/4] Fetch templates and header files according to selection. --- components/Navigation.tsx | 104 ++++++++++-------------------------- state/actions/fetchFiles.ts | 11 ++++ 2 files changed, 39 insertions(+), 76 deletions(-) diff --git a/components/Navigation.tsx b/components/Navigation.tsx index a58b4fc..315ad67 100644 --- a/components/Navigation.tsx +++ b/components/Navigation.tsx @@ -82,12 +82,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 +95,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..c836700 100644 --- a/state/actions/fetchFiles.ts +++ b/state/actions/fetchFiles.ts @@ -4,6 +4,8 @@ import state from '../index'; const octokit = new Octokit(); +const HEADER_GIST_ID = '9b448e8a55fab11ef5d1274cb59f9cf3' + /* Fetches Gist files from Githug Gists based on * gistId and stores the content in global state */ @@ -17,6 +19,14 @@ export const fetchFiles = (gistId: string) => { octokit .request("GET /gists/{gist_id}", { gist_id: gistId }) + .then(res => { + // fetch header file(s) and append to res + return octokit.request("GET /gists/{gist_id}", { gist_id: HEADER_GIST_ID }).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 +54,7 @@ export const fetchFiles = (gistId: string) => { state.loading = false; }) .catch((err) => { + // console.error(err) state.loading = false; state.logs.push({ type: "error", From ad947be0bc135bf6dbe0b4407a6847c824aace5d Mon Sep 17 00:00:00 2001 From: muzam Date: Thu, 16 Dec 2021 18:52:16 +0530 Subject: [PATCH 2/4] Only fetch extra headers on template files. --- components/Navigation.tsx | 13 +++++++------ state/actions/fetchFiles.ts | 11 +++++++---- utils/templates.ts | 9 +++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 utils/templates.ts diff --git a/components/Navigation.tsx b/components/Navigation.tsx index 315ad67..6bb1943 100644 --- a/components/Navigation.tsx +++ b/components/Navigation.tsx @@ -27,6 +27,7 @@ import { DialogTrigger, } from "./Dialog"; import PanelBox from "./PanelBox"; +import { templates } from '../utils/templates'; const Navigation = () => { const router = useRouter(); @@ -243,27 +244,27 @@ const Navigation = () => { }, }} > - + Starter Just an empty starter with essential imports - + Firewall This Hook essentially checks a blacklist of accounts - + Accept This hook just accepts any transaction coming through it - + Notary Collecting signatures for multi-sign transactions - + Carbon Send a percentage of sum to an address - + Peggy An oracle based stabe coin hook diff --git a/state/actions/fetchFiles.ts b/state/actions/fetchFiles.ts index c836700..02f7369 100644 --- a/state/actions/fetchFiles.ts +++ b/state/actions/fetchFiles.ts @@ -1,11 +1,11 @@ import { Octokit } from "@octokit/core"; import Router from "next/router"; import state from '../index'; +import { templates } from '../../utils/templates'; + const octokit = new Octokit(); -const HEADER_GIST_ID = '9b448e8a55fab11ef5d1274cb59f9cf3' - /* Fetches Gist files from Githug Gists based on * gistId and stores the content in global state */ @@ -20,8 +20,11 @@ export const fetchFiles = (gistId: string) => { octokit .request("GET /gists/{gist_id}", { gist_id: gistId }) .then(res => { - // fetch header file(s) and append to res - return octokit.request("GET /gists/{gist_id}", { gist_id: HEADER_GIST_ID }).then(({ data: { files: headerFiles } }) => { + if (!Object.values(templates).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: templates.headers }).then(({ data: { files: headerFiles } }) => { const files = { ...res.data.files, ...headerFiles } res.data.files = files return res diff --git a/utils/templates.ts b/utils/templates.ts new file mode 100644 index 0000000..4af5bc1 --- /dev/null +++ b/utils/templates.ts @@ -0,0 +1,9 @@ +export const templates = { + 'starter': '1d14e51e2e02dc0a508cb0733767a914', // TODO currently same as accept + 'accept': '1d14e51e2e02dc0a508cb0733767a914', + 'firewall': 'bcd6d0c0fcbe52545ddb802481ff9d26', + 'notary': 'a789c75f591eeab7932fd702ed8cf9ea', + 'carbon': '43925143fa19735d8c6505c34d3a6a47', + 'peggy': 'ceaf352e2a65741341033ab7ef05c448', + 'headers': '9b448e8a55fab11ef5d1274cb59f9cf3' +} \ No newline at end of file From b4ca360661c566533715f1afe4c185633b9cf582 Mon Sep 17 00:00:00 2001 From: muzam Date: Thu, 16 Dec 2021 22:30:03 +0530 Subject: [PATCH 3/4] Implement read-only editors for some headers. --- components/HooksEditor.tsx | 36 ++++++++++++++++--------- components/Navigation.tsx | 14 +++++----- state/actions/fetchFiles.ts | 6 ++--- state/constants/index.ts | 1 + {utils => state/constants}/templates.ts | 6 +++-- 5 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 state/constants/index.ts rename {utils => state/constants}/templates.ts (78%) diff --git a/components/HooksEditor.tsx b/components/HooksEditor.tsx index 5daa7b3..3d15f1a 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,27 @@ loader.config({ }, }); +const validateWritability = (editor: monaco.editor.IStandaloneCodeEditor) => { + const currPath = editor.getModel()?.uri.path; + console.log(currPath) + 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 +74,9 @@ const HooksEditor = () => { language={snap.files?.[snap.active]?.language} path={`file://tmp/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, @@ -86,7 +102,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(); @@ -125,12 +141,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"} /> @@ -148,9 +162,7 @@ const HooksEditor = () => { - + { const router = useRouter(); @@ -244,27 +244,27 @@ const Navigation = () => { }, }} > - + Starter Just an empty starter with essential imports - + Firewall This Hook essentially checks a blacklist of accounts - + Accept This hook just accepts any transaction coming through it - + Notary Collecting signatures for multi-sign transactions - + Carbon Send a percentage of sum to an address - + Peggy An oracle based stabe coin hook diff --git a/state/actions/fetchFiles.ts b/state/actions/fetchFiles.ts index 02f7369..e219b96 100644 --- a/state/actions/fetchFiles.ts +++ b/state/actions/fetchFiles.ts @@ -1,7 +1,7 @@ import { Octokit } from "@octokit/core"; import Router from "next/router"; import state from '../index'; -import { templates } from '../../utils/templates'; +import { templateFileIds } from '../constants'; const octokit = new Octokit(); @@ -20,11 +20,11 @@ export const fetchFiles = (gistId: string) => { octokit .request("GET /gists/{gist_id}", { gist_id: gistId }) .then(res => { - if (!Object.values(templates).includes(gistId)) { + 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: templates.headers }).then(({ data: { files: headerFiles } }) => { + 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 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/utils/templates.ts b/state/constants/templates.ts similarity index 78% rename from utils/templates.ts rename to state/constants/templates.ts index 4af5bc1..0bdc2ae 100644 --- a/utils/templates.ts +++ b/state/constants/templates.ts @@ -1,4 +1,4 @@ -export const templates = { +export const templateFileIds = { 'starter': '1d14e51e2e02dc0a508cb0733767a914', // TODO currently same as accept 'accept': '1d14e51e2e02dc0a508cb0733767a914', 'firewall': 'bcd6d0c0fcbe52545ddb802481ff9d26', @@ -6,4 +6,6 @@ export const templates = { 'carbon': '43925143fa19735d8c6505c34d3a6a47', 'peggy': 'ceaf352e2a65741341033ab7ef05c448', 'headers': '9b448e8a55fab11ef5d1274cb59f9cf3' -} \ No newline at end of file +} + +export const apiHeaderFiles = ['hookapi.h', 'sfcodes.h', 'hookmacro.h'] From 027b2c8ed4c8597e6ac94a0e0983bd44a4fb413c Mon Sep 17 00:00:00 2001 From: muzam Date: Tue, 21 Dec 2021 17:07:45 +0530 Subject: [PATCH 4/4] remove console log --- components/HooksEditor.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/components/HooksEditor.tsx b/components/HooksEditor.tsx index 3d15f1a..522cc49 100644 --- a/components/HooksEditor.tsx +++ b/components/HooksEditor.tsx @@ -29,7 +29,6 @@ loader.config({ const validateWritability = (editor: monaco.editor.IStandaloneCodeEditor) => { const currPath = editor.getModel()?.uri.path; - console.log(currPath) if (apiHeaderFiles.find(h => currPath?.endsWith(h))) { editor.updateOptions({ readOnly: true }); } else {