mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-01-12 19:02:53 +08:00
fix encode decode uri
This commit is contained in:
parent
42667f7268
commit
2e21e670df
@ -4,6 +4,7 @@ import styled from "styled-components";
|
|||||||
import { useCopyToClipboard } from "usehooks-ts";
|
import { useCopyToClipboard } from "usehooks-ts";
|
||||||
import { Modal, ModalProps } from "src/components/Modal";
|
import { Modal, ModalProps } from "src/components/Modal";
|
||||||
import { Button } from "src/components/Button";
|
import { Button } from "src/components/Button";
|
||||||
|
import { BiErrorAlt } from "react-icons/bi";
|
||||||
import { compress } from "compress-json";
|
import { compress } from "compress-json";
|
||||||
import { useConfig } from "src/hocs/config";
|
import { useConfig } from "src/hocs/config";
|
||||||
|
|
||||||
@ -20,6 +21,17 @@ const StyledInput = styled.input`
|
|||||||
height: 30px;
|
height: 30px;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const StyledWarning = styled.p``;
|
||||||
|
|
||||||
|
const StyledErrorWrapper = styled.div`
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
color: ${({ theme }) => theme.TEXT_DANGER};
|
||||||
|
font-weight: 600;
|
||||||
|
`;
|
||||||
|
|
||||||
export const ShareModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
|
export const ShareModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
|
||||||
const { json } = useConfig();
|
const { json } = useConfig();
|
||||||
const [url, setURL] = React.useState("");
|
const [url, setURL] = React.useState("");
|
||||||
@ -29,7 +41,9 @@ export const ShareModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
|
|||||||
const jsonEncode = compress(JSON.parse(json));
|
const jsonEncode = compress(JSON.parse(json));
|
||||||
const jsonString = JSON.stringify(jsonEncode);
|
const jsonString = JSON.stringify(jsonEncode);
|
||||||
|
|
||||||
setURL(`https://jsonvisio.com/editor?json=${encodeURI(jsonString)}`);
|
setURL(
|
||||||
|
`http://localhost:3000/editor?json=${encodeURIComponent(jsonString)}`
|
||||||
|
);
|
||||||
}, [json]);
|
}, [json]);
|
||||||
|
|
||||||
const handleShare = () => {
|
const handleShare = () => {
|
||||||
@ -42,12 +56,24 @@ export const ShareModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
|
|||||||
<Modal visible={visible} setVisible={setVisible}>
|
<Modal visible={visible} setVisible={setVisible}>
|
||||||
<Modal.Header>Create a Share Link</Modal.Header>
|
<Modal.Header>Create a Share Link</Modal.Header>
|
||||||
<Modal.Content>
|
<Modal.Content>
|
||||||
<StyledInput value={url} type="url" readOnly />
|
{url.length > 5000 ? (
|
||||||
|
<StyledErrorWrapper>
|
||||||
|
<BiErrorAlt size={60} />
|
||||||
|
<StyledWarning>
|
||||||
|
Link size exceeds 5000 characters, unable to generate link for
|
||||||
|
file of this size!
|
||||||
|
</StyledWarning>
|
||||||
|
</StyledErrorWrapper>
|
||||||
|
) : (
|
||||||
|
<StyledInput value={url} type="url" readOnly />
|
||||||
|
)}
|
||||||
</Modal.Content>
|
</Modal.Content>
|
||||||
<Modal.Controls setVisible={setVisible}>
|
<Modal.Controls setVisible={setVisible}>
|
||||||
<Button status="SECONDARY" onClick={handleShare}>
|
{url.length < 5000 && (
|
||||||
Copy
|
<Button status="SECONDARY" onClick={handleShare}>
|
||||||
</Button>
|
Copy
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</Modal.Controls>
|
</Modal.Controls>
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
|
@ -50,10 +50,11 @@ const WithConfig: ReactComponent = ({ children }) => {
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const jsonStored = localStorage.getItem("json");
|
const jsonStored = localStorage.getItem("json");
|
||||||
const isJsonValid = typeof json === "string" && isValidJson(json);
|
const isJsonValid =
|
||||||
|
typeof json === "string" && isValidJson(decodeURIComponent(json));
|
||||||
|
|
||||||
if (isJsonValid) {
|
if (isJsonValid) {
|
||||||
const jsonDecoded = decompress(JSON.parse(json));
|
const jsonDecoded = decompress(JSON.parse(isJsonValid));
|
||||||
const jsonString = JSON.stringify(jsonDecoded);
|
const jsonString = JSON.stringify(jsonDecoded);
|
||||||
|
|
||||||
dispatch({ type: ConfigActionType.SET_JSON, payload: jsonString });
|
dispatch({ type: ConfigActionType.SET_JSON, payload: jsonString });
|
||||||
|
@ -4,5 +4,5 @@ export const isValidJson = (str: string) => {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return str;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user