TBodyCount.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, { FC, useState } from "react";
  2. import Router from "next/router";
  3. import styled from "styled-components";
  4. import URL from "url";
  5. import QRCode from "qrcode.react";
  6. import { Flex } from "reflexbox/styled-components";
  7. import TBodyButton from "./TBodyButton";
  8. import Modal from "../../Modal";
  9. interface Props {
  10. showModal: any;
  11. url: {
  12. count: number;
  13. domain: string;
  14. id: string;
  15. password: boolean;
  16. shortLink: string;
  17. visit_count: number;
  18. };
  19. }
  20. const Actions = styled(Flex).attrs({
  21. justifyContent: "center",
  22. alignItems: "center"
  23. })`
  24. button {
  25. margin: 0 2px 0 12px;
  26. }
  27. `;
  28. const Icon = styled.img`
  29. width: 12px;
  30. height: 12px;
  31. `;
  32. const TBodyCount: FC<Props> = ({ url }) => {
  33. const [showModal, setShowModal] = useState(false);
  34. const toggleQrCodeModal = () => setShowModal(current => !current);
  35. function goTo(e) {
  36. e.preventDefault();
  37. const { id, domain } = url;
  38. Router.push(`/stats?id=${id}${domain ? `&domain=${domain}` : ""}`);
  39. }
  40. const showQrCode = window.innerWidth > 640;
  41. return (
  42. <Flex flex="1 1 auto" justifyContent="space-between" alignItems="center">
  43. {url.visit_count || 0}
  44. <Actions>
  45. {url.password && <Icon src="/images/lock.svg" />}
  46. {url.visit_count > 0 && (
  47. <TBodyButton withText onClick={goTo}>
  48. <Icon src="/images/chart.svg" />
  49. Stats
  50. </TBodyButton>
  51. )}
  52. {showQrCode && (
  53. <TBodyButton onClick={toggleQrCodeModal}>
  54. <Icon src="/images/qrcode.svg" />
  55. </TBodyButton>
  56. )}
  57. <TBodyButton
  58. data-id={url.id}
  59. data-host={URL.parse(url.shortLink).hostname}
  60. onClick={toggleQrCodeModal} // FIXME: what does this do?
  61. >
  62. <Icon src="/images/trash.svg" />
  63. </TBodyButton>
  64. </Actions>
  65. <Modal show={showModal}>
  66. <QRCode value={url.shortLink} size={196} />
  67. </Modal>
  68. </Flex>
  69. );
  70. };
  71. export default TBodyCount;