ResourceTab.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import { kindToIcon } from "shared/rosettaStone";
  4. type PropsType = {
  5. label: string;
  6. name: string;
  7. handleClick?: () => void;
  8. selected?: boolean;
  9. isLast?: boolean;
  10. roundAllCorners?: boolean;
  11. status?: {
  12. label: string;
  13. available?: number;
  14. total?: number;
  15. } | null;
  16. expanded?: boolean;
  17. };
  18. type StateType = {
  19. expanded: boolean;
  20. showTooltip: boolean;
  21. };
  22. export default class ResourceTab extends Component<PropsType, StateType> {
  23. state = {
  24. expanded: this.props.expanded || false,
  25. showTooltip: false
  26. };
  27. renderDropdownIcon = () => {
  28. if (this.props.children) {
  29. return (
  30. <DropdownIcon expanded={this.state.expanded}>
  31. <i className="material-icons">arrow_right</i>
  32. </DropdownIcon>
  33. );
  34. }
  35. };
  36. renderIcon = (kind: string) => {
  37. let icon = "tonality";
  38. if (Object.keys(kindToIcon).includes(kind)) {
  39. icon = kindToIcon[kind];
  40. }
  41. return (
  42. <IconWrapper>
  43. <i className="material-icons">{icon}</i>
  44. </IconWrapper>
  45. );
  46. };
  47. renderTooltip = (x: string): JSX.Element | undefined => {
  48. if (this.state.showTooltip) {
  49. return <Tooltip>{x}</Tooltip>;
  50. }
  51. };
  52. getStatusText = () => {
  53. let { status } = this.props;
  54. if (status.available && status.total) {
  55. return `${status.available}/${status.total}`;
  56. } else if (status.label) {
  57. return status.label;
  58. }
  59. };
  60. renderStatus = () => {
  61. let { status } = this.props;
  62. if (status) {
  63. return (
  64. <Status>
  65. {this.getStatusText()}
  66. <StatusColor status={status.label} />
  67. </Status>
  68. );
  69. }
  70. };
  71. renderExpanded = () => {
  72. if (this.props.children && this.state.expanded) {
  73. return <ExpandWrapper>{this.props.children}</ExpandWrapper>;
  74. }
  75. };
  76. render() {
  77. let {
  78. label,
  79. name,
  80. children,
  81. isLast,
  82. handleClick,
  83. selected,
  84. status,
  85. roundAllCorners
  86. } = this.props;
  87. return (
  88. <StyledResourceTab
  89. isLast={isLast}
  90. onClick={() => handleClick && handleClick()}
  91. roundAllCorners={roundAllCorners}
  92. >
  93. <ResourceHeader
  94. hasChildren={children && true}
  95. expanded={this.state.expanded || selected}
  96. onClick={() => {
  97. if (children) {
  98. this.setState({ expanded: !this.state.expanded });
  99. }
  100. }}
  101. >
  102. <Info>
  103. {this.renderDropdownIcon()}
  104. <Metadata hasStatus={status && true}>
  105. {this.renderIcon(label)}
  106. {label}
  107. <ResourceName
  108. showKindLabels={true}
  109. onMouseOver={() => {
  110. this.setState({ showTooltip: true });
  111. }}
  112. onMouseOut={() => {
  113. this.setState({ showTooltip: false });
  114. }}
  115. >
  116. {name}
  117. </ResourceName>
  118. {this.renderTooltip(name)}
  119. </Metadata>
  120. </Info>
  121. {this.renderStatus()}
  122. </ResourceHeader>
  123. {this.renderExpanded()}
  124. </StyledResourceTab>
  125. );
  126. }
  127. }
  128. const StyledResourceTab = styled.div`
  129. width: 100%;
  130. margin-bottom: 2px;
  131. background: #ffffff11;
  132. border-bottom-left-radius: ${(props: {
  133. isLast: boolean;
  134. roundAllCorners: boolean;
  135. }) => (props.isLast ? "5px" : "")};
  136. border-bottom-right-radius: ${(props: {
  137. isLast: boolean;
  138. roundAllCorners: boolean;
  139. }) => (props.roundAllCorners && props.isLast ? "5px" : "")};
  140. `;
  141. const Tooltip = styled.div`
  142. position: absolute;
  143. right: 0px;
  144. top: 25px;
  145. white-space: nowrap;
  146. height: 18px;
  147. padding: 2px 5px;
  148. background: #383842dd;
  149. display: flex;
  150. align-items: center;
  151. justify-content: center;
  152. flex: 1;
  153. color: white;
  154. text-transform: none;
  155. font-size: 12px;
  156. font-family: "Work Sans", sans-serif;
  157. outline: 1px solid #ffffff55;
  158. opacity: 0;
  159. animation: faded-in 0.2s 0.15s;
  160. animation-fill-mode: forwards;
  161. @keyframes faded-in {
  162. from {
  163. opacity: 0;
  164. }
  165. to {
  166. opacity: 1;
  167. }
  168. }
  169. `;
  170. const ExpandWrapper = styled.div``;
  171. const ResourceHeader = styled.div`
  172. width: 100%;
  173. height: 50px;
  174. display: flex;
  175. font-size: 13px;
  176. align-items: center;
  177. justify-content: space-between;
  178. color: #ffffff66;
  179. user-select: none;
  180. padding: 8px 18px;
  181. padding-left: ${(props: { expanded: boolean; hasChildren: boolean }) =>
  182. props.hasChildren ? "10px" : "22px"};
  183. cursor: pointer;
  184. background: ${(props: { expanded: boolean; hasChildren: boolean }) =>
  185. props.expanded ? "#ffffff11" : ""};
  186. :hover {
  187. background: #ffffff18;
  188. > i {
  189. background: #ffffff22;
  190. }
  191. }
  192. `;
  193. const Info = styled.div`
  194. display: flex;
  195. flex-direction: row;
  196. align-items: center;
  197. width: 80%;
  198. height: 100%;
  199. `;
  200. const Metadata = styled.div`
  201. display: flex;
  202. align-items: center;
  203. position: relative;
  204. max-width: ${(props: { hasStatus: boolean }) =>
  205. props.hasStatus ? "calc(100% - 20px)" : "100%"};
  206. `;
  207. const Status = styled.div`
  208. display: flex;
  209. width; 20%;
  210. font-size: 12px;
  211. text-transform: capitalize;
  212. justify-content: flex-end;
  213. align-items: center;
  214. font-family: 'Work Sans', sans-serif;
  215. color: #aaaabb;
  216. animation: fadeIn 0.5s;
  217. @keyframes fadeIn {
  218. from { opacity: 0 }
  219. to { opacity: 1 }
  220. }
  221. `;
  222. const StatusColor = styled.div`
  223. margin-left: 12px;
  224. width: 8px;
  225. min-width: 8px;
  226. height: 8px;
  227. background: ${(props: { status: string }) =>
  228. props.status === "running" ||
  229. props.status === "Ready" ||
  230. props.status === "Completed"
  231. ? "#4797ff"
  232. : props.status === "failed" || props.status === "FailedValidation"
  233. ? "#ed5f85"
  234. : props.status === "completed"
  235. ? "#00d12a"
  236. : "#f5cb42"};
  237. border-radius: 20px;
  238. `;
  239. const ResourceName = styled.div`
  240. color: #ffffff;
  241. max-width: 40%;
  242. margin-left: ${(props: { showKindLabels: boolean }) =>
  243. props.showKindLabels ? "10px" : ""};
  244. text-transform: none;
  245. white-space: nowrap;
  246. overflow: hidden;
  247. text-overflow: ellipsis;
  248. `;
  249. const IconWrapper = styled.div`
  250. width: 25px;
  251. height: 25px;
  252. display: flex;
  253. align-items: center;
  254. justify-content: center;
  255. > i {
  256. font-size: 15px;
  257. color: #ffffff;
  258. margin-right: 14px;
  259. }
  260. `;
  261. const DropdownIcon = styled.div`
  262. > i {
  263. margin-top: 2px;
  264. margin-right: 11px;
  265. font-size: 20px;
  266. color: #ffffff66;
  267. cursor: pointer;
  268. border-radius: 20px;
  269. background: ${(props: { expanded: boolean }) =>
  270. props.expanded ? "#ffffff18" : ""};
  271. transform: ${(props: { expanded: boolean }) =>
  272. props.expanded ? "rotate(180deg)" : ""};
  273. animation: ${(props: { expanded: boolean }) =>
  274. props.expanded ? "quarterTurn 0.3s" : ""};
  275. animation-fill-mode: forwards;
  276. @keyframes quarterTurn {
  277. from {
  278. transform: rotate(0deg);
  279. }
  280. to {
  281. transform: rotate(90deg);
  282. }
  283. }
  284. }
  285. `;