ButtonEnablePREnvironments.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { useContext, useEffect, useState } from "react";
  2. import api from "shared/api";
  3. import { Context } from "shared/Context";
  4. import styled from "styled-components";
  5. import pr_icon from "assets/pull_request_icon.svg";
  6. import { Link } from "react-router-dom";
  7. import DynamicLink from "components/DynamicLink";
  8. import Loading from "components/Loading";
  9. type Props = {
  10. setIsReady: (status: boolean) => void;
  11. };
  12. // TODO: Billing is still not capable to show if a user can use or not PR environments, add that instead of "hasBillingEnabled"
  13. const ButtonEnablePREnvironments = ({ setIsReady }: Props) => {
  14. // const { hasBillingEnabled } = useContext(Context);
  15. const [isLoading, setIsLoading] = useState(true);
  16. const [hasGHAccountConnected, setHasGHAccountConnected] = useState(false);
  17. let hasBillingEnabled = true;
  18. const getAccounts = async () => {
  19. setIsLoading(true);
  20. try {
  21. const res = await api.getGithubAccounts("<token>", {}, {});
  22. if (res.status !== 200) {
  23. throw new Error("Not authorized");
  24. }
  25. return res.data;
  26. } catch (error) {
  27. console.log(error);
  28. } finally {
  29. setIsLoading(false);
  30. }
  31. };
  32. useEffect(() => {
  33. let isSubscribed = true;
  34. getAccounts().then((accountsData) => {
  35. if (isSubscribed) {
  36. if (!accountsData) {
  37. setHasGHAccountConnected(false);
  38. } else {
  39. setHasGHAccountConnected(true);
  40. }
  41. }
  42. });
  43. return () => {
  44. isSubscribed = false;
  45. };
  46. }, []);
  47. useEffect(() => {
  48. setIsReady(!isLoading);
  49. }, [isLoading]);
  50. const getButtonProps = () => {
  51. const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
  52. const encoded_redirect_uri = encodeURIComponent(url);
  53. const backendUrl = `${window.location.protocol}//${window.location.host}`;
  54. if (!hasGHAccountConnected) {
  55. return {
  56. to: `${backendUrl}/api/integrations/github-app/install?redirect_uri=${encoded_redirect_uri}`,
  57. target: "_self",
  58. };
  59. }
  60. if (!hasBillingEnabled) {
  61. return {
  62. to: {
  63. pathname: "/project-settings",
  64. search: "?selected_tab=billing",
  65. },
  66. };
  67. }
  68. return {
  69. to: "/preview-environments/connect-repo",
  70. };
  71. };
  72. if (isLoading) {
  73. return (
  74. <Container>
  75. <Button disabled={true} to="">
  76. <img src={pr_icon} alt="Pull request icon" />
  77. Loading . . .
  78. </Button>
  79. </Container>
  80. );
  81. }
  82. if (!hasGHAccountConnected) {
  83. return (
  84. <>
  85. <Container>
  86. <Button {...getButtonProps()}>
  87. <img src={pr_icon} alt="Pull request icon" />
  88. Connect GitHub account
  89. </Button>
  90. </Container>
  91. </>
  92. );
  93. }
  94. return (
  95. <>
  96. <Container>
  97. <Button {...getButtonProps()}>
  98. <i className="material-icons">add</i> Add repository
  99. </Button>
  100. </Container>
  101. </>
  102. );
  103. };
  104. export default ButtonEnablePREnvironments;
  105. const Button = styled(DynamicLink)`
  106. display: flex;
  107. flex-direction: row;
  108. align-items: center;
  109. justify-content: space-between;
  110. font-size: 13px;
  111. cursor: pointer;
  112. font-family: "Work Sans", sans-serif;
  113. border-radius: 5px;
  114. color: white;
  115. height: 30px;
  116. padding: 0px 8px;
  117. padding-bottom: 1px;
  118. margin-right: 10px;
  119. font-weight: 500;
  120. padding-right: 15px;
  121. overflow: hidden;
  122. white-space: nowrap;
  123. text-overflow: ellipsis;
  124. box-shadow: 0 5px 8px 0px #00000010;
  125. cursor: ${(props: { disabled?: boolean }) =>
  126. props.disabled ? "not-allowed" : "pointer"};
  127. background: ${(props: { disabled?: boolean }) =>
  128. props.disabled ? "#aaaabbee" : "#616FEEcc"};
  129. :hover {
  130. background: ${(props: { disabled?: boolean }) =>
  131. props.disabled ? "" : "#505edddd"};
  132. }
  133. img {
  134. margin-left: 2px;
  135. margin-right: 5px;
  136. width: 18px;
  137. height: 18px;
  138. }
  139. > i {
  140. color: white;
  141. width: 18px;
  142. height: 18px;
  143. font-weight: 600;
  144. font-size: 12px;
  145. border-radius: 20px;
  146. display: flex;
  147. align-items: center;
  148. margin-right: 5px;
  149. justify-content: center;
  150. }
  151. `;
  152. const Container = styled.div`
  153. `;