mirror of
https://github.com/ershisan99/DevToysWeb.git
synced 2025-12-17 04:59:23 +00:00
feat: add url encoder / decoder
This commit is contained in:
55
src/components/pages/encoders-decoders/url/Content.tsx
Normal file
55
src/components/pages/encoders-decoders/url/Content.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { ComponentPropsWithoutRef, memo, useCallback, useState } from "react";
|
||||||
|
|
||||||
|
import { Main, MainItem, TextField } from "@/components/common";
|
||||||
|
|
||||||
|
type TextFieldValue = ComponentPropsWithoutRef<typeof TextField>["value"];
|
||||||
|
type OnTextFieldChange = NonNullable<ComponentPropsWithoutRef<typeof TextField>["onChange"]>;
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
decoded: TextFieldValue;
|
||||||
|
encoded: TextFieldValue;
|
||||||
|
onDecodedChange: OnTextFieldChange;
|
||||||
|
onEncodedChange: OnTextFieldChange;
|
||||||
|
};
|
||||||
|
|
||||||
|
const StyledComponent = ({ decoded, encoded, onDecodedChange, onEncodedChange }: Props) => (
|
||||||
|
<Main title="URL Encoder / Decoder">
|
||||||
|
<MainItem title="Decoded">
|
||||||
|
<TextField multiline rows={10} value={decoded} onChange={onDecodedChange} />
|
||||||
|
</MainItem>
|
||||||
|
<MainItem title="Encoded">
|
||||||
|
<TextField multiline rows={10} value={encoded} onChange={onEncodedChange} />
|
||||||
|
</MainItem>
|
||||||
|
</Main>
|
||||||
|
);
|
||||||
|
|
||||||
|
export const Component = memo(StyledComponent);
|
||||||
|
|
||||||
|
const Container = () => {
|
||||||
|
const [decoded, setDecoded] = useState("http://example.com?q=foo bar");
|
||||||
|
const [encoded, setEncoded] = useState("http://example.com?q=foo%20bar");
|
||||||
|
|
||||||
|
const onDecodedChange: Props["onDecodedChange"] = useCallback(({ currentTarget: { value } }) => {
|
||||||
|
setDecoded(value);
|
||||||
|
|
||||||
|
try {
|
||||||
|
setEncoded(encodeURI(value));
|
||||||
|
} catch {
|
||||||
|
setEncoded("");
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onEncodedChange: Props["onEncodedChange"] = useCallback(({ currentTarget: { value } }) => {
|
||||||
|
setEncoded(value);
|
||||||
|
|
||||||
|
try {
|
||||||
|
setDecoded(decodeURI(value));
|
||||||
|
} catch {
|
||||||
|
setDecoded("");
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return <Component {...{ decoded, encoded, onDecodedChange, onEncodedChange }} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Container;
|
||||||
3
src/components/pages/encoders-decoders/url/index.ts
Normal file
3
src/components/pages/encoders-decoders/url/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import Content from "./Content";
|
||||||
|
|
||||||
|
export { Content };
|
||||||
@@ -64,8 +64,8 @@ const toolGroups = [
|
|||||||
description:
|
description:
|
||||||
"Encode or decode all the applicable characters to their corresponding URL entities",
|
"Encode or decode all the applicable characters to their corresponding URL entities",
|
||||||
keywords: "url encoder escaper decocder unescaper",
|
keywords: "url encoder escaper decocder unescaper",
|
||||||
href: pagesPath.$url(),
|
href: pagesPath.encoders_decoders.url.$url(),
|
||||||
disabled: true,
|
disabled: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
icon: <DragHandle />,
|
icon: <DragHandle />,
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ export const pagesPath = {
|
|||||||
hash: url?.hash,
|
hash: url?.hash,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
url: {
|
||||||
|
$url: (url?: { hash?: string }) => ({
|
||||||
|
pathname: "/encoders-decoders/url" as const,
|
||||||
|
hash: url?.hash,
|
||||||
|
}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
search: {
|
search: {
|
||||||
$url: (url?: { hash?: string }) => ({ pathname: "/search" as const, hash: url?.hash }),
|
$url: (url?: { hash?: string }) => ({ pathname: "/search" as const, hash: url?.hash }),
|
||||||
|
|||||||
7
src/pages/encoders-decoders/url.tsx
Normal file
7
src/pages/encoders-decoders/url.tsx
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import type { NextPage } from "next";
|
||||||
|
|
||||||
|
import { Content } from "@/components/pages/encoders-decoders/url";
|
||||||
|
|
||||||
|
const Page: NextPage = Content;
|
||||||
|
|
||||||
|
export default Page;
|
||||||
Reference in New Issue
Block a user