ResourceTab.tsx 6.6 KB

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