1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix issues

Signed-off-by: Pavel Ashevskiy <pavel.ashevskiy@ifellow.ru>
This commit is contained in:
Pavel Ashevskiy 2021-04-07 16:39:26 +04:00 committed by Pavel Ashevskii
parent 30ce887273
commit 997c38ba06
2 changed files with 21 additions and 16 deletions

View File

@ -31,17 +31,21 @@ export class CreateResourceStore extends DockTabStore<string> {
getTemplates(templatesPath: string, defaultGroup: string) { getTemplates(templatesPath: string, defaultGroup: string) {
const templates = filehound.create().path(templatesPath).ext("yaml").depth(1).findSync(); const templates = filehound.create().path(templatesPath).ext("yaml").depth(1).findSync();
return templates ? (groupBy(templates,(v:string) => path.relative(templatesPath,v).split(path.sep).length>1 return templates ? this.groupTemplates(templates, templatesPath, defaultGroup) : {};
? path.parse(path.relative(templatesPath,v)).dir }
: defaultGroup)) : {};
groupTemplates(templates: string[], templatesPath: string, defaultGroup: string) {
return groupBy(templates,(v:string) =>
path.relative(templatesPath,v).split(path.sep).length>1
? path.parse(path.relative(templatesPath,v)).dir
: defaultGroup);
} }
async getMergedTemplates() { async getMergedTemplates() {
return {...this.getTemplates(this.userTemplatesFolder, "ungrouped"),...this.lensTemplates}; return {...this.getTemplates(this.userTemplatesFolder, "ungrouped"),...this.lensTemplates};
} }
async watchUserTemplates(calback: ()=> void){ async watchUserTemplates(callback: ()=> void){
calback();
watch(this.userTemplatesFolder, { watch(this.userTemplatesFolder, {
depth: 1, depth: 1,
ignoreInitial: true, ignoreInitial: true,
@ -49,7 +53,7 @@ export class CreateResourceStore extends DockTabStore<string> {
stabilityThreshold: 500 stabilityThreshold: 500
} }
}).on("all", () => { }).on("all", () => {
calback(); callback();
}); });
} }
} }

View File

@ -26,19 +26,20 @@ export class CreateResource extends React.Component<Props> {
@observable error = ""; @observable error = "";
@observable templates:GroupSelectOption<SelectOption>[] = []; @observable templates:GroupSelectOption<SelectOption>[] = [];
async componentDidMount(){ componentDidMount() {
createResourceStore.watchUserTemplates(()=> { createResourceStore.getMergedTemplates().then(v => this.updateGroupSelecOptions(v));
this.templates = []; createResourceStore.watchUserTemplates(() => createResourceStore.getMergedTemplates().then(v => this.updateGroupSelecOptions(v)));
createResourceStore.getMergedTemplates().then( }
templatesDictionary => {
Object.keys(templatesDictionary).forEach(group => { updateGroupSelecOptions(templates :{[x:string]: string[]}) {
this.templates.push(this.convertEntryToGroup(group, templatesDictionary[group])); this.templates = [];
});
}); Object.keys(templates).forEach(group => {
this.templates.push(this.convertToGroup(group, templates[group]));
}); });
} }
convertEntryToGroup(group:string, items:string[]):GroupSelectOption { convertToGroup(group:string, items:string[]):GroupSelectOption {
const options = items.map(v => ({label: path.parse(v).name, value: v})); const options = items.map(v => ({label: path.parse(v).name, value: v}));
return {label: group, options}; return {label: group, options};