HelpModal.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import cn from "classnames";
  2. import EasyModal, { type InnerModalProps } from "ez-modal-react";
  3. import { useEffect, useState } from "react";
  4. import Modal from "react-bootstrap/Modal";
  5. import ReactMarkdown from "react-markdown";
  6. import { Button } from "src/components";
  7. import { getLocale, T } from "src/locale";
  8. import { getHelpFile } from "src/locale/src/HelpDoc";
  9. interface Props extends InnerModalProps {
  10. section: string;
  11. color?: string;
  12. }
  13. const showHelpModal = (section: string, color?: string) => {
  14. EasyModal.show(HelpModal, { section, color });
  15. };
  16. const HelpModal = EasyModal.create(({ section, color, visible, remove }: Props) => {
  17. const [markdownText, setMarkdownText] = useState("");
  18. const lang = getLocale(true);
  19. useEffect(() => {
  20. try {
  21. const docFile = getHelpFile(lang, section) as any;
  22. fetch(docFile)
  23. .then((response) => response.text())
  24. .then(setMarkdownText);
  25. } catch (ex: any) {
  26. setMarkdownText(`**ERROR:** ${ex.message}`);
  27. }
  28. }, [lang, section]);
  29. return (
  30. <Modal show={visible} onHide={remove}>
  31. <Modal.Body>
  32. <ReactMarkdown>{markdownText}</ReactMarkdown>
  33. </Modal.Body>
  34. <Modal.Footer>
  35. <Button
  36. type="button"
  37. actionType="primary"
  38. className={cn("ms-auto", color ? `btn-${color}` : null)}
  39. data-bs-dismiss="modal"
  40. onClick={remove}
  41. >
  42. <T id="action.close" />
  43. </Button>
  44. </Modal.Footer>
  45. </Modal>
  46. );
  47. });
  48. export { showHelpModal };