| 1 | import { XMarkIcon } from "@heroicons/react/24/outline"; |
| 2 | import React, { isValidElement, useEffect } from "react"; |
| 3 | import ReactMarkdown from "react-markdown"; |
| 4 | import styled from "styled-components"; |
| 5 | import { |
| 6 | CloseButton, |
| 7 | VSC_BACKGROUND_VAR, |
| 8 | defaultBorderRadius, |
| 9 | parseColorForHex, |
| 10 | vscBackground, |
| 11 | vscForeground, |
| 12 | } from ".."; |
| 13 | |
| 14 | interface TextDialogProps { |
| 15 | showDialog: boolean; |
| 16 | onEnter: () => void; |
| 17 | onClose: () => void; |
| 18 | message?: string | JSX.Element; |
| 19 | } |
| 20 | |
| 21 | const ScreenCover = styled.div` |
| 22 | position: fixed; |
| 23 | width: 100%; |
| 24 | height: 100%; |
| 25 | background-color: ${parseColorForHex(VSC_BACKGROUND_VAR)}aa; |
| 26 | z-index: 100000; |
| 27 | flex-direction: column; |
| 28 | `; |
| 29 | |
| 30 | const DialogContainer = styled.div` |
| 31 | position: absolute; |
| 32 | top: 50%; |
| 33 | left: 50%; |
| 34 | transform: translate(-50%, -50%); |
| 35 | color: ${vscForeground}; |
| 36 | background-color: ${vscBackground}; |
| 37 | border-radius: ${defaultBorderRadius}; |
| 38 | display: flex; |
| 39 | flex-direction: column; |
| 40 | box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); |
| 41 | word-wrap: break-word; |
| 42 | `; |
| 43 | |
| 44 | const TextDialog = (props: TextDialogProps) => { |
| 45 | useEffect(() => { |
| 46 | const handleKeyDown = (event: KeyboardEvent) => { |
| 47 | if (event.key === "Escape") { |
| 48 | props.onClose(); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | document.addEventListener("keydown", handleKeyDown); |
| 53 | return () => { |
| 54 | document.removeEventListener("keydown", handleKeyDown); |
| 55 | }; |
| 56 | }, [props]); |
| 57 | |
| 58 | if (!isValidElement(props.message) && typeof props.message !== "string") { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | return ( |
| 63 | <ScreenCover onClick={props.onClose} hidden={!props.showDialog}> |
| 64 | <DialogContainer |
| 65 | className="xs:w-[90%] no-scrollbar max-h-[95%] w-[92%] max-w-[600px] overflow-auto sm:w-[88%] md:w-[80%]" |
| 66 | onClick={(e) => { |
| 67 | e.stopPropagation(); |
| 68 | }} |
| 69 | > |
| 70 | <CloseButton onClick={props.onClose}> |
| 71 | <XMarkIcon className="z-50 h-5 w-5 hover:brightness-125" /> |
| 72 | </CloseButton> |
| 73 | |
| 74 | {typeof props.message === "string" ? ( |
| 75 | <ReactMarkdown>{props.message || ""}</ReactMarkdown> |
| 76 | ) : !React.isValidElement(props.message) ? null : ( |
| 77 | props.message |
| 78 | )} |
| 79 | </DialogContainer> |
| 80 | </ScreenCover> |
| 81 | ); |
| 82 | }; |
| 83 | |
| 84 | export default TextDialog; |