Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add blueprint code to the blueprint detail screen #293

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"d3-selection": "3.0.0",
"d3-zoom": "3.0.0",
"font-awesome": "4.7.0",
"highlight.js": "11.10.0",
"jquery": "3.7.1",
"lodash": "4.17.21",
"npm-run-all": "4.1.5",
Expand Down
19 changes: 19 additions & 0 deletions src/api/nanoApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ const nanoApi = {
}
);
},

/**
* Get the blueprint source code
*
* @param {string} blueprintId ID of the blueprint
*
* For more details, see full node api docs
*/
getBlueprintSourceCode(blueprintId) {
const data = { blueprint_id: blueprintId };
return requestExplorerServiceV1.get(`node_api/nc_blueprint_source_code`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
},
};

export default nanoApi;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import App from './App';
import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'font-awesome/css/font-awesome.min.css';
import 'highlight.js/styles/github.css';
import './index.css';

import store from './store/index';
Expand Down
21 changes: 21 additions & 0 deletions src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,25 @@ th.sortable {

.table-method-arguments tbody tr {
background-color: transparent !important;
}

.source-code code {
background-color: #f2f2f2;
}

.source-code pre {
height: 100%;
}

.source-code {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
transition: all 1s ease;
height: 0;
opacity: 0;
}

.source-code.show {
height: auto !important;
opacity: 1 !important;
}
52 changes: 46 additions & 6 deletions src/screens/nano/BlueprintDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import hljs from 'highlight.js/lib/core';
import python from 'highlight.js/lib/languages/python';
import Loading from '../../components/Loading';
import nanoApi from '../../api/nanoApi';

hljs.registerLanguage('python', python);

/**
* Details of a Nano Contract
* Details of a Blueprint
*
* @memberof Screens
*/
Expand All @@ -19,10 +23,16 @@ function BlueprintDetail(props) {

// blueprintInformation {Object | null} Blueprint information
const [blueprintInformation, setBlueprintInformation] = useState(null);
// blueprintSourceCode {string | null} Blueprint source code
const [blueprintSourceCode, setBlueprintSourceCode] = useState(null);
// loading {boolean} Bool to show/hide loading when getting blueprint information
const [loading, setLoading] = useState(true);
// errorMessage {string | null} Error message in case a request to get nano contract data fails
const [errorMessage, setErrorMessage] = useState(null);
// showCode {boolean} If should show the blueprint source code
const [showCode, setShowCode] = useState(false);

const codeRef = useRef();

useEffect(() => {
let ignore = false;
Expand All @@ -31,19 +41,21 @@ function BlueprintDetail(props) {
setLoading(true);
setBlueprintInformation(null);
try {
const blueprintInformation = await nanoApi.getBlueprintInformation(blueprintId);
const blueprintInformationData = await nanoApi.getBlueprintInformation(blueprintId);
const blueprintSourceCodeData = await nanoApi.getBlueprintSourceCode(blueprintId);
if (ignore) {
// This is to prevent setting a state after the component has been already cleaned
return;
}
setBlueprintInformation(blueprintInformation);
setLoading(false);
setBlueprintInformation(blueprintInformationData);
setBlueprintSourceCode(blueprintSourceCodeData.source_code);
} catch (e) {
if (ignore) {
// This is to prevent setting a state after the component has been already cleaned
return;
}
setErrorMessage('Error getting blueprint information.');
} finally {
setLoading(false);
}
}
Expand All @@ -54,6 +66,12 @@ function BlueprintDetail(props) {
};
}, [blueprintId]);

useEffect(() => {
if (codeRef && codeRef.current) {
hljs.highlightBlock(codeRef.current);
}
}, [blueprintSourceCode]);

if (errorMessage) {
return <p className="text-danger mb-4">{errorMessage}</p>;
}
Expand Down Expand Up @@ -119,6 +137,16 @@ function BlueprintDetail(props) {
return `${name}(${parameters.join(', ')}): ${returnType === 'null' ? 'None' : returnType}`;
};

/**
* Handle toggle click to hide or show the blueprint source code
*
* @param {Event} e Click event
*/
const onToggleShowCode = e => {
e.preventDefault();
setShowCode(!showCode);
};

return (
<div className="content-wrapper">
<h3 className="mt-4">Blueprint Information</h3>
Expand All @@ -135,7 +163,19 @@ function BlueprintDetail(props) {
{renderBlueprintAttributes()}
{renderBlueprintMethods('public_methods', 'Public Methods')}
{renderBlueprintMethods('private_methods', 'Private Methods')}
<hr />
<div className="d-flex flex-row align-items-center mb-4 mt-4">
<h4 className="mb-0 mr-3">Source Code</h4>
<a href="true" onClick={e => onToggleShowCode(e)}>
{showCode ? 'Hide' : 'Show'}
</a>
</div>
<div className={`source-code ${showCode ? 'show' : ''}`}>
<pre>
<code ref={codeRef} className="language-python">
{blueprintSourceCode}
</code>
</pre>
</div>
</div>
</div>
);
Expand Down