|
| 1 | +import { useEffect, useMemo, useState } from 'react' |
| 2 | +import { Route, Switch, useRouteMatch } from 'react-router-dom' |
| 3 | + |
| 4 | +import { |
| 5 | + APIResponseHandler, |
| 6 | + BreadCrumb, |
| 7 | + handleAnalyticsEvent, |
| 8 | + PageHeader, |
| 9 | + SegmentedControl, |
| 10 | + SegmentedControlProps, |
| 11 | + SelectPickerProps, |
| 12 | + useAsync, |
| 13 | + useBreadcrumb, |
| 14 | + useUrlFilters, |
| 15 | +} from '@devtron-labs/devtron-fe-common-lib' |
| 16 | + |
| 17 | +import { ChartSelector } from '@Components/AppSelector' |
| 18 | +import { getChartSelectAPI } from '@Components/charts/charts.util' |
| 19 | +import { URLS } from '@Config/routes' |
| 20 | + |
| 21 | +import { ChartDetailsAbout } from './ChartDetailsAbout' |
| 22 | +import { ChartDetailsDeploy } from './ChartDetailsDeploy' |
| 23 | +import { ChartDetailsDeployments } from './ChartDetailsDeployments' |
| 24 | +import { ChartDetailsPresetValues } from './ChartDetailsPresetValues' |
| 25 | +import { ChartDetailsReadme } from './ChartDetailsReadme' |
| 26 | +import { CHART_DETAILS_PORTAL_CONTAINER_ID, CHART_DETAILS_SEGMENTS } from './constants' |
| 27 | +import { fetchChartDetails, fetchChartVersions } from './services' |
| 28 | +import { ChartDetailsRouteParams, ChartDetailsSearchParams, ChartDetailsSegment } from './types' |
| 29 | +import { chartSelectorFilterOption, chartSelectorFormatOptionLabel, parseChartDetailsSearchParams } from './utils' |
| 30 | + |
| 31 | +import './chartDetails.scss' |
| 32 | + |
| 33 | +export const ChartDetails = () => { |
| 34 | + // STATES |
| 35 | + const [selectedChartVersion, setSelectedChartVersion] = useState<number>(null) |
| 36 | + |
| 37 | + // HOOKS |
| 38 | + const { |
| 39 | + path, |
| 40 | + params: { chartId }, |
| 41 | + } = useRouteMatch<ChartDetailsRouteParams>() |
| 42 | + |
| 43 | + const { tab, updateSearchParams } = useUrlFilters<void, ChartDetailsSearchParams>({ |
| 44 | + parseSearchParams: parseChartDetailsSearchParams, |
| 45 | + }) |
| 46 | + |
| 47 | + // ASYNC CALLS |
| 48 | + const [isFetchingChartVersions, chartVersions, chartVersionsErr, reloadChartVersions] = useAsync( |
| 49 | + () => fetchChartVersions(chartId), |
| 50 | + [chartId], |
| 51 | + ) |
| 52 | + |
| 53 | + const [isFetchingChartDetails, chartDetails, chartDetailsErr, reloadChartDetails] = useAsync( |
| 54 | + () => fetchChartDetails(selectedChartVersion), |
| 55 | + [selectedChartVersion], |
| 56 | + !!selectedChartVersion, |
| 57 | + { resetOnChange: false }, |
| 58 | + ) |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + if (!isFetchingChartVersions && chartVersions?.length) { |
| 62 | + setSelectedChartVersion(chartVersions[0].id) |
| 63 | + } |
| 64 | + }, [isFetchingChartVersions, chartVersions]) |
| 65 | + |
| 66 | + // CONFIGS |
| 67 | + const { breadcrumbs } = useBreadcrumb( |
| 68 | + { |
| 69 | + alias: { |
| 70 | + ':chartSegment?': null, |
| 71 | + ':chartId': { |
| 72 | + component: ( |
| 73 | + <ChartSelector |
| 74 | + primaryKey="chartId" |
| 75 | + primaryValue="name" |
| 76 | + api={getChartSelectAPI} |
| 77 | + matchedKeys={[]} |
| 78 | + apiPrimaryKey="id" |
| 79 | + formatOptionLabel={chartSelectorFormatOptionLabel} |
| 80 | + filterOption={chartSelectorFilterOption} |
| 81 | + /> |
| 82 | + ), |
| 83 | + linked: false, |
| 84 | + }, |
| 85 | + chart: null, |
| 86 | + 'chart-store': null, |
| 87 | + }, |
| 88 | + }, |
| 89 | + [chartId], |
| 90 | + ) |
| 91 | + |
| 92 | + const chartOptions = useMemo( |
| 93 | + () => |
| 94 | + (chartVersions ?? []).map(({ id, version }) => ({ |
| 95 | + label: version.startsWith('v') ? version : `v${version}`, |
| 96 | + value: id, |
| 97 | + })), |
| 98 | + [JSON.stringify(chartVersions)], |
| 99 | + ) |
| 100 | + |
| 101 | + // CONSTANTS |
| 102 | + const isChartDetailsLoading = isFetchingChartDetails || !chartDetails |
| 103 | + |
| 104 | + // HANDLERS |
| 105 | + const handleSegmentChange: SegmentedControlProps['onChange'] = (selectedSegment) => { |
| 106 | + const updatedTab = selectedSegment.value as ChartDetailsSegment |
| 107 | + |
| 108 | + if (updatedTab === ChartDetailsSegment.PRESET_VALUES) { |
| 109 | + handleAnalyticsEvent({ category: 'Chart Store', action: 'CS_CHART_PRESET_VALUES' }) |
| 110 | + } |
| 111 | + |
| 112 | + updateSearchParams({ tab: updatedTab }) |
| 113 | + } |
| 114 | + |
| 115 | + const handleChartChange: SelectPickerProps<number>['onChange'] = ({ value }) => { |
| 116 | + setSelectedChartVersion(value) |
| 117 | + } |
| 118 | + |
| 119 | + // RENDERERS |
| 120 | + const renderBreadcrumbs = () => <BreadCrumb breadcrumbs={breadcrumbs} /> |
| 121 | + |
| 122 | + const renderSegments = () => { |
| 123 | + switch (tab) { |
| 124 | + case ChartDetailsSegment.README: |
| 125 | + return ( |
| 126 | + <ChartDetailsReadme |
| 127 | + chartName={chartDetails?.name} |
| 128 | + readme={chartDetails?.readme} |
| 129 | + chartsOptions={chartOptions} |
| 130 | + selectedChartVersion={selectedChartVersion} |
| 131 | + onChartChange={handleChartChange} |
| 132 | + isLoading={isChartDetailsLoading} |
| 133 | + error={chartDetailsErr} |
| 134 | + reload={reloadChartDetails} |
| 135 | + /> |
| 136 | + ) |
| 137 | + case ChartDetailsSegment.PRESET_VALUES: |
| 138 | + return <ChartDetailsPresetValues /> |
| 139 | + case ChartDetailsSegment.DEPLOYMENTS: |
| 140 | + return <ChartDetailsDeployments chartIcon={chartDetails?.icon} /> |
| 141 | + default: |
| 142 | + return null |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return ( |
| 147 | + <div className="flex-grow-1 flexbox-col bg__secondary dc__overflow-hidden"> |
| 148 | + <PageHeader isBreadcrumbs breadCrumbs={renderBreadcrumbs} /> |
| 149 | + |
| 150 | + <APIResponseHandler |
| 151 | + isLoading={isFetchingChartVersions} |
| 152 | + progressingProps={{ pageLoader: true }} |
| 153 | + error={chartVersionsErr} |
| 154 | + errorScreenManagerProps={{ code: chartVersionsErr?.code, reload: reloadChartVersions }} |
| 155 | + > |
| 156 | + <Switch> |
| 157 | + <Route path={`${path}${URLS.DEPLOY_CHART}/:presetValueId?`}> |
| 158 | + <ChartDetailsDeploy |
| 159 | + chartDetails={chartDetails} |
| 160 | + chartVersions={chartVersions} |
| 161 | + selectedChartVersion={selectedChartVersion} |
| 162 | + /> |
| 163 | + </Route> |
| 164 | + <Route> |
| 165 | + <div className="chart-details flex-grow-1 p-20 dc__overflow-auto"> |
| 166 | + <div className="flexbox-col dc__gap-16 mw-none"> |
| 167 | + <div id={CHART_DETAILS_PORTAL_CONTAINER_ID} className="flex dc__content-space"> |
| 168 | + <SegmentedControl |
| 169 | + name="chart-details-segmented-control" |
| 170 | + segments={CHART_DETAILS_SEGMENTS} |
| 171 | + value={tab} |
| 172 | + onChange={handleSegmentChange} |
| 173 | + /> |
| 174 | + </div> |
| 175 | + {renderSegments()} |
| 176 | + </div> |
| 177 | + <ChartDetailsAbout isLoading={isChartDetailsLoading} chartDetails={chartDetails} /> |
| 178 | + </div> |
| 179 | + </Route> |
| 180 | + </Switch> |
| 181 | + </APIResponseHandler> |
| 182 | + </div> |
| 183 | + ) |
| 184 | +} |
0 commit comments