|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import {ArrowLeftIcon, ClipboardIcon} from '@phosphor-icons/react'; |
| 4 | +import colors from '../../config/colors'; |
| 5 | +import usePageTitle from '../../utils/usePageTitle'; |
| 6 | +import { useToast } from '../../hooks/useToast'; |
| 7 | +import piml from 'piml'; // Import the piml library |
| 8 | + |
| 9 | +function JsonPimlConverterPage() { |
| 10 | + usePageTitle('JSON - PIML Converter'); |
| 11 | + const { addToast } = useToast(); |
| 12 | + |
| 13 | + const [jsonInput, setJsonInput] = useState(''); |
| 14 | + const [pimlInput, setPimlInput] = useState(''); |
| 15 | + const [output, setOutput] = useState(''); |
| 16 | + const [errorMsg, setErrorMsg] = useState(''); |
| 17 | + const [selectedFormat, setSelectedFormat] = useState(''); |
| 18 | + |
| 19 | + const handleJsonToPiml = () => { |
| 20 | + setSelectedFormat("PIML"); |
| 21 | + try { |
| 22 | + const json = JSON.parse(jsonInput); |
| 23 | + const pimlOutput = piml.stringify(json); // Use piml.stringify |
| 24 | + setOutput(pimlOutput); |
| 25 | + setErrorMsg(''); |
| 26 | + addToast({ title: 'Success', message: 'JSON converted to PIML!', duration: 10000, type: 'success' }); |
| 27 | + } catch (error) { |
| 28 | + addToast({ title: `Error ${selectedFormat}`, message: `Invalid JSON input: ${error.message}` , duration: 10000, type: 'error' }); |
| 29 | + console.log(error.message) |
| 30 | + setOutput(''); |
| 31 | + setErrorMsg(error.toString()); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const handlePimlToJson = () => { |
| 36 | + setSelectedFormat("JSON"); |
| 37 | + try { |
| 38 | + const jsonOutput = piml.parse(pimlInput); // Use piml.parse |
| 39 | + setOutput(JSON.stringify(jsonOutput, null, 2)); |
| 40 | + setErrorMsg(''); |
| 41 | + addToast({ title: 'Success', message: 'PIML converted to JSON!', duration: 10000 }); |
| 42 | + } catch (error) { |
| 43 | + addToast({ title: `Error ${selectedFormat}`, message: `Invalid PIML input: ${error.message}`, duration: 10000, type: 'error' }); |
| 44 | + console.log(error.message) |
| 45 | + setOutput(''); |
| 46 | + setErrorMsg(error.toString()); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + // Removed custom convertJsonToPiml, convertPimlToJson, and parsePimlValue functions |
| 51 | + |
| 52 | + const copyToClipboard = () => { |
| 53 | + if (output) { |
| 54 | + navigator.clipboard.writeText(output); |
| 55 | + addToast({ title: 'Copied!', message: `${selectedFormat} object is copied to clipboard.`, duration: 2000 }); |
| 56 | + } else { |
| 57 | + addToast({ title: 'Cannot Copy', message: `${selectedFormat} object is cannot be clipboard.`, duration: 2000, type: 'error' }); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const cardStyle = { |
| 62 | + backgroundColor: colors['app-alpha-10'], |
| 63 | + borderColor: colors['app-alpha-50'], |
| 64 | + color: colors.app, |
| 65 | + }; |
| 66 | + |
| 67 | + const buttonStyle = "px-6 py-2 flex items-center gap-2 text-lg font-arvo font-normal px-4 py-2 rounded-md border transition-colors duration-300 ease-in-out bg-tb text-app border-app-alpha-50 hover:bg-app/15"; |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="py-16 sm:py-24"> |
| 71 | + <div className="mx-auto max-w-7xl px-6 lg:px-8 text-gray-300"> |
| 72 | + <Link |
| 73 | + to="/apps" |
| 74 | + className="text-article hover:underline flex items-center justify-center gap-2 text-lg mb-4" |
| 75 | + > |
| 76 | + <ArrowLeftIcon size={24} /> Back to Apps |
| 77 | + </Link> |
| 78 | + <h1 className="text-4xl font-bold tracking-tight sm:text-6xl mb-4 flex items-center"> |
| 79 | + <span className="codex-color">fc</span> |
| 80 | + <span className="separator-color">::</span> |
| 81 | + <span className="apps-color">apps</span> |
| 82 | + <span className="separator-color">::</span> |
| 83 | + <span className="single-app-color">jpc</span> |
| 84 | + </h1> |
| 85 | + <hr className="border-gray-700" /> |
| 86 | + <div className="flex justify-center items-center mt-16"> |
| 87 | + <div |
| 88 | + className="group bg-transparent border rounded-lg shadow-2xl p-6 flex flex-col justify-between relative overflow-hidden h-full w-full max-w-6xl" |
| 89 | + style={cardStyle} |
| 90 | + > |
| 91 | + <div |
| 92 | + className="absolute top-0 left-0 w-full h-full opacity-10" |
| 93 | + style={{ |
| 94 | + backgroundImage: |
| 95 | + 'radial-gradient(circle, white 1px, transparent 1px)', |
| 96 | + backgroundSize: '10px 10px', |
| 97 | + }} |
| 98 | + ></div> |
| 99 | + <h1 className="text-3xl font-arvo font-normal mb-4 text-app">JSON - PIML Converter</h1> |
| 100 | + <hr className="border-gray-700 mb-4" /> |
| 101 | + <div className="relative z-10 p-1"> |
| 102 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 103 | + <div> |
| 104 | + <h2 className="text-xl font-arvo font-normal mb-2 text-app">JSON Input</h2> |
| 105 | + <textarea |
| 106 | + className="w-full h-64 p-2 rounded-md bg-gray-800 text-white border border-gray-700" |
| 107 | + value={jsonInput} |
| 108 | + onChange={(e) => setJsonInput(e.target.value)} |
| 109 | + placeholder="Enter JSON here..." |
| 110 | + ></textarea> |
| 111 | + <button onClick={handleJsonToPiml} className={`${buttonStyle} mt-6`}>Convert JSON to PIML</button> |
| 112 | + </div> |
| 113 | + <div> |
| 114 | + <h2 className="text-xl font-arvo font-normal mb-2 text-app">PIML Input</h2> |
| 115 | + <textarea |
| 116 | + className="w-full h-64 p-2 rounded-md bg-gray-800 text-white border border-gray-700" |
| 117 | + value={pimlInput} |
| 118 | + onChange={(e) => setPimlInput(e.target.value)} |
| 119 | + placeholder="Enter PIML here..." |
| 120 | + ></textarea> |
| 121 | + <button onClick={handlePimlToJson} className={`${buttonStyle} mt-6`}>Convert PIML to JSON</button> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <hr className="border-gray-700 mt-8 mb-8" /> |
| 125 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 126 | + <div> |
| 127 | + <h2 className="text-xl font-arvo font-normal mb-2 text-app">Output</h2> |
| 128 | + <textarea |
| 129 | + className="w-full h-64 p-2 rounded-md bg-gray-800 text-white border border-gray-700" |
| 130 | + value={output} |
| 131 | + readOnly |
| 132 | + placeholder="Conversion output..." |
| 133 | + ></textarea> |
| 134 | + <button onClick={copyToClipboard} className={`${buttonStyle} mt-6`}> |
| 135 | + <ClipboardIcon size={24}> </ClipboardIcon> |
| 136 | + Copy to Clipboard |
| 137 | + </button> |
| 138 | + </div> |
| 139 | + <div> |
| 140 | + <h2 className="text-xl font-arvo font-normal mb-2 text-app">Error Message</h2> |
| 141 | + <textarea |
| 142 | + className="w-full h-64 p-2 rounded-md bg-gray-800 text-white border border-gray-700" |
| 143 | + value={errorMsg} |
| 144 | + readOnly |
| 145 | + placeholder="No error messages..." |
| 146 | + ></textarea> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +export default JsonPimlConverterPage; |
0 commit comments