ResourceTab.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 ? "10px" : "")};
  136. `;
  137. const Tooltip = styled.div`
  138. position: absolute;
  139. right: 0px;
  140. top: 25px;
  141. white-space: nowrap;
  142. height: 18px;
  143. padding: 2px 5px;
  144. background: #383842dd;
  145. display: flex;
  146. align-items: center;
  147. justify-content: center;
  148. flex: 1;
  149. color: white;
  150. text-transform: none;
  151. font-size: 12px;
  152. font-family: "Work Sans", sans-serif;
  153. outline: 1px solid #ffffff55;
  154. opacity: 0;
  155. animation: faded-in 0.2s 0.15s;
  156. animation-fill-mode: forwards;
  157. @keyframes faded-in {
  158. from {
  159. opacity: 0;
  160. }
  161. to {
  162. opacity: 1;
  163. }
  164. }
  165. `;
  166. const ExpandWrapper = styled.div``;
  167. const ResourceHeader = styled.div`
  168. width: 100%;
  169. height: 50px;
  170. display: flex;
  171. font-size: 13px;
  172. align-items: center;
  173. justify-content: space-between;
  174. color: #ffffff66;
  175. user-select: none;
  176. padding: 8px 18px;
  177. padding-left: ${(props: { expanded: boolean; hasChildren: boolean }) =>
  178. props.hasChildren ? "10px" : "22px"};
  179. cursor: pointer;
  180. background: ${(props: { expanded: boolean; hasChildren: boolean }) =>
  181. props.expanded ? "#ffffff11" : ""};
  182. :hover {
  183. background: #ffffff18;
  184. > i {
  185. background: #ffffff22;
  186. }
  187. }
  188. `;
  189. const Info = styled.div`
  190. display: flex;
  191. flex-direction: row;
  192. align-items: center;
  193. width: 80%;
  194. height: 100%;
  195. `;
  196. const Metadata = styled.div`
  197. display: flex;
  198. align-items: center;
  199. position: relative;
  200. max-width: ${(props: { hasStatus: boolean }) =>
  201. props.hasStatus ? "calc(100% - 20px)" : "100%"};
  202. `;
  203. const Status = styled.div`
  204. display: flex;
  205. width; 20%;
  206. font-size: 12px;
  207. text-transform: capitalize;
  208. justify-content: flex-end;
  209. align-items: center;
  210. font-family: 'Work Sans', sans-serif;
  211. color: #aaaabb;
  212. animation: fadeIn 0.5s;
  213. @keyframes fadeIn {
  214. from { opacity: 0 }
  215. to { opacity: 1 }
  216. }
  217. `;
  218. const StatusColor = styled.div`
  219. margin-left: 12px;
  220. width: 8px;
  221. min-width: 8px;
  222. height: 8px;
  223. background: ${(props: { status: string }) =>
  224. props.status === "running" ||
  225. props.status === "Ready" ||
  226. props.status === "Completed"
  227. ? "#4797ff"
  228. : props.status === "failed" || props.status === "FailedValidation"
  229. ? "#ed5f85"
  230. : props.status === "completed"
  231. ? "#00d12a"
  232. : "#f5cb42"};
  233. border-radius: 20px;
  234. `;
  235. const ResourceName = styled.div`
  236. color: #ffffff;
  237. max-width: 40%;
  238. margin-left: ${(props: { showKindLabels: boolean }) =>
  239. props.showKindLabels ? "10px" : ""};
  240. text-transform: none;
  241. white-space: nowrap;
  242. overflow: hidden;
  243. text-overflow: ellipsis;
  244. `;
  245. const IconWrapper = styled.div`
  246. width: 25px;
  247. height: 25px;
  248. display: flex;
  249. align-items: center;
  250. justify-content: center;
  251. > i {
  252. font-size: 15px;
  253. color: #ffffff;
  254. margin-right: 14px;
  255. }
  256. `;
  257. const DropdownIcon = styled.div`
  258. > i {
  259. margin-top: 2px;
  260. margin-right: 11px;
  261. font-size: 20px;
  262. color: #ffffff66;
  263. cursor: pointer;
  264. border-radius: 20px;
  265. background: ${(props: { expanded: boolean }) =>
  266. props.expanded ? "#ffffff18" : ""};
  267. transform: ${(props: { expanded: boolean }) =>
  268. props.expanded ? "rotate(180deg)" : ""};
  269. animation: ${(props: { expanded: boolean }) =>
  270. props.expanded ? "quarterTurn 0.3s" : ""};
  271. animation-fill-mode: forwards;
  272. @keyframes quarterTurn {
  273. from {
  274. transform: rotate(0deg);
  275. }
  276. to {
  277. transform: rotate(90deg);
  278. }
  279. }
  280. }
  281. `;