WelcomeModal.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import { Trans } from "react-i18next";
  4. import styled from "styled-components";
  5. import { toast } from "react-toastify";
  6. import Modal from "../../components/ui/Modal";
  7. import useSession from "../../hooks/useSession";
  8. const StyledUrl = styled.div`
  9. background-color: var(--color-midGrey);
  10. padding: 0.5em;
  11. border-radius: 2px;
  12. display: flex;
  13. align-items: center;
  14. justify-content: space-between;
  15. margin-bottom: 2em;
  16. & span {
  17. overflow: hidden;
  18. white-space: nowrap;
  19. text-overflow: ellipsis;
  20. }
  21. & img {
  22. margin-left: 1em;
  23. cursor: pointer;
  24. }
  25. `;
  26. const WelcomeModal = ({ show, setShow, welcome = true }) => {
  27. const { t } = useTranslation();
  28. const currentUrl = window.location.href;
  29. const inputRef = React.useRef();
  30. const { sessionId: room } = useSession();
  31. const handleCopy = () => {
  32. inputRef.current.style.display = "block";
  33. inputRef.current.select();
  34. document.execCommand("copy");
  35. inputRef.current.style.display = "none";
  36. toast.info(t("Url copied to clipboard!"), { autoClose: 1000 });
  37. };
  38. const meetUrl = `https://meet.jit.si/airboardgame__${room}`;
  39. return (
  40. <Modal
  41. title={welcome ? t("Ready to play ?") : t("Invite more player")}
  42. setShow={setShow}
  43. show={show}
  44. >
  45. <header>
  46. <h3>{t("Share this link with your friends")}</h3>
  47. </header>
  48. <section>
  49. <StyledUrl>
  50. <span>{currentUrl}</span>
  51. <img
  52. className="copy"
  53. src="https://icongr.am/entypo/copy.svg?size=22&color=888886"
  54. alt={t("Copy")}
  55. onClick={handleCopy}
  56. />
  57. </StyledUrl>
  58. <Trans i18nKey="welcomeTip">
  59. <p>
  60. Tip: Use an audio conferencing system to talk with other players,
  61. like <a href={meetUrl}>Jitsi</a> (free & open-source).
  62. </p>
  63. </Trans>
  64. </section>
  65. {welcome && (
  66. <button
  67. onClick={() => {
  68. setShow(false);
  69. }}
  70. className="button success"
  71. style={{ margin: "1em auto" }}
  72. >
  73. {t("I want to play...")}
  74. </button>
  75. )}
  76. <input
  77. value={currentUrl}
  78. readOnly
  79. ref={inputRef}
  80. style={{ display: "none" }}
  81. />
  82. </Modal>
  83. );
  84. };
  85. export default WelcomeModal;