-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added missed source code inspector files
- Loading branch information
1 parent
749d9f6
commit 9fd1ed6
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import defaultPropTypes from "./defaultPropTypes"; | ||
import styles from "./SourceCodeInspector.module.css"; | ||
import { nodePath } from "./nodeData"; | ||
import ToggleablePanel from "./ToggleablePanel"; | ||
|
||
const SourceCodePanel = (props) => { | ||
const { node, state } = props; | ||
const { | ||
config: { | ||
codeInspector: { prefix }, | ||
}, | ||
} = state; | ||
|
||
const [code, setCode] = useState(); | ||
|
||
const path = nodePath(node); | ||
const url = `${prefix}${path}`; | ||
|
||
useEffect(() => { | ||
async function fetchData() { | ||
console.log(`fetching ${path} from ${url}`); | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
const text = `Error fetching URL ${url}\nHTTP response: ${response.status}:${response.statusText}`; | ||
setCode(text); | ||
} else { | ||
const text = await response.text(); | ||
setCode(text); | ||
} | ||
} catch (ex) { | ||
console.error("Exception caught:", ex); | ||
if (ex.message.startsWith("NetworkError")) { | ||
setCode( | ||
`Network error talking to server\ntrying URL ${url}\nServer not found, or invalid CORS handling.` | ||
); | ||
} else { | ||
throw ex; | ||
} | ||
} | ||
} | ||
fetchData(); | ||
}, [path, url]); | ||
|
||
return code ? ( | ||
<pre className={styles.code}> | ||
<code>{code}</code> | ||
</pre> | ||
) : ( | ||
<p>loading</p> | ||
); | ||
}; | ||
|
||
SourceCodePanel.propTypes = defaultPropTypes; | ||
|
||
const SourceCodeInspector = (props) => { | ||
const { node, state, dispatch } = props; | ||
const { enabled } = state.config.codeInspector; | ||
|
||
return enabled ? ( | ||
<ToggleablePanel title="Code" showInitially={false}> | ||
<SourceCodePanel node={node} state={state} dispatch={dispatch} /> | ||
</ToggleablePanel> | ||
) : ( | ||
"" | ||
); | ||
}; | ||
|
||
SourceCodeInspector.propTypes = defaultPropTypes; | ||
|
||
export default SourceCodeInspector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.code { | ||
overflow:auto; | ||
max-height: 500px; | ||
} |