generated from shuding/nextra-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 29
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
add interactive terminal component w/ json view #159
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,59 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import styles from '../../styles/InteractiveTerminal.module.css'; | ||
|
||
const InteractiveTerminal: React.FC = () => { | ||
const [jsonData, setJsonData] = useState<any>(null); | ||
const [expandedNodes, setExpandedNodes] = useState<Set<string>>(new Set()); | ||
|
||
useEffect(() => { | ||
fetch('https://raw.githubusercontent.com/cosmos/chain-registry/refs/heads/master/sei/chain.json') | ||
.then((res) => res.json()) | ||
.then((data) => setJsonData(data)) | ||
.catch((err) => console.error('Failed to fetch JSON data:', err)); | ||
}, []); | ||
|
||
const toggleNode = (path: string) => { | ||
setExpandedNodes((prevNodes) => { | ||
const newNodes = new Set(prevNodes); | ||
if (newNodes.has(path)) { | ||
newNodes.delete(path); | ||
} else { | ||
newNodes.add(path); | ||
} | ||
return newNodes; | ||
}); | ||
}; | ||
|
||
const renderNode = (key: string, value: any, path: string) => { | ||
const isExpandable = typeof value === 'object' && value !== null; | ||
const isOpen = expandedNodes.has(path); | ||
const displayKey = key || 'root'; | ||
|
||
return ( | ||
<div key={path} className={styles.node}> | ||
<div className={styles.nodeHeader} onClick={() => isExpandable && toggleNode(path)}> | ||
{isExpandable && (isOpen ? '▼' : '▶')} {displayKey} | ||
</div> | ||
{isExpandable && isOpen && ( | ||
<div className={styles.nodeChildren}> | ||
{Object.entries(value).map(([childKey, childValue]) => renderNode(childKey, childValue, `${path}.${childKey}`))} | ||
</div> | ||
)} | ||
{!isExpandable && <div className={styles.leafNode}>{String(value)}</div>} | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div className={styles.terminal}> | ||
<div className={styles.header}> | ||
<div className={styles.circle} style={{ backgroundColor: '#FF605C' }} /> | ||
<div className={styles.circle} style={{ backgroundColor: '#FFBD44' }} /> | ||
<div className={styles.circle} style={{ backgroundColor: '#00CA4E' }} /> | ||
</div> | ||
<div className={styles.body}>{jsonData ? renderNode('', jsonData, 'root') : <div className={styles.loading}>Loading...</div>}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default InteractiveTerminal; |
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 @@ | ||
export { default as InteractiveTerminal } from './InteractiveTerminal'; |
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
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,53 @@ | ||
.terminal { | ||
background-color: #1e1e1e; | ||
color: #00ff00; | ||
border-radius: 8px; | ||
padding: 16px; | ||
font-family: 'Courier New', monospace; | ||
width: 600px; | ||
margin: 0 auto; | ||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
.header { | ||
display: flex; | ||
justify-content: flex-start; | ||
padding-bottom: 8px; | ||
} | ||
|
||
.circle { | ||
width: 12px; | ||
height: 12px; | ||
border-radius: 50%; | ||
margin-right: 8px; | ||
} | ||
|
||
.body { | ||
background-color: #1b1b1b; | ||
border-radius: 4px; | ||
padding: 8px; | ||
overflow-y: auto; | ||
max-height: 400px; | ||
} | ||
|
||
.node { | ||
margin-left: 16px; | ||
} | ||
|
||
.nodeHeader { | ||
cursor: pointer; | ||
user-select: none; | ||
} | ||
|
||
.nodeChildren { | ||
margin-left: 16px; | ||
} | ||
|
||
.leafNode { | ||
margin-left: 32px; | ||
} | ||
|
||
.loading { | ||
text-align: center; | ||
color: #d1d5db; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll probably need to add theming here when we actually use the component
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I will see about making the top-left buttons do stuff too