|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Card, |
| 5 | + CardContent, |
| 6 | + CardActions, |
| 7 | + CircularProgress, |
| 8 | +} from "@mui/material"; |
| 9 | +import CodeMirror from "@uiw/react-codemirror"; // CodeMirror editor |
| 10 | +import { json } from "@codemirror/lang-json"; // JSON language support for CodeMirror |
| 11 | +import { downloadVisData, fileDownloaded } from "../../../utils/visualization"; |
| 12 | +import { updateFile as updateFileAction } from "../../../actions/file"; |
| 13 | +import { readTextFromFile } from "../../../utils/common"; |
| 14 | +import { downloadPublicVisData } from "../../../actions/public_visualization"; |
| 15 | +import { filePublicDownloaded } from "../../../actions/public_file"; |
| 16 | +import { useDispatch, useSelector } from "react-redux"; |
| 17 | +import { RootState } from "../../../types/data"; |
| 18 | + |
| 19 | +type jsonProps = { |
| 20 | + fileId?: string; |
| 21 | + visualizationId?: string; |
| 22 | + publicView?: boolean; |
| 23 | +}; |
| 24 | + |
| 25 | +export default function JSONVisualizer(props: jsonProps) { |
| 26 | + const { fileId, visualizationId, publicView } = props; |
| 27 | + const [jsonContent, setJsonContent] = useState<string | undefined>(); |
| 28 | + const [fileName, setFileName] = useState<string | undefined>(); |
| 29 | + const [loading, setLoading] = useState<boolean>(false); |
| 30 | + |
| 31 | + // use useSelector to get fileSummary to get filename. |
| 32 | + const fileData = useSelector((state: RootState) => state.file); |
| 33 | + |
| 34 | + // use useDispatch to update file |
| 35 | + const dispatch = useDispatch(); |
| 36 | + const updateFile = async (file: File, fileId: string | undefined) => |
| 37 | + dispatch(updateFileAction(file, fileId)); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (fileData.fileSummary) { |
| 41 | + setFileName(fileData.fileSummary.name); |
| 42 | + } |
| 43 | + }, [fileData.fileSummary]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + console.log("fileData", fileData.fileSummary.content_type); |
| 47 | + }, [fileName]); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const fetchData = async () => { |
| 51 | + try { |
| 52 | + let blob; |
| 53 | + if (visualizationId) { |
| 54 | + blob = publicView |
| 55 | + ? await downloadPublicVisData(visualizationId) |
| 56 | + : await downloadVisData(visualizationId); |
| 57 | + } else { |
| 58 | + blob = publicView |
| 59 | + ? await filePublicDownloaded(fileId) |
| 60 | + : await fileDownloaded(fileId, 0); |
| 61 | + } |
| 62 | + |
| 63 | + const file = new File([blob], fileName); |
| 64 | + const text = await readTextFromFile(file); |
| 65 | + setJsonContent(text); |
| 66 | + } catch (error) { |
| 67 | + console.error("Error fetching data:", error); |
| 68 | + } |
| 69 | + }; |
| 70 | + fetchData(); |
| 71 | + }, [visualizationId, fileId, publicView]); |
| 72 | + |
| 73 | + const handleSave = async () => { |
| 74 | + try { |
| 75 | + if ( |
| 76 | + jsonContent !== undefined && |
| 77 | + fileName && |
| 78 | + fileData.fileSummary?.content_type |
| 79 | + ) { |
| 80 | + // Parse the jsonContent to ensure it's valid JSON |
| 81 | + const textBlob = new Blob([jsonContent], { type: "text/plain" }); |
| 82 | + const file = new File([textBlob], fileName, { |
| 83 | + type: fileData.fileSummary.content_type.content_type, |
| 84 | + }); |
| 85 | + |
| 86 | + setLoading(true); |
| 87 | + await updateFile(file, fileId); |
| 88 | + setLoading(false); |
| 89 | + } |
| 90 | + } catch (error) { |
| 91 | + console.error("Error updating file:", error); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + return ( |
| 96 | + <Card> |
| 97 | + <CardContent> |
| 98 | + {loading ? ( |
| 99 | + <CircularProgress /> |
| 100 | + ) : ( |
| 101 | + <CodeMirror |
| 102 | + value={jsonContent} |
| 103 | + extensions={[json()]} |
| 104 | + onChange={(value) => setJsonContent(value)} |
| 105 | + theme="dark" |
| 106 | + /> |
| 107 | + )} |
| 108 | + </CardContent> |
| 109 | + <CardActions> |
| 110 | + <Button variant="contained" color="primary" onClick={handleSave}> |
| 111 | + Save Changes |
| 112 | + </Button> |
| 113 | + </CardActions> |
| 114 | + </Card> |
| 115 | + ); |
| 116 | +} |
0 commit comments