2024-01-08 16:58:37 +08:00
|
|
|
import React, {useState} from "react";
|
2023-12-13 12:39:15 +08:00
|
|
|
import {Modal} from "react-bootstrap";
|
|
|
|
import {useShopStore} from "./shop_store";
|
|
|
|
import {useClickAway} from "./options/useClickAway";
|
2023-12-14 16:29:32 +08:00
|
|
|
|
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
import {useRenderCount} from "@uidotdev/usehooks";
|
2024-01-08 16:58:37 +08:00
|
|
|
import {Validation} from "./validate";
|
|
|
|
|
|
|
|
|
|
|
|
const copyButtonStates = {
|
|
|
|
[Validation.OK]: {
|
|
|
|
style: "btn-outline-success",
|
|
|
|
content: "✓ copied"
|
|
|
|
},
|
|
|
|
[Validation.Empty]: {
|
|
|
|
style: "btn-outline-primary",
|
|
|
|
content: "Copy"
|
|
|
|
},
|
|
|
|
[Validation.Invalid]: {
|
|
|
|
style: "btn-outline-danger",
|
|
|
|
content: "Error"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-12-13 12:39:15 +08:00
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
export function ShowJSON() {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
const shouldShow = useShopStore((state) => state.shouldShowDescription);
|
|
|
|
const description = useShopStore((state) => state.description);
|
|
|
|
const closeDescription = useShopStore((state) => state.closeDescription);
|
|
|
|
const showDescription = useShopStore((state) => state.showDescription);
|
|
|
|
|
2024-01-08 16:58:37 +08:00
|
|
|
const [copiedState, setCopiedState] = useState(Validation.Empty);
|
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("ShowJSON renders: ", renderCount)
|
2023-12-13 12:39:15 +08:00
|
|
|
|
|
|
|
const ref = useClickAway((e) => {
|
|
|
|
if (e.type === "mousedown") // ignore touchstart
|
|
|
|
closeDescription()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-01-08 16:58:37 +08:00
|
|
|
const copyToClipboard = (text) => {
|
|
|
|
try {
|
|
|
|
navigator.clipboard.writeText(text)
|
|
|
|
.then((_value) => { // success
|
|
|
|
setCopiedState(Validation.OK);
|
|
|
|
setTimeout(() => {setCopiedState(Validation.Empty)}, 1500);
|
|
|
|
}, (reason) => { // error
|
|
|
|
setCopiedState(Validation.Invalid);
|
|
|
|
setTimeout(() => {setCopiedState(Validation.Empty)}, 3000);
|
|
|
|
console.warn("Copy to clipboard rejected: ", reason)
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
setCopiedState(Validation.Invalid);
|
|
|
|
setTimeout(() => {setCopiedState(Validation.Empty)}, 3000);
|
|
|
|
console.warn("Copy to clipboard error: ", e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-13 12:39:15 +08:00
|
|
|
return (<>
|
|
|
|
<input
|
|
|
|
className="btn btn-outline-primary w-100 m-0 mb-2 mb-sm-0 me-sm-2"
|
|
|
|
style={{'cursor': 'pointer', 'fontWeight': '700'}}
|
|
|
|
defaultValue="Show JSON"
|
|
|
|
onClick={showDescription}
|
|
|
|
readOnly={true}/>
|
2023-12-15 15:24:23 +08:00
|
|
|
<Modal show={shouldShow} animation={true} className="rfqFeedback" centered>
|
|
|
|
<Modal.Body ref={ref}>
|
2023-12-13 12:39:15 +08:00
|
|
|
<textarea
|
|
|
|
value={description}
|
|
|
|
className="form-control w-100"
|
|
|
|
rows={10}
|
|
|
|
readOnly={true}
|
|
|
|
placeholder="There should be description of the crate"/>
|
|
|
|
|
2023-12-14 14:17:18 +08:00
|
|
|
<div className="d-flex flex-column flex-sm-row justify-content-end">
|
2024-01-08 16:58:37 +08:00
|
|
|
<a type="button"
|
|
|
|
onClick={() => {copyToClipboard(description)}}
|
|
|
|
className={"btn btn-sm m-0 mb-1 mt-2 mb-sm-0 me-sm-2 " + copyButtonStates[copiedState].style}
|
|
|
|
>
|
|
|
|
{ copyButtonStates[copiedState].content }
|
|
|
|
</a>
|
2023-12-13 12:39:15 +08:00
|
|
|
<a type="button" onClick={closeDescription}
|
2023-12-14 14:17:18 +08:00
|
|
|
className="btn btn-sm btn-outline-primary m-0 mb-1 mt-2 mb-sm-0 me-sm-2">Close</a>
|
2023-12-13 12:39:15 +08:00
|
|
|
</div>
|
|
|
|
</Modal.Body>
|
|
|
|
</Modal>
|
|
|
|
</>)
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|