-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,73 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { Button, Card, CardContent, CardActions } from "@mui/material"; | ||
import CodeMirror from "@uiw/react-codemirror"; // CodeMirror editor | ||
import { json } from "@codemirror/lang-json"; // JSON language support for CodeMirror | ||
import { | ||
downloadVisData, | ||
fileDownloaded, | ||
updateFile, | ||
} from "../../../utils/visualization"; | ||
import { readTextFromFile } from "../../../utils/common"; | ||
import { downloadPublicVisData } from "../../../actions/public_visualization"; | ||
import { filePublicDownloaded } from "../../../actions/public_file"; | ||
|
||
type jsonProps = { | ||
fileId?: string; | ||
visualizationId?: string; | ||
publicView?: boolean; | ||
}; | ||
|
||
export default function JSON(props: jsonProps) { | ||
const { fileId, visualizationId, publicView } = props; | ||
const [jsonContent, setJsonContent] = useState<string | undefined>(); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
let blob; | ||
if (visualizationId) { | ||
blob = publicView | ||
? await downloadPublicVisData(visualizationId) | ||
: await downloadVisData(visualizationId); | ||
} else { | ||
blob = publicView | ||
? await filePublicDownloaded(fileId) | ||
: await fileDownloaded(fileId, 0); | ||
} | ||
|
||
const file = new File([blob], "data.json"); | ||
const text = await readTextFromFile(file); | ||
setJsonContent(text); | ||
} catch (error) { | ||
console.error("Error fetching data:", error); | ||
} | ||
}; | ||
fetchData(); | ||
}, [visualizationId, fileId, publicView]); | ||
|
||
const handleSave = async () => { | ||
try { | ||
await updateFile(fileId, jsonContent); | ||
} catch (error) { | ||
console.error("Error updating file:", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<Card> | ||
<CardContent> | ||
<CodeMirror | ||
value={jsonContent} | ||
extensions={[json()]} | ||
onChange={(value) => setJsonContent(value)} | ||
theme="dark" | ||
/> | ||
</CardContent> | ||
<CardActions> | ||
<Button variant="contained" color="primary" onClick={handleSave}> | ||
Save Changes | ||
</Button> | ||
</CardActions> | ||
</Card> | ||
); | ||
} |
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,19 @@ | ||
{ | ||
"name": "json-editor", | ||
"version": "1.0.0", | ||
"description": "A React component for visualizing and editing JSON data", | ||
"main": "JSON.tsx", | ||
"dependencies": { | ||
"clowder2-core": "1.0.0", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"@uiw/react-codemirror": "^4.23.5", | ||
"@codemirror/lang-json": "^6.0.1" | ||
}, | ||
"visConfig": { | ||
"name": "JSON", | ||
"mainType": "application", | ||
"mimeTypes": ["application/json"], | ||
"fields": [] | ||
} | ||
} |
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