/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import fse from "fs-extra"; import { computed, makeObservable, observable, reaction } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import React from "react"; import { Notice } from "../+extensions/notice"; import { KubeconfigSyncEntry, KubeconfigSyncValue, UserStore } from "../../../common/user-store"; import { isWindows } from "../../../common/vars"; import logger from "../../../main/logger"; import { iter, multiSet } from "../../utils"; import { SubTitle } from "../layout/sub-title"; import { PathPicker } from "../path-picker/path-picker"; import { Spinner } from "../spinner"; import { RemovableItem } from "./removable-item"; interface SyncInfo { type: "file" | "folder" | "unknown"; } interface Entry extends Value { filePath: string; } interface Value { data: KubeconfigSyncValue; info: SyncInfo; } async function getMapEntry({ filePath, ...data }: KubeconfigSyncEntry): Promise<[string, Value]> { try { // stat follows the stat(2) linux syscall spec, namely it follows symlinks const stats = await fse.stat(filePath); if (stats.isFile()) { return [filePath, { info: { type: "file" }, data }]; } if (stats.isDirectory()) { return [filePath, { info: { type: "folder" }, data }]; } logger.warn("[KubeconfigSyncs]: unknown stat entry", { stats }); return [filePath, { info: { type: "unknown" }, data }]; } catch (error) { logger.warn(`[KubeconfigSyncs]: failed to stat entry: ${error}`, { error }); return [filePath, { info: { type: "unknown" }, data }]; } } export async function getAllEntries(filePaths: string[]): Promise<[string, Value][]> { return Promise.all(filePaths.map(filePath => getMapEntry({ filePath }))); } @observer export class KubeconfigSyncs extends React.Component { syncs = observable.map(); @observable loaded = false; constructor(props: {}) { super(props); makeObservable(this); } async componentDidMount() { const mapEntries = await Promise.all( iter.map( UserStore.getInstance().syncKubeconfigEntries, ([filePath, ...value]) => getMapEntry({ filePath, ...value }), ), ); this.syncs.replace(mapEntries); this.loaded = true; disposeOnUnmount(this, [ reaction(() => Array.from(this.syncs.entries(), ([filePath, { data }]) => [filePath, data]), syncs => { UserStore.getInstance().syncKubeconfigEntries.replace(syncs); }), ]); } @computed get syncsList(): Entry[] | undefined { if (!this.loaded) { return undefined; } return Array.from(this.syncs.entries(), ([filePath, value]) => ({ filePath, ...value })); } onPick = async (filePaths: string[]) => multiSet(this.syncs, await getAllEntries(filePaths)); getIconName(entry: Entry) { switch (entry.info.type) { case "file": return "description"; case "folder": return "folder"; case "unknown": return "help_outline"; } } renderEntry = (entry: Entry) => { return ( this.syncs.delete(entry.filePath)} className="mt-3" icon={this.getIconName(entry)} >
{entry.filePath}
); }; renderEntries() { const entries = this.syncsList; if (!entries) { return (
); } if (!entries.length) { return (
No files and folders have been synced yet
); } return (
{entries.map(this.renderEntry)}
); } renderSyncButtons() { if (isWindows) { return (
or
); } return (
); } render() { return ( <> {this.renderSyncButtons()} {this.renderEntries()} ); } }