@@ -24,9 +24,15 @@ type jsonProps = {
2424
2525export default function JSONVisualizer ( props : jsonProps ) {
2626 const { fileId, visualizationId, publicView } = props ;
27+
28+ // State to store the original content of the file and the displayed JSON content that can be edited
29+ const [ originalContent , setOriginalContent ] = useState < string | undefined > ( ) ;
2730 const [ jsonContent , setJsonContent ] = useState < string | undefined > ( ) ;
31+
32+ // Utility states to help with saving the file, displaying loading spinner and validating JSON
2833 const [ fileName , setFileName ] = useState < string | undefined > ( ) ;
2934 const [ loading , setLoading ] = useState < boolean > ( false ) ;
35+ const [ validJson , setValidJson ] = useState < boolean > ( true ) ;
3036
3137 // use useSelector to get fileSummary to get filename.
3238 const fileData = useSelector ( ( state : RootState ) => state . file ) ;
@@ -42,10 +48,6 @@ export default function JSONVisualizer(props: jsonProps) {
4248 }
4349 } , [ fileData . fileSummary ] ) ;
4450
45- useEffect ( ( ) => {
46- console . log ( "fileData" , fileData . fileSummary . content_type ) ;
47- } , [ fileName ] ) ;
48-
4951 useEffect ( ( ) => {
5052 const fetchData = async ( ) => {
5153 try {
@@ -62,6 +64,7 @@ export default function JSONVisualizer(props: jsonProps) {
6264
6365 const file = new File ( [ blob ] , fileName ) ;
6466 const text = await readTextFromFile ( file ) ;
67+ setOriginalContent ( text ) ;
6568 setJsonContent ( text ) ;
6669 } catch ( error ) {
6770 console . error ( "Error fetching data:" , error ) ;
@@ -70,6 +73,20 @@ export default function JSONVisualizer(props: jsonProps) {
7073 fetchData ( ) ;
7174 } , [ visualizationId , fileId , publicView ] ) ;
7275
76+ const validateJson = ( jsonString : string ) => {
77+ try {
78+ JSON . parse ( jsonString ) ;
79+ return true ;
80+ } catch ( error ) {
81+ return false ;
82+ }
83+ }
84+
85+ const handleChange = ( value : string ) => {
86+ setJsonContent ( value ) ;
87+ setValidJson ( validateJson ( value ) ) ;
88+ }
89+
7390 const handleSave = async ( ) => {
7491 try {
7592 if (
@@ -91,7 +108,7 @@ export default function JSONVisualizer(props: jsonProps) {
91108 console . error ( "Error updating file:" , error ) ;
92109 }
93110 } ;
94-
111+
95112 return (
96113 < Card >
97114 < CardContent >
@@ -101,7 +118,7 @@ export default function JSONVisualizer(props: jsonProps) {
101118 < CodeMirror
102119 value = { jsonContent }
103120 extensions = { [ json ( ) ] }
104- onChange = { ( value ) => setJsonContent ( value ) }
121+ onChange = { ( value ) => handleChange ( value ) }
105122 theme = "dark"
106123 />
107124 ) }
0 commit comments