mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Store current template value for each tab
Signed-off-by: Pavel Ashevskii <pashevskii@mirantis.com>
This commit is contained in:
parent
997c38ba06
commit
c6b9c40fd4
@ -1,3 +1,4 @@
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
import groupBy from "lodash/groupBy";
|
||||
@ -14,6 +15,7 @@ export class CreateResourceStore extends DockTabStore<string> {
|
||||
super({
|
||||
storageKey: "create_resource"
|
||||
});
|
||||
fs.ensureDirSync(this.userTemplatesFolder);
|
||||
}
|
||||
|
||||
get lensTemplatesFolder():string {
|
||||
@ -24,12 +26,8 @@ export class CreateResourceStore extends DockTabStore<string> {
|
||||
return path.join(os.homedir(), ".k8slens", "templates");
|
||||
}
|
||||
|
||||
get lensTemplates() {
|
||||
return this.getTemplates(this.lensTemplatesFolder, "lens");
|
||||
}
|
||||
|
||||
getTemplates(templatesPath: string, defaultGroup: string) {
|
||||
const templates = filehound.create().path(templatesPath).ext("yaml").depth(1).findSync();
|
||||
async getTemplates(templatesPath: string, defaultGroup: string) {
|
||||
const templates = await filehound.create().path(templatesPath).ext(["yaml", "json"]).depth(1).find();
|
||||
|
||||
return templates ? this.groupTemplates(templates, templatesPath, defaultGroup) : {};
|
||||
}
|
||||
@ -42,7 +40,10 @@ export class CreateResourceStore extends DockTabStore<string> {
|
||||
}
|
||||
|
||||
async getMergedTemplates() {
|
||||
return {...this.getTemplates(this.userTemplatesFolder, "ungrouped"),...this.lensTemplates};
|
||||
const userTemplates = await this.getTemplates(this.userTemplatesFolder, "ungrouped");
|
||||
const lensTemplates = await this.getTemplates(this.lensTemplatesFolder, "lens");
|
||||
|
||||
return {...userTemplates,...lensTemplates};
|
||||
}
|
||||
|
||||
async watchUserTemplates(callback: ()=> void){
|
||||
|
||||
@ -23,20 +23,18 @@ interface Props {
|
||||
|
||||
@observer
|
||||
export class CreateResource extends React.Component<Props> {
|
||||
@observable currentTemplates:Map<string,SelectOption> = new Map();
|
||||
@observable error = "";
|
||||
@observable templates:GroupSelectOption<SelectOption>[] = [];
|
||||
|
||||
componentDidMount() {
|
||||
createResourceStore.getMergedTemplates().then(v => this.updateGroupSelecOptions(v));
|
||||
createResourceStore.watchUserTemplates(() => createResourceStore.getMergedTemplates().then(v => this.updateGroupSelecOptions(v)));
|
||||
createResourceStore.getMergedTemplates().then(v => this.updateGroupSelectOptions(v));
|
||||
createResourceStore.watchUserTemplates(() => createResourceStore.getMergedTemplates().then(v => this.updateGroupSelectOptions(v)));
|
||||
}
|
||||
|
||||
updateGroupSelecOptions(templates :{[x:string]: string[]}) {
|
||||
this.templates = [];
|
||||
|
||||
Object.keys(templates).forEach(group => {
|
||||
this.templates.push(this.convertToGroup(group, templates[group]));
|
||||
});
|
||||
updateGroupSelectOptions(templates :Record<string, string[]>) {
|
||||
this.templates = Object.entries(templates)
|
||||
.map(([name, grouping]) => this.convertToGroup(name, grouping));
|
||||
}
|
||||
|
||||
convertToGroup(group:string, items:string[]):GroupSelectOption {
|
||||
@ -53,12 +51,17 @@ export class CreateResource extends React.Component<Props> {
|
||||
return createResourceStore.getData(this.tabId);
|
||||
}
|
||||
|
||||
get currentTemplate() {
|
||||
return this.currentTemplates.get(this.tabId) ?? null;
|
||||
}
|
||||
|
||||
onChange = (value: string, error?: string) => {
|
||||
createResourceStore.setData(this.tabId, value);
|
||||
this.error = error;
|
||||
};
|
||||
|
||||
onSelectTemplate = (item: SelectOption) => {
|
||||
this.currentTemplates.set(this.tabId, item);
|
||||
fs.readFile(item.value,"utf8").then(v => createResourceStore.setData(this.tabId,v));
|
||||
};
|
||||
|
||||
@ -105,6 +108,7 @@ export class CreateResource extends React.Component<Props> {
|
||||
menuPlacement="top"
|
||||
themeName="outlined"
|
||||
onChange={v => this.onSelectTemplate(v)}
|
||||
value = {this.currentTemplate}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user