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 path from "path";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
import groupBy from "lodash/groupBy";
|
import groupBy from "lodash/groupBy";
|
||||||
@ -14,6 +15,7 @@ export class CreateResourceStore extends DockTabStore<string> {
|
|||||||
super({
|
super({
|
||||||
storageKey: "create_resource"
|
storageKey: "create_resource"
|
||||||
});
|
});
|
||||||
|
fs.ensureDirSync(this.userTemplatesFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
get lensTemplatesFolder():string {
|
get lensTemplatesFolder():string {
|
||||||
@ -24,12 +26,8 @@ export class CreateResourceStore extends DockTabStore<string> {
|
|||||||
return path.join(os.homedir(), ".k8slens", "templates");
|
return path.join(os.homedir(), ".k8slens", "templates");
|
||||||
}
|
}
|
||||||
|
|
||||||
get lensTemplates() {
|
async getTemplates(templatesPath: string, defaultGroup: string) {
|
||||||
return this.getTemplates(this.lensTemplatesFolder, "lens");
|
const templates = await filehound.create().path(templatesPath).ext(["yaml", "json"]).depth(1).find();
|
||||||
}
|
|
||||||
|
|
||||||
getTemplates(templatesPath: string, defaultGroup: string) {
|
|
||||||
const templates = filehound.create().path(templatesPath).ext("yaml").depth(1).findSync();
|
|
||||||
|
|
||||||
return templates ? this.groupTemplates(templates, templatesPath, defaultGroup) : {};
|
return templates ? this.groupTemplates(templates, templatesPath, defaultGroup) : {};
|
||||||
}
|
}
|
||||||
@ -42,7 +40,10 @@ export class CreateResourceStore extends DockTabStore<string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getMergedTemplates() {
|
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){
|
async watchUserTemplates(callback: ()=> void){
|
||||||
|
|||||||
@ -23,20 +23,18 @@ interface Props {
|
|||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class CreateResource extends React.Component<Props> {
|
export class CreateResource extends React.Component<Props> {
|
||||||
|
@observable currentTemplates:Map<string,SelectOption> = new Map();
|
||||||
@observable error = "";
|
@observable error = "";
|
||||||
@observable templates:GroupSelectOption<SelectOption>[] = [];
|
@observable templates:GroupSelectOption<SelectOption>[] = [];
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
createResourceStore.getMergedTemplates().then(v => this.updateGroupSelecOptions(v));
|
createResourceStore.getMergedTemplates().then(v => this.updateGroupSelectOptions(v));
|
||||||
createResourceStore.watchUserTemplates(() => createResourceStore.getMergedTemplates().then(v => this.updateGroupSelecOptions(v)));
|
createResourceStore.watchUserTemplates(() => createResourceStore.getMergedTemplates().then(v => this.updateGroupSelectOptions(v)));
|
||||||
}
|
}
|
||||||
|
|
||||||
updateGroupSelecOptions(templates :{[x:string]: string[]}) {
|
updateGroupSelectOptions(templates :Record<string, string[]>) {
|
||||||
this.templates = [];
|
this.templates = Object.entries(templates)
|
||||||
|
.map(([name, grouping]) => this.convertToGroup(name, grouping));
|
||||||
Object.keys(templates).forEach(group => {
|
|
||||||
this.templates.push(this.convertToGroup(group, templates[group]));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
convertToGroup(group:string, items:string[]):GroupSelectOption {
|
convertToGroup(group:string, items:string[]):GroupSelectOption {
|
||||||
@ -53,12 +51,17 @@ export class CreateResource extends React.Component<Props> {
|
|||||||
return createResourceStore.getData(this.tabId);
|
return createResourceStore.getData(this.tabId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get currentTemplate() {
|
||||||
|
return this.currentTemplates.get(this.tabId) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
onChange = (value: string, error?: string) => {
|
onChange = (value: string, error?: string) => {
|
||||||
createResourceStore.setData(this.tabId, value);
|
createResourceStore.setData(this.tabId, value);
|
||||||
this.error = error;
|
this.error = error;
|
||||||
};
|
};
|
||||||
|
|
||||||
onSelectTemplate = (item: SelectOption) => {
|
onSelectTemplate = (item: SelectOption) => {
|
||||||
|
this.currentTemplates.set(this.tabId, item);
|
||||||
fs.readFile(item.value,"utf8").then(v => createResourceStore.setData(this.tabId,v));
|
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"
|
menuPlacement="top"
|
||||||
themeName="outlined"
|
themeName="outlined"
|
||||||
onChange={v => this.onSelectTemplate(v)}
|
onChange={v => this.onSelectTemplate(v)}
|
||||||
|
value = {this.currentTemplate}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user