|
| 1 | +import { Box } from '@mui/material'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import ToolTextResult from '@components/result/ToolTextResult'; |
| 4 | +import { GetGroupsType } from '@components/options/ToolOptions'; |
| 5 | +import { encodeString } from './service'; |
| 6 | +import ToolTextInput from '@components/input/ToolTextInput'; |
| 7 | +import { InitialValuesType } from './types'; |
| 8 | +import ToolContent from '@components/ToolContent'; |
| 9 | +import { CardExampleType } from '@components/examples/ToolExamples'; |
| 10 | +import { ToolComponentProps } from '@tools/defineTool'; |
| 11 | +import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; |
| 12 | +import { useTranslation } from 'react-i18next'; |
| 13 | + |
| 14 | +const initialValues: InitialValuesType = { |
| 15 | + nonSpecialChar: false |
| 16 | +}; |
| 17 | + |
| 18 | +const exampleCards: CardExampleType<InitialValuesType>[] = [ |
| 19 | + { |
| 20 | + title: 'Encode an actual URL', |
| 21 | + description: |
| 22 | + 'This example URL-encodes a string that also happens to be a valid web link. Special characters in this example are a colon, slash, question mark and equals sign.', |
| 23 | + sampleText: 'https://omnitools.app/', |
| 24 | + sampleResult: 'https%3A%2F%2Fomnitools.app%2F', |
| 25 | + sampleOptions: initialValues |
| 26 | + }, |
| 27 | + { |
| 28 | + title: 'Encode All Characters', |
| 29 | + description: |
| 30 | + "In this example, we've enabled the option that encodes absolutely all characters in a string to URL-encoding. This option makes non-special characters, such as letters get encoded to their hex codes prefixed by a percent sign.", |
| 31 | + sampleText: "I can't believe it's not butter!", |
| 32 | + sampleResult: |
| 33 | + '%49%20%63%61%6E%27%74%20%62%65%6C%69%65%76%65%20%69%74%27%73%20%6E%6F%74%20%62%75%74%74%65%72%21', |
| 34 | + sampleOptions: { |
| 35 | + nonSpecialChar: true |
| 36 | + } |
| 37 | + } |
| 38 | +]; |
| 39 | + |
| 40 | +export default function EncodeString({ |
| 41 | + title, |
| 42 | + longDescription |
| 43 | +}: ToolComponentProps) { |
| 44 | + const { t } = useTranslation('string'); |
| 45 | + const [input, setInput] = useState<string>(''); |
| 46 | + const [result, setResult] = useState<string>(''); |
| 47 | + |
| 48 | + function compute(initialValues: InitialValuesType, input: string) { |
| 49 | + setResult(encodeString(input, initialValues)); |
| 50 | + } |
| 51 | + |
| 52 | + const getGroups: GetGroupsType<InitialValuesType> = ({ |
| 53 | + values, |
| 54 | + updateField |
| 55 | + }) => [ |
| 56 | + { |
| 57 | + title: t('urlEncode.encodingOption.title'), |
| 58 | + component: ( |
| 59 | + <Box> |
| 60 | + <CheckboxWithDesc |
| 61 | + checked={values.nonSpecialChar} |
| 62 | + onChange={(value) => updateField('nonSpecialChar', value)} |
| 63 | + title={t('urlEncode.encodingOption.nonSpecialCharPlaceholder')} |
| 64 | + description={t( |
| 65 | + 'urlEncode.encodingOption.nonSpecialCharDescription' |
| 66 | + )} |
| 67 | + /> |
| 68 | + </Box> |
| 69 | + ) |
| 70 | + } |
| 71 | + ]; |
| 72 | + |
| 73 | + return ( |
| 74 | + <ToolContent |
| 75 | + title={title} |
| 76 | + initialValues={initialValues} |
| 77 | + getGroups={getGroups} |
| 78 | + compute={compute} |
| 79 | + input={input} |
| 80 | + setInput={setInput} |
| 81 | + inputComponent={ |
| 82 | + <ToolTextInput |
| 83 | + title={t('urlEncode.inputTitle')} |
| 84 | + value={input} |
| 85 | + onChange={setInput} |
| 86 | + /> |
| 87 | + } |
| 88 | + resultComponent={ |
| 89 | + <ToolTextResult title={t('urlEncode.resultTitle')} value={result} /> |
| 90 | + } |
| 91 | + toolInfo={{ |
| 92 | + title: t('urlEncode.toolInfo.title', { title }), |
| 93 | + description: longDescription |
| 94 | + }} |
| 95 | + exampleCards={exampleCards} |
| 96 | + /> |
| 97 | + ); |
| 98 | +} |
0 commit comments