RepoSelector.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import github from "assets/github.png";
  4. import info from "assets/info.svg";
  5. import { RepoType, ChartType, ActionConfigType } from "shared/types";
  6. import { Context } from "shared/Context";
  7. import ButtonTray from "./ButtonTray";
  8. import ActionConfEditor from "./ActionConfEditor";
  9. type PropsType = {
  10. chart: ChartType | null;
  11. forceExpanded?: boolean;
  12. actionConfig: ActionConfigType | null;
  13. setActionConfig: (x: ActionConfigType) => void;
  14. resetActionConfig: () => void;
  15. };
  16. type StateType = {
  17. isExpanded: boolean;
  18. repos: RepoType[];
  19. branch: string;
  20. pathIsSet: boolean;
  21. dockerfileSelected: boolean;
  22. };
  23. export default class RepoSelector extends Component<PropsType, StateType> {
  24. state = {
  25. isExpanded: this.props.forceExpanded,
  26. repos: [] as RepoType[],
  27. branch: "",
  28. pathIsSet: false,
  29. dockerfileSelected: false,
  30. };
  31. renderExpanded = () => {
  32. let { actionConfig, setActionConfig, chart } = this.props;
  33. return (
  34. <div>
  35. <ActionConfEditor
  36. actionConfig={actionConfig}
  37. branch={this.state.branch}
  38. pathIsSet={this.state.pathIsSet}
  39. setActionConfig={setActionConfig}
  40. setBranch={(branch: string) => this.setState({ branch })}
  41. setPath={(pathIsSet: boolean) => this.setState({ pathIsSet })}
  42. reset={() => {
  43. this.setState({
  44. branch: "",
  45. pathIsSet: false,
  46. dockerfileSelected: false,
  47. });
  48. this.props.resetActionConfig();
  49. }}
  50. />
  51. <ButtonTray
  52. chartName={chart.name}
  53. chartNamespace={chart.namespace}
  54. pathIsSet={this.state.pathIsSet}
  55. branch={this.state.branch}
  56. actionConfig={actionConfig}
  57. setBranch={(branch: string) => this.setState({ branch })}
  58. setActionConfig={setActionConfig}
  59. setPath={(pathIsSet: boolean) => this.setState({ pathIsSet })}
  60. />
  61. </div>
  62. );
  63. };
  64. renderSelected = () => {
  65. let { actionConfig } = this.props;
  66. if (actionConfig.git_repo) {
  67. let subdir =
  68. actionConfig.dockerfile_path === ""
  69. ? ""
  70. : "/" + actionConfig.dockerfile_path;
  71. return (
  72. <RepoLabel>
  73. <img src={github} />
  74. {actionConfig.git_repo + subdir}
  75. <SelectedBranch>
  76. {!this.state.branch ? "(Select Branch)" : this.state.branch}
  77. </SelectedBranch>
  78. </RepoLabel>
  79. );
  80. }
  81. return (
  82. <RepoLabel>
  83. <img src={info} />
  84. No source selected
  85. </RepoLabel>
  86. );
  87. };
  88. handleClick = () => {
  89. if (!this.props.forceExpanded) {
  90. this.setState({ isExpanded: !this.state.isExpanded });
  91. }
  92. };
  93. render() {
  94. return (
  95. <>
  96. <StyledRepoSelector
  97. onClick={this.handleClick}
  98. isExpanded={this.state.isExpanded}
  99. forceExpanded={this.props.forceExpanded}
  100. >
  101. {this.renderSelected()}
  102. {this.props.forceExpanded ? null : (
  103. <i className="material-icons">
  104. {this.state.isExpanded ? "close" : "build"}
  105. </i>
  106. )}
  107. </StyledRepoSelector>
  108. {this.state.isExpanded ? this.renderExpanded() : null}
  109. </>
  110. );
  111. }
  112. }
  113. RepoSelector.contextType = Context;
  114. const SelectedBranch = styled.div`
  115. color: #ffffff55;
  116. margin-left: 10px;
  117. `;
  118. const RepoLabel = styled.div`
  119. display: flex;
  120. align-items: center;
  121. > img {
  122. width: 18px;
  123. height: 18px;
  124. margin-left: 12px;
  125. margin-right: 12px;
  126. }
  127. `;
  128. const StyledRepoSelector = styled.div`
  129. width: 100%;
  130. margin-top: 22px;
  131. border: 1px solid #ffffff55;
  132. background: ${(props: { isExpanded: boolean; forceExpanded: boolean }) =>
  133. props.isExpanded ? "#ffffff11" : ""};
  134. border-radius: 3px;
  135. user-select: none;
  136. height: 40px;
  137. font-size: 13px;
  138. color: #ffffff;
  139. display: flex;
  140. align-items: center;
  141. justify-content: space-between;
  142. cursor: ${(props: { isExpanded: boolean; forceExpanded: boolean }) =>
  143. props.forceExpanded ? "" : "pointer"};
  144. :hover {
  145. background: #ffffff11;
  146. > i {
  147. background: #ffffff22;
  148. }
  149. }
  150. > i {
  151. font-size: 16px;
  152. color: #ffffff66;
  153. margin-right: 10px;
  154. display: flex;
  155. align-items: center;
  156. justify-content: center;
  157. border-radius: 20px;
  158. padding: 4px;
  159. }
  160. `;