diff --git a/src/components/tool/index.tsx b/src/components/tool/index.tsx index 64aa641..44e56ac 100644 --- a/src/components/tool/index.tsx +++ b/src/components/tool/index.tsx @@ -1,7 +1,8 @@ import type { ReactNode } from 'react'; -import { Hash } from 'lucide-react' +import { FileJson, Hash } from 'lucide-react' import UUID from './uuid' +import JSON from './json' export interface Tool { path: string; @@ -18,5 +19,12 @@ export const tools: Tool[] = [ description: "Generate a UUID", icon: , component: , + }, + { + path: "json", + name: "JSON Formatter", + description: "Format and validate JSON", + icon: , + component: , } ]; \ No newline at end of file diff --git a/src/components/tool/json.tsx b/src/components/tool/json.tsx new file mode 100644 index 0000000..206c6c5 --- /dev/null +++ b/src/components/tool/json.tsx @@ -0,0 +1,59 @@ +import { useState, type FC } from "react"; +import { Textarea } from "@/components/ui/textarea"; +import { Button } from "@/components/ui/button"; +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; + +const Tool: FC = () => { + const [json, setJson] = useState(""); + const [error, setError] = useState(""); + + const validateJson = () => { + try { + JSON.parse(json); + setError(""); + return true; + } catch (error: unknown) { + if (error instanceof Error) { + setError(error.message); + } else { + setError("Invalid JSON"); + } + return false; + } + }; + + const minifyJson = () => { + if (!validateJson()) return; + const formattedJson = JSON.stringify(JSON.parse(json), null, 0); + setJson(formattedJson); + }; + + const prettifyJson = () => { + if (!validateJson()) return; + const formattedJson = JSON.stringify(JSON.parse(json), null, 2); + setJson(formattedJson); + }; + + return ( +
+