|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. Devtron Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + Button, |
| 19 | + ButtonStyleType, |
| 20 | + ButtonVariantType, |
| 21 | + ComponentSizeType, |
| 22 | + handleAnalyticsEvent, |
| 23 | + Icon, |
| 24 | + ImageWithFallback, |
| 25 | + noop, |
| 26 | + stopPropagation, |
| 27 | + useMainContext, |
| 28 | +} from '@devtron-labs/devtron-fe-common-lib' |
| 29 | + |
| 30 | +import { ReactComponent as ICCaretSmall } from '@Icons/ic-caret-left-small.svg' |
| 31 | +import { InteractiveCellText } from '@Components/common/helpers/InteractiveCellText/InteractiveCellText' |
| 32 | + |
| 33 | +import { ReactComponent as Helm } from '../../assets/icons/ic-default-chart.svg' |
| 34 | +import { SERVER_MODE } from '../../config' |
| 35 | +import { ChartSelectProps } from './charts.types' |
| 36 | +import { renderDeprecatedWarning } from './charts.util' |
| 37 | + |
| 38 | +const ChartCard = ({ |
| 39 | + chart, |
| 40 | + addChart, |
| 41 | + subtractChart, |
| 42 | + selectedCount = 0, |
| 43 | + onClick, |
| 44 | + dataTestId, |
| 45 | + isListView, |
| 46 | +}: ChartSelectProps) => { |
| 47 | + const { serverMode } = useMainContext() |
| 48 | + |
| 49 | + const addChartTab = (e): void => { |
| 50 | + stopPropagation(e) |
| 51 | + addChart(chart.id) |
| 52 | + handleAnalyticsEvent({ category: 'Chart store add icon', action: 'CS_BULK_DEPLOY_ADD_CHART' }) |
| 53 | + } |
| 54 | + |
| 55 | + const removeChartTab = (e): void => { |
| 56 | + stopPropagation(e) |
| 57 | + subtractChart(chart.id) |
| 58 | + handleAnalyticsEvent({ category: 'Chart store remove icon', action: 'CS_BULK_DEPLOY_REMOVE_CHART' }) |
| 59 | + } |
| 60 | + |
| 61 | + const onClickChartSelect = (): void => { |
| 62 | + onClick(chart.id) |
| 63 | + handleAnalyticsEvent({ category: 'Chart store card', action: 'CS_CHART_CARD_CLICKED' }) |
| 64 | + } |
| 65 | + |
| 66 | + const renderAddIcon = () => ( |
| 67 | + <div className={`${selectedCount > 0 ? 'dc__visible' : 'dc__border br-6'} dc__visible-hover--child`}> |
| 68 | + <Button |
| 69 | + icon={<Icon name="ic-add" size={null} color={null} />} |
| 70 | + onClick={addChartTab} |
| 71 | + dataTestId={`chart-add-${dataTestId}`} |
| 72 | + variant={ButtonVariantType.borderLess} |
| 73 | + size={ComponentSizeType.small} |
| 74 | + style={ButtonStyleType.default} |
| 75 | + ariaLabel="Add chart to selection" |
| 76 | + /> |
| 77 | + </div> |
| 78 | + ) |
| 79 | + |
| 80 | + const renderRemoveIcon = () => ( |
| 81 | + <Button |
| 82 | + icon={<Icon name="ic-minus" size={null} color={null} />} |
| 83 | + onClick={removeChartTab} |
| 84 | + dataTestId={`chart-remove-${dataTestId}`} |
| 85 | + variant={ButtonVariantType.borderLess} |
| 86 | + size={ComponentSizeType.small} |
| 87 | + style={ButtonStyleType.negativeGrey} |
| 88 | + showAriaLabelInTippy={selectedCount > 0} |
| 89 | + ariaLabel="Remove chart from selection" |
| 90 | + /> |
| 91 | + ) |
| 92 | + const chartIconClass = 'dc__chart-grid-item__icon chart-icon-dim br-4 dc__no-shrink' |
| 93 | + |
| 94 | + const renderIcon = () => ( |
| 95 | + <div className="icon-wrapper"> |
| 96 | + <ImageWithFallback |
| 97 | + imageProps={{ |
| 98 | + height: 32, |
| 99 | + width: 32, |
| 100 | + src: chart.icon, |
| 101 | + alt: 'chart', |
| 102 | + className: chartIconClass, |
| 103 | + }} |
| 104 | + fallbackImage={<Helm className={chartIconClass} />} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + ) |
| 108 | + |
| 109 | + const getDescriptionTruncate = () => { |
| 110 | + if (isListView) { |
| 111 | + return 'dc__truncate--clamp-4' |
| 112 | + } |
| 113 | + |
| 114 | + if (chart.deprecated) return 'dc__truncate' |
| 115 | + return 'dc__truncate--clamp-2' |
| 116 | + } |
| 117 | + |
| 118 | + const renderCardInfo = () => ( |
| 119 | + <div className="flexbox-col flex-grow-1 dc__gap-8"> |
| 120 | + <div className="flexbox-col dc__gap-2"> |
| 121 | + <div className="flex left"> |
| 122 | + <InteractiveCellText |
| 123 | + text={chart.name} |
| 124 | + rootClassName="fw-6 chart-grid-item__title cn-9" |
| 125 | + fontSize={14} |
| 126 | + /> |
| 127 | + <div className="chart-name__arrow dc__no-shrink flex"> |
| 128 | + <ICCaretSmall className="icon-dim-16 dc__flip-180 scb-5" /> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + {chart.deprecated && renderDeprecatedWarning()} |
| 132 | + </div> |
| 133 | + |
| 134 | + <span className={`fw-4 fs-13 lh-20 ${getDescriptionTruncate()}`}> |
| 135 | + {chart.description || 'No description'} |
| 136 | + </span> |
| 137 | + </div> |
| 138 | + ) |
| 139 | + |
| 140 | + const renderFooter = () => ( |
| 141 | + <div className="flex left dc__content-space dc__border-top-n1 px-20 py-16 dc__gap-6"> |
| 142 | + <div className="flex dc__gap-6"> |
| 143 | + <Icon name="ic-folder-color" size={16} color={null} /> |
| 144 | + <InteractiveCellText |
| 145 | + text={chart.chart_name ? chart.chart_name : chart.docker_artifact_store_id} |
| 146 | + rootClassName="cn-7" |
| 147 | + fontSize={12} |
| 148 | + /> |
| 149 | + </div> |
| 150 | + <div className="fs-12 cn-7 dc__ellipsis-right__2nd-line lh-18 dc__no-shrink">v{chart.version}</div> |
| 151 | + </div> |
| 152 | + ) |
| 153 | + return ( |
| 154 | + <div |
| 155 | + key={chart.id} |
| 156 | + className={`chart-grid-item dc__visible-hover dc__visible-hover--parent bg__primary border__secondary-translucent cursor dc__position-rel br-8 ${isListView ? 'flexbox-col' : ''} ${selectedCount > 0 ? 'chart-grid-item--selected' : ''} `} |
| 157 | + onClick={onClick ? onClickChartSelect : noop} |
| 158 | + data-testid={`chart-card-${dataTestId}`} |
| 159 | + > |
| 160 | + <div |
| 161 | + className={`${isListView ? 'dc__grid chart-list-item dc__gap-16' : 'flexbox-col h-166 dc__gap-12'} px-20 pt-20 pb-16`} |
| 162 | + > |
| 163 | + {renderIcon()} |
| 164 | + {serverMode === SERVER_MODE.FULL && addChart && subtractChart && ( |
| 165 | + <div |
| 166 | + className={`devtron-stepper ${selectedCount > 0 ? 'dc__grid devtron-stepper-grid dc__border br-6 fw-6 cursor bg__primary' : ''}`} |
| 167 | + > |
| 168 | + {selectedCount > 0 && ( |
| 169 | + <> |
| 170 | + {renderRemoveIcon()} |
| 171 | + |
| 172 | + <span className="devtron-stepper__item fs-13 icon-dim-20 px-4">{selectedCount}</span> |
| 173 | + </> |
| 174 | + )} |
| 175 | + |
| 176 | + {renderAddIcon()} |
| 177 | + </div> |
| 178 | + )} |
| 179 | + {renderCardInfo()} |
| 180 | + </div> |
| 181 | + {renderFooter()} |
| 182 | + </div> |
| 183 | + ) |
| 184 | +} |
| 185 | + |
| 186 | +export default ChartCard |
0 commit comments