/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./helm-chart-details.scss"; import React, { Component } from "react"; import type { HelmChart } from "../../../common/k8s-api/endpoints/helm-charts.api"; import { computed, observable, reaction, runInAction } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import { Drawer, DrawerItem } from "../drawer"; import { autoBind, stopPropagation } from "../../utils"; import { MarkdownViewer } from "../markdown-viewer"; import { Spinner } from "../spinner"; import { Button } from "../button"; import { Select } from "../select"; import { Badge } from "../badge"; import { Tooltip, withStyles } from "@material-ui/core"; import { withInjectables } from "@ogre-tools/injectable-react"; import createInstallChartTabInjectable from "../dock/install-chart/create-install-chart-tab.injectable"; import type { ShowCheckedErrorNotification } from "../notifications/show-checked-error.injectable"; import type { SingleValue } from "react-select"; import AbortController from "abort-controller"; import showCheckedErrorNotificationInjectable from "../notifications/show-checked-error.injectable"; import type { GetChartDetails } from "./get-char-details.injectable"; import getChartDetailsInjectable from "./get-char-details.injectable"; import { HelmChartIcon } from "./icon"; export interface HelmChartDetailsProps { chart: HelmChart; hideDetails(): void; } const LargeTooltip = withStyles({ tooltip: { fontSize: "var(--font-size-small)", }, })(Tooltip); interface Dependencies { createInstallChartTab: (helmChart: HelmChart) => void; showCheckedErrorNotification: ShowCheckedErrorNotification; getChartDetails: GetChartDetails; } @observer class NonInjectedHelmChartDetails extends Component { readonly chartVersions = observable.array(); readonly selectedChart = observable.box(); readonly readme = observable.box(undefined); readonly chartVerionOptions = computed(() => ( this.chartVersions.map(chart => ({ value: chart, label: chart.version, })) )); private abortController = new AbortController(); constructor(props: HelmChartDetailsProps & Dependencies) { super(props); autoBind(this); } componentWillUnmount() { this.abortController.abort(); } componentDidMount() { disposeOnUnmount(this, [ reaction(() => this.props.chart, async ({ name, repo, version }) => { runInAction(() => { this.selectedChart.set(undefined); this.chartVersions.clear(); this.readme.set(""); }); try { const { readme, versions } = await this.props.getChartDetails(repo, name, { version }); runInAction(() => { this.readme.set(readme); this.chartVersions.replace(versions); this.selectedChart.set(versions[0]); }); } catch (error) { this.props.showCheckedErrorNotification(error, "Unknown error occured while getting chart details"); } }, { fireImmediately: true, }), ]); } async onVersionChange(option: SingleValue<{ value: HelmChart }>) { const chart = option?.value ?? this.chartVersions[0]; runInAction(() => { this.selectedChart.set(chart ?? undefined); this.readme.set(undefined); }); try { this.abortController.abort(); this.abortController = new AbortController(); const { chart: { name, repo }} = this.props; const { readme } = await this.props.getChartDetails(repo, name, { version: chart.version, reqInit: { signal: this.abortController.signal }}); this.readme.set(readme); } catch (error) { this.props.showCheckedErrorNotification(error, "Unknown error occured while getting chart details"); } } install(selectedChart: HelmChart) { this.props.createInstallChartTab(selectedChart); this.props.hideDetails(); } renderIntroduction(selectedChart: HelmChart) { return (
{selectedChart.getDescription()}