feat: add url encoder / decoder

This commit is contained in:
rusconn
2022-03-30 11:27:43 +09:00
parent ad8b6a26b9
commit c31a128d7a
5 changed files with 73 additions and 2 deletions

View 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;

View File

@@ -0,0 +1,3 @@
import Content from "./Content";
export { Content };

View File

@@ -64,8 +64,8 @@ const toolGroups = [
description:
"Encode or decode all the applicable characters to their corresponding URL entities",
keywords: "url encoder escaper decocder unescaper",
href: pagesPath.$url(),
disabled: true,
href: pagesPath.encoders_decoders.url.$url(),
disabled: false,
},
{
icon: <DragHandle />,

View File

@@ -20,6 +20,12 @@ export const pagesPath = {
hash: url?.hash,
}),
},
url: {
$url: (url?: { hash?: string }) => ({
pathname: "/encoders-decoders/url" as const,
hash: url?.hash,
}),
},
},
search: {
$url: (url?: { hash?: string }) => ({ pathname: "/search" as const, hash: url?.hash }),

View 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;