1- import { useEffect , useState } from "react" ;
1+ import { useEffect , useState , useRef } from "react" ;
22import CodeMirror from "@uiw/react-codemirror" ;
33import { javascript } from "@codemirror/lang-javascript" ;
44import { createTheme } from "@uiw/codemirror-themes" ;
@@ -7,9 +7,14 @@ import Head from "next/head";
77import Toastr from "../components/Toastr" ;
88import { ToastContainer } from "react-toastify" ;
99import Button from "../components/Button" ;
10+ import Select from "react-select" ;
1011
1112import { CODE_EDITOR_THEME } from "../constants/theme" ;
12- import { DEMO_JSON_STRING } from "../components/constants" ;
13+ import {
14+ DEFAULT_INDENTATION_SPACE ,
15+ DEMO_JSON_STRING ,
16+ INDENTATION_SPACE_OPTIONS ,
17+ } from "../components/constants" ;
1318import { Copy } from "../icons/Copy" ;
1419import Header from "../components/Header" ;
1520
@@ -19,11 +24,16 @@ export default function Home() {
1924 ) ;
2025 const [ outputString , setOutputString ] = useState ( "" ) ;
2126
27+ const hiddenFileInput = useRef ( null ) ;
28+ const selectRef = useRef ( null ) ;
29+
2230 let editorExtensions = [ javascript ( { jsx : true } ) , EditorView . lineWrapping ] ;
2331
24- const handleFormat = ( ) => {
32+ const handleFormat = ( indentationSpace = 2 ) => {
2533 try {
26- setOutputString ( JSON . stringify ( JSON . parse ( inputString ) , null , 2 ) ) ;
34+ setOutputString (
35+ JSON . stringify ( JSON . parse ( inputString ) , null , indentationSpace )
36+ ) ;
2737 } catch ( error ) {
2838 Toastr . error ( "Enter Valid JSON" ) ;
2939 }
@@ -59,6 +69,13 @@ export default function Home() {
5969 setOutputString ( "" ) ;
6070 } ;
6171
72+ const handleUpload = ( e ) => {
73+ const fileReader = new FileReader ( ) ;
74+ fileReader . readAsText ( e . target . files [ 0 ] , "UTF-8" ) ;
75+ fileReader . onload = ( e ) => setInputString ( e . target . result ) ;
76+ setOutputString ( "" ) ;
77+ } ;
78+
6279 useEffect ( ( ) => {
6380 inputString === "" && setOutputString ( "" ) ;
6481 } , [ inputString ] ) ;
@@ -73,7 +90,7 @@ export default function Home() {
7390
7491 < main className = "h-screen flex flex-col justify-between bg-zinc-900 p-5" >
7592 < ToastContainer theme = "dark" />
76- < Header />
93+ < Header />
7794 < div className = "space-y-3 grow flex flex-col" >
7895 < div className = "flex grow space-x-3" >
7996 < div className = "w-full bg-[#282c34]" >
@@ -113,13 +130,35 @@ export default function Home() {
113130 </ div >
114131 </ div >
115132 < div className = "flex justify-end bg-zinc-800 space-x-3 px-3 py-3 rounded" >
116- < Button label = "Format" onClick = { handleFormat } />
133+ < Button
134+ label = "Format"
135+ onClick = { ( ) => handleFormat ( selectRef . current . state . value . value ) }
136+ />
117137 < Button label = "Clear" onClick = { handleClear } />
118138 < Button
119139 label = "Download"
120140 onClick = { handleDownload }
121141 disabled = { outputString === "" }
122142 />
143+ < Button
144+ onClick = { ( ) => hiddenFileInput . current . click ( ) }
145+ label = "Upload"
146+ />
147+ < input
148+ type = "file"
149+ style = { { display : "none" } }
150+ ref = { hiddenFileInput }
151+ onChange = { handleUpload }
152+ />
153+ < Select
154+ ref = { selectRef }
155+ classNamePrefix = "react-select"
156+ onChange = { ( e ) => handleFormat ( e . value ) }
157+ className = { "react-select__container" }
158+ menuPlacement = "top"
159+ options = { INDENTATION_SPACE_OPTIONS }
160+ defaultValue = { DEFAULT_INDENTATION_SPACE }
161+ />
123162 </ div >
124163 </ div >
125164 { /* <Footer /> */ }
0 commit comments