()
| 15 | import { ModePicker } from "@/components/shared/ModePicker"; |
| 16 | |
| 17 | const SAMPLE_URL = "https://example.com/search?q=hello world&lang=en&filter=a+b"; |
| 18 | const SAMPLE_TEXT = "Hello World! Special chars: @#$%^&*() URL: https://example.com/path?foo=bar"; |
| 19 | |
| 20 | export default function UrlEncoderDecoder() { |
| 21 | const t = useTranslations("Tools.UrlEncoderDecoder"); |
| 22 | const tc = useTranslations("ToolsConfig"); |
| 23 | const [input, setInput] = useState(""); |
| 24 | const [output, setOutput] = useState(""); |
| 25 | const [tab, setTab] = useState("encode"); |
| 26 | const [fullUrl, setFullUrl] = useState(false); |
| 27 | |
| 28 | useEffect(() => { |
| 29 | if (!input.trim()) { setOutput(""); return; } |
| 30 | try { |
| 31 | if (tab === "encode") { |
| 32 | setOutput(fullUrl ? encodeURI(input) : encodeURIComponent(input)); |
| 33 | } else { |
| 34 | setOutput(decodeURIComponent(input)); |
| 35 | } |
| 36 | } catch { setOutput(""); } |
| 37 | }, [input, tab, fullUrl]); |
| 38 | |
| 39 | const handleEncode = () => { |
| 40 | if (!input.trim()) { toast.error(t("pleaseEnterInput")); return; } |
| 41 | try { |
| 42 | setOutput(fullUrl ? encodeURI(input) : encodeURIComponent(input)); |
| 43 | toast.success(t("encodedSuccessfully")); |
| 44 | } catch (e) { toast.error(t("encodingFailed", { msg: e instanceof Error ? e.message : String(e) })); } |
| 45 | }; |
| 46 | |
| 47 | const handleDecode = () => { |
| 48 | if (!input.trim()) { toast.error(t("pleaseEnterInput")); return; } |
| 49 | try { |
| 50 | setOutput(decodeURIComponent(input)); |
| 51 | toast.success(t("decodedSuccessfully")); |
| 52 | } catch (e) { toast.error(t("decodingFailed", { msg: e instanceof Error ? e.message : String(e) })); } |
| 53 | }; |
| 54 | |
| 55 | const handleSwap = () => { setInput(output); setOutput(input); }; |
| 56 | const handleClear = () => { setInput(""); setOutput(""); }; |
| 57 | |
| 58 | const handleLoadSample = () => { setInput(fullUrl ? SAMPLE_URL : SAMPLE_TEXT); }; |
| 59 | |
| 60 | return ( |
| 61 | <ToolShell |
| 62 | slug="url-encoder" |
| 63 | title={tc("tools.url-encoder.name")} |
| 64 | sub={tc("tools.url-encoder.description")} |
| 65 | > |
| 66 | <div className="space-y-4"> |
| 67 | <div className="flex flex-wrap items-center gap-2"> |
| 68 | <ModePicker |
| 69 | aria-label={t("encode")} |
| 70 | value={tab} |
| 71 | onChange={setTab} |
| 72 | options={[ |
| 73 | { value: "encode", label: t("encode") }, |
| 74 | { value: "decode", label: t("decode") }, |
nothing calls this directly
no outgoing calls
no test coverage detected