From d5750f76d29c5d2d435b596121c4854dd0cc63eb Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Tue, 28 Apr 2026 03:20:38 +0200 Subject: [PATCH] chore: updated index file - Use of existing components - full i8n support --- src/pages/tools/json/sort-json/index.tsx | 193 ----------------------- src/pages/tools/json/sort/index.tsx | 163 +++++++++++++++++++ 2 files changed, 163 insertions(+), 193 deletions(-) delete mode 100644 src/pages/tools/json/sort-json/index.tsx create mode 100644 src/pages/tools/json/sort/index.tsx diff --git a/src/pages/tools/json/sort-json/index.tsx b/src/pages/tools/json/sort-json/index.tsx deleted file mode 100644 index 394fdf2b..00000000 --- a/src/pages/tools/json/sort-json/index.tsx +++ /dev/null @@ -1,193 +0,0 @@ -import React, { useState, useEffect } from 'react'; -import { Box, MenuItem, TextField } from '@mui/material'; -import ToolCodeInput from '@components/input/ToolCodeInput'; -import ToolTextResult from '@components/result/ToolTextResult'; -import { sortJson } from './service'; -import ToolExamples, { - CardExampleType -} from '@components/examples/ToolExamples'; -import { ToolComponentProps } from '@tools/defineTool'; -import ToolContent from '@components/ToolContent'; - -type SortMode = 'value' | 'key'; - -type InitialValuesType = { - sortMode: SortMode; - sortKey: string; - order: 'asc' | 'desc'; -}; - -const initialValues: InitialValuesType = { - sortMode: 'value', - sortKey: '', - order: 'asc' -}; - -const exampleCards: CardExampleType[] = [ - { - title: 'Sort by name ascending', - description: - 'Sort a JSON array of objects alphabetically by the "name" key.', - sampleText: `[{"name":"Charlie","age":30},{"name":"Alice","age":25},{"name":"Bob","age":35}]`, - sampleResult: `[ - { - "name": "Alice", - "age": 25 - }, - { - "name": "Bob", - "age": 35 - }, - { - "name": "Charlie", - "age": 30 - } -]`, - sampleOptions: { sortMode: 'value', sortKey: 'name', order: 'asc' } - }, - { - title: 'Sort object keys alphabetically', - description: - 'Sort the keys of a JSON object alphabetically in ascending order.', - sampleText: `{"zebra":1,"apple":2,"mango":3}`, - sampleResult: `{ - "apple": 2, - "mango": 3, - "zebra": 1 -}`, - sampleOptions: { sortMode: 'key', sortKey: '', order: 'asc' } - } -]; - -export default function SortJson({ title }: ToolComponentProps) { - const [input, setInput] = useState(''); - const [result, setResult] = useState(''); - const [availableKeys, setAvailableKeys] = useState([]); - const [inputError, setInputError] = useState(''); - - useEffect(() => { - if (!input.trim()) { - setAvailableKeys([]); - setInputError(''); - return; - } - try { - const parsed = JSON.parse(input); - const keys = new Set(); - if (Array.isArray(parsed)) { - parsed.forEach((item) => { - if (item && typeof item === 'object') { - Object.keys(item).forEach((k) => keys.add(k)); - } - }); - } else if (parsed && typeof parsed === 'object') { - Object.keys(parsed).forEach((k) => keys.add(k)); - } - setAvailableKeys(Array.from(keys).sort()); - setInputError(''); - } catch { - setAvailableKeys([]); - setInputError('Invalid JSON'); - } - }, [input]); - - const compute = (optionsValues: InitialValuesType, input: string) => { - const { sortMode, sortKey, order } = optionsValues; - if (!input.trim()) return; - - if (sortMode === 'value') { - if (availableKeys.length === 0) return; - if (!sortKey) { - setResult(''); - return; - } - setResult(sortJson(input, sortKey, order, 'value')); - } else { - setResult(sortJson(input, sortKey, order, 'key')); - } - }; - - return ( - - } - resultComponent={} - initialValues={initialValues} - getGroups={({ values, setFieldValue }) => [ - { - title: 'Sort options', - component: ( - - { - setFieldValue('sortMode', e.target.value); - setFieldValue('sortKey', ''); - }} - > - Sort array by key value - - Sort object keys alphabetically - - - {values.sortMode === 'value' && ( - setFieldValue('sortKey', e.target.value)} - error={!!inputError} - helperText={inputError} - > - {availableKeys.length === 0 ? ( - - {input.trim() - ? 'No keys found' - : 'Enter JSON to see keys'} - - ) : ( - availableKeys.map((key) => ( - - {key} - - )) - )} - - )} - setFieldValue('order', e.target.value)} - > - Ascending (A → Z) - Descending (Z → A) - - - ) - } - ]} - compute={compute} - setInput={setInput} - input={input} - exampleCards={exampleCards} - toolInfo={{ - title: 'Sort JSON', - description: - 'Sort a JSON array of objects by any key, or sort the keys of a JSON object alphabetically.' - }} - /> - ); -} diff --git a/src/pages/tools/json/sort/index.tsx b/src/pages/tools/json/sort/index.tsx new file mode 100644 index 00000000..85677ff0 --- /dev/null +++ b/src/pages/tools/json/sort/index.tsx @@ -0,0 +1,163 @@ +import { Box } from '@mui/material'; +import React, { useState } from 'react'; +import ToolCodeInput from '@components/input/ToolCodeInput'; +import ToolTextResult from '@components/result/ToolTextResult'; +import { sortJson } from './service'; +import { CardExampleType } from '@components/examples/ToolExamples'; +import SelectWithDesc from '@components/options/SelectWithDesc'; +import { ToolComponentProps } from '@tools/defineTool'; +import ToolContent from '@components/ToolContent'; +import { InitialValuesType } from './types'; +import { GetGroupsType } from '@components/options/ToolOptions'; +import { getJsonHeaders } from '@utils/json'; +import { useTranslation } from 'react-i18next'; + +const initialValues: InitialValuesType = { + mode: 'value', + key: '', + order: 'asc' +}; + +const exampleCards: CardExampleType[] = [ + { + title: 'Sort by name ascending', + description: + 'Sort a JSON array of objects alphabetically by the "name" key.', + sampleText: `[{"name":"Charlie","age":30},{"name":"Alice","age":25},{"name":"Bob","age":35}]`, + sampleResult: `[ + { + "name": "Alice", + "age": 25 + }, + { + "name": "Bob", + "age": 35 + }, + { + "name": "Charlie", + "age": 30 + } +]`, + sampleOptions: { mode: 'value', key: 'name', order: 'asc' } + }, + { + title: 'Sort object keys alphabetically', + description: + 'Sort the keys of a JSON object alphabetically in ascending order.', + sampleText: `{"zebra":1,"apple":2,"mango":3}`, + sampleResult: `{ + "apple": 2, + "mango": 3, + "zebra": 1 +}`, + sampleOptions: { mode: 'key', key: '', order: 'asc' } + } +]; + +export default function SortJson({ + title, + longDescription +}: ToolComponentProps) { + const { t } = useTranslation('json'); + const [input, setInput] = useState(''); + const [result, setResult] = useState(''); + + const compute = (values: InitialValuesType, input: string) => { + if (!input.trim()) return; + setResult(sortJson(input, values)); + }; + + const keys = getJsonHeaders(input); + const keyOptions = + keys.length > 0 + ? keys.map((item) => ({ + label: item, + value: item + })) + : []; + + const getGroups: GetGroupsType = ({ + values, + updateField + }) => [ + { + title: t('sortJson.options.title'), + component: ( + + { + updateField('mode', value); + updateField('key', ''); + }} + description={t('sortJson.options.modeDescription')} + /> + {values.mode === 'value' && ( + updateField('key', value)} + description={t('sortJson.options.keyDescription')} + /> + )} + updateField('order', value)} + description={t('sortJson.options.orderDescription')} + /> + + ) + } + ]; + + return ( + + } + resultComponent={ + + } + initialValues={initialValues} + getGroups={getGroups} + compute={compute} + input={input} + setInput={setInput} + exampleCards={exampleCards} + toolInfo={{ title: `What is a ${title}?`, description: longDescription }} + /> + ); +}