/** * Copyright (c) 2021 OpenLens Authors * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import styles from "./helm-charts.module.scss"; import React from "react"; import { action, computed, observable, makeObservable } from "mobx"; import { HelmRepo, HelmRepoManager } from "../../../main/helm/helm-repo-manager"; import { Button } from "../button"; import { Icon } from "../icon"; import { Notifications } from "../notifications"; import { Select, SelectOption } from "../select"; import { AddHelmRepoDialog } from "./add-helm-repo-dialog"; import { observer } from "mobx-react"; import { RemovableItem } from "./removable-item"; import { Notice } from "../+extensions/notice"; import { Spinner } from "../spinner"; @observer export class HelmCharts extends React.Component { @observable loading = false; @observable repos: HelmRepo[] = []; @observable addedRepos = observable.map(); constructor(props: {}) { super(props); makeObservable(this); } @computed get options(): SelectOption[] { return this.repos.map(repo => ({ label: repo.name, value: repo, })); } async componentDidMount() { await this.loadRepos(); } @action async loadRepos() { this.loading = true; try { if (!this.repos.length) { this.repos = await HelmRepoManager.loadAvailableRepos(); } const repos = await HelmRepoManager.getInstance().repositories(); // via helm-cli this.addedRepos.clear(); repos.forEach(repo => this.addedRepos.set(repo.name, repo)); } catch (err) { Notifications.error(String(err)); } this.loading = false; } async addRepo(repo: HelmRepo) { try { await HelmRepoManager.addRepo(repo); this.addedRepos.set(repo.name, repo); } catch (err) { Notifications.error(<>Adding helm branch {repo.name} has failed: {String(err)}); } } async removeRepo(repo: HelmRepo) { try { await HelmRepoManager.removeRepo(repo); this.addedRepos.delete(repo.name); } catch (err) { Notifications.error( <>Removing helm branch {repo.name} has failed: {String(err)}, ); } } onRepoSelect = async ({ value: repo }: SelectOption) => { const isAdded = this.addedRepos.has(repo.name); if (isAdded) { Notifications.ok(<>Helm branch {repo.name} already in use); return; } this.loading = true; await this.addRepo(repo); this.loading = false; }; formatOptionLabel = ({ value: repo }: SelectOption) => { const isAdded = this.addedRepos.has(repo.name); return (
{repo.name} {isAdded && }
); }; renderRepositories() { const repos = Array.from(this.addedRepos); if (this.loading) { return
; } if (!repos.length) { return (
The repositories have not been added yet
); } return repos.map(([name, repo]) => { return ( this.removeRepo(repo)} className="mt-3">
{name}
{repo.url}
); }); } render() { return (