ResourceTab.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 (
  50. <Tooltip>{x}</Tooltip>
  51. );
  52. }
  53. }
  54. getStatusText = () => {
  55. let { status } = this.props;
  56. if (status.available && status.total) {
  57. return `${status.available}/${status.total}`;
  58. } else if (status.label) {
  59. return status.label;
  60. }
  61. }
  62. renderStatus = () => {
  63. let { status } = this.props;
  64. if (status) {
  65. return (
  66. <Status>
  67. {this.getStatusText()}
  68. <StatusColor status={status.label} />
  69. </Status>
  70. );
  71. }
  72. }
  73. renderExpanded = () => {
  74. if (this.props.children && this.state.expanded) {
  75. return (
  76. <ExpandWrapper>
  77. {this.props.children}
  78. </ExpandWrapper>
  79. );
  80. }
  81. }
  82. render() {
  83. let {
  84. label,
  85. name,
  86. children,
  87. isLast,
  88. handleClick,
  89. selected,
  90. status,
  91. roundAllCorners,
  92. } = this.props;
  93. return (
  94. <StyledResourceTab
  95. isLast={isLast}
  96. onClick={() => handleClick && handleClick()}
  97. roundAllCorners={roundAllCorners}
  98. >
  99. <ResourceHeader
  100. hasChildren={children && true}
  101. expanded={this.state.expanded || selected}
  102. onClick={() => {
  103. if (children) {
  104. this.setState({ expanded: !this.state.expanded });
  105. }
  106. }}
  107. >
  108. <Info>
  109. {this.renderDropdownIcon()}
  110. <Metadata hasStatus={status && true}>
  111. {this.renderIcon(label)}
  112. {label}
  113. <ResourceName
  114. showKindLabels={true}
  115. onMouseOver={() => { this.setState({ showTooltip: true }) }}
  116. onMouseOut={() => { this.setState({ showTooltip: false }) }}
  117. >
  118. {name}
  119. </ResourceName>
  120. {this.renderTooltip(name)}
  121. </Metadata>
  122. </Info>
  123. {this.renderStatus()}
  124. </ResourceHeader>
  125. {this.renderExpanded()}
  126. </StyledResourceTab>
  127. );
  128. }
  129. }
  130. const StyledResourceTab = styled.div`
  131. width: 100%;
  132. margin-bottom: 2px;
  133. background: #ffffff11;
  134. border-bottom-left-radius: ${(props: { isLast: boolean, roundAllCorners: boolean }) => props.isLast ? '5px' : ''};
  135. border-bottom-right-radius: ${(props: { isLast: boolean, roundAllCorners: boolean }) => props.roundAllCorners && props.isLast ? '5px' : ''};
  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 { opacity: 0 }
  159. to { opacity: 1 }
  160. }
  161. `;
  162. const ExpandWrapper = styled.div`
  163. overflow: hidden;
  164. `;
  165. const ResourceHeader = styled.div`
  166. width: 100%;
  167. height: 50px;
  168. display: flex;
  169. font-size: 13px;
  170. align-items: center;
  171. justify-content: space-between;
  172. color: #ffffff66;
  173. user-select: none;
  174. padding: 8px 18px;
  175. padding-left: ${(props: { expanded: boolean, hasChildren: boolean }) => props.hasChildren ? '10px' : '22px'};
  176. cursor: pointer;
  177. background: ${(props: { expanded: boolean, hasChildren: boolean }) => props.expanded ? '#ffffff11' : ''};
  178. :hover {
  179. background: #ffffff18;
  180. > i {
  181. background: #ffffff22;
  182. }
  183. }
  184. `;
  185. const Info = styled.div`
  186. display: flex;
  187. flex-direction: row;
  188. align-items: center;
  189. width: 80%;
  190. height: 100%;
  191. `;
  192. const Metadata = styled.div`
  193. display: flex;
  194. align-items: center;
  195. position: relative;
  196. max-width: ${(props: { hasStatus: boolean }) => props.hasStatus ? 'calc(100% - 50px)' : '100%'};
  197. `;
  198. const Status = styled.div`
  199. display: flex;
  200. width; 20%;
  201. font-size: 12px;
  202. text-transform: capitalize;
  203. justify-content: flex-end;
  204. align-items: center;
  205. font-family: 'Work Sans', sans-serif;
  206. color: #aaaabb;
  207. animation: fadeIn 0.5s;
  208. @keyframes fadeIn {
  209. from { opacity: 0 }
  210. to { opacity: 1 }
  211. }
  212. `;
  213. const StatusColor = styled.div`
  214. margin-left: 12px;
  215. width: 8px;
  216. min-width: 8px;
  217. height: 8px;
  218. background: ${(props: { status: string }) => (props.status === 'running' || props.status === 'Ready' || props.status === 'Completed' ? '#4797ff' : props.status === 'failed' || props.status === 'FailedValidation' ? "#ed5f85" : "#f5cb42")};
  219. border-radius: 20px;
  220. `;
  221. const ResourceName = styled.div`
  222. color: #ffffff;
  223. margin-left: ${(props: { showKindLabels: boolean }) => props.showKindLabels ? '10px' : ''};
  224. text-transform: none;
  225. white-space: nowrap;
  226. overflow: hidden;
  227. text-overflow: ellipsis;
  228. `;
  229. const IconWrapper = styled.div`
  230. width: 25px;
  231. height: 25px;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. > i {
  236. font-size: 15px;
  237. color: #ffffff;
  238. margin-right: 14px;
  239. }
  240. `;
  241. const DropdownIcon = styled.div`
  242. > i {
  243. margin-top: 2px;
  244. margin-right: 11px;
  245. font-size: 20px;
  246. color: #ffffff66;
  247. cursor: pointer;
  248. border-radius: 20px;
  249. background: ${(props: { expanded: boolean }) => props.expanded ? '#ffffff18' : ''};
  250. transform: ${(props: { expanded: boolean }) => props.expanded ? 'rotate(180deg)' : ''};
  251. animation: ${(props: { expanded: boolean }) => props.expanded ? 'quarterTurn 0.3s' : ''};
  252. animation-fill-mode: forwards;
  253. @keyframes quarterTurn {
  254. from { transform: rotate(0deg) }
  255. to { transform: rotate(90deg) }
  256. }
  257. }
  258. `;