mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Restructure to monorepo (#6907)
* wip: restructure to monorepo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor create-release-pr to a package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * github workflow fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix typo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * add webpack-env types to core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix github workflows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor/fix integration tests Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * lint fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * yarn run dev Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * eslint settings for vscode Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * move templates to right package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * open-lens build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * integration test fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix nx task dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * use bash shell for unit tests in test workflow Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit for windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix build:app on windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove ELECTRON_BUILDER_EXTRA_ARGS Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * sync src/ from master Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove Makefile from core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
0066ca4ea2
commit
2657df2293
3321 files changed
+28699
-11447
No files matched your search
@@ -0,0 +1,215 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import orderBy from "lodash/orderBy";
|
||||
import { autoBind } from "./utils";
|
||||
import { action, computed, observable, when, makeObservable } from "mobx";
|
||||
|
||||
export interface ItemObject {
|
||||
getId(): string;
|
||||
getName(): string;
|
||||
}
|
||||
|
||||
export abstract class ItemStore<Item extends ItemObject> {
|
||||
protected defaultSorting = (item: Item) => item.getName();
|
||||
|
||||
@observable failedLoading = false;
|
||||
@observable isLoading = false;
|
||||
@observable isLoaded = false;
|
||||
@observable items = observable.array<Item>([], { deep: false });
|
||||
@observable selectedItemsIds = observable.set<string>();
|
||||
|
||||
constructor() {
|
||||
makeObservable(this);
|
||||
autoBind(this);
|
||||
}
|
||||
|
||||
@computed get selectedItems(): Item[] {
|
||||
return this.pickOnlySelected(this.items);
|
||||
}
|
||||
|
||||
public pickOnlySelected(items: Item[]): Item[] {
|
||||
return items.filter(item => this.selectedItemsIds.has(item.getId()));
|
||||
}
|
||||
|
||||
public getItems(): Item[] {
|
||||
return Array.from(this.items);
|
||||
}
|
||||
|
||||
public getTotalCount(): number {
|
||||
return this.items.length;
|
||||
}
|
||||
|
||||
getByName(name: string): Item | undefined {
|
||||
return this.items.find(item => item.getName() === name);
|
||||
}
|
||||
|
||||
getIndexById(id: string): number {
|
||||
return this.items.findIndex(item => item.getId() === id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return `items` sorted by the given ordering functions. If two elements of
|
||||
* `items` are sorted to the same "index" then the next sorting function is used
|
||||
* to determine where to place them relative to each other. Once the `sorting`
|
||||
* functions have been all exhausted then the order is unchanged (ie a stable sort).
|
||||
* @param items the items to be sorted (default: the current items in this store)
|
||||
* @param sorting list of functions to determine sort order (default: sorting by name)
|
||||
* @param order whether to sort from least to greatest (`"asc"` (default)) or vice-versa (`"desc"`)
|
||||
*/
|
||||
@action
|
||||
protected sortItems(items: Item[] = this.items, sorting: ((item: Item) => any)[] = [this.defaultSorting], order?: "asc" | "desc"): Item[] {
|
||||
return orderBy(items, sorting, order);
|
||||
}
|
||||
|
||||
protected async createItem(...args: any[]): Promise<any>;
|
||||
@action
|
||||
protected async createItem(request: () => Promise<Item>) {
|
||||
const newItem = await request();
|
||||
const item = this.items.find(item => item.getId() === newItem.getId());
|
||||
|
||||
if (item) {
|
||||
return item;
|
||||
} else {
|
||||
const items = this.sortItems([...this.items, newItem]);
|
||||
|
||||
this.items.replace(items);
|
||||
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
protected async loadItems(...args: any[]): Promise<any>;
|
||||
/**
|
||||
* Load items to this.items
|
||||
* @param request Function to return the items to be loaded.
|
||||
* @param sortItems If true, items will be sorted.
|
||||
* @param concurrency If true, concurrent loadItems() calls will all be executed. If false, only the first.
|
||||
* @returns
|
||||
*/
|
||||
@action
|
||||
protected async loadItems(request: () => Promise<Item[] | any>, sortItems = true, concurrency = false) {
|
||||
if (this.isLoading) {
|
||||
await when(() => !this.isLoading);
|
||||
|
||||
// If concurrency for loading is disabled, return instead of loading
|
||||
if (!concurrency) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.isLoading = true;
|
||||
|
||||
try {
|
||||
let items = await request();
|
||||
|
||||
if (sortItems) items = this.sortItems(items);
|
||||
this.items.replace(items);
|
||||
this.isLoaded = true;
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
protected async loadItem(request: () => Promise<Item>, sortItems = true) {
|
||||
const item = await Promise.resolve(request()).catch(() => null);
|
||||
|
||||
if (item) {
|
||||
const existingItem = this.items.find(el => el.getId() === item.getId());
|
||||
|
||||
if (existingItem) {
|
||||
const index = this.items.findIndex(item => item === existingItem);
|
||||
|
||||
this.items.splice(index, 1, item);
|
||||
} else {
|
||||
let items = [...this.items, item];
|
||||
|
||||
if (sortItems) items = this.sortItems(items);
|
||||
this.items.replace(items);
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
@action
|
||||
protected async updateItem(item: Item, request: () => Promise<Item>) {
|
||||
const updatedItem = await request();
|
||||
const index = this.items.findIndex(i => i.getId() === item.getId());
|
||||
|
||||
this.items.splice(index, 1, updatedItem);
|
||||
|
||||
return updatedItem;
|
||||
}
|
||||
|
||||
@action
|
||||
protected async removeItem(item: Item, request: () => Promise<any>) {
|
||||
await request();
|
||||
this.items.remove(item);
|
||||
this.selectedItemsIds.delete(item.getId());
|
||||
}
|
||||
|
||||
isSelected(item: Item) {
|
||||
return this.selectedItemsIds.has(item.getId());
|
||||
}
|
||||
|
||||
@action
|
||||
select(item: Item) {
|
||||
this.selectedItemsIds.add(item.getId());
|
||||
}
|
||||
|
||||
@action
|
||||
unselect(item: Item) {
|
||||
this.selectedItemsIds.delete(item.getId());
|
||||
}
|
||||
|
||||
@action
|
||||
toggleSelection(item: Item) {
|
||||
if (this.isSelected(item)) {
|
||||
this.unselect(item);
|
||||
} else {
|
||||
this.select(item);
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
toggleSelectionAll(visibleItems: Item[] = this.items) {
|
||||
const allSelected = visibleItems.every(this.isSelected);
|
||||
|
||||
if (allSelected) {
|
||||
visibleItems.forEach(this.unselect);
|
||||
} else {
|
||||
visibleItems.forEach(this.select);
|
||||
}
|
||||
}
|
||||
|
||||
isSelectedAll(visibleItems: Item[] = this.items) {
|
||||
if (!visibleItems.length) return false;
|
||||
|
||||
return visibleItems.every(this.isSelected);
|
||||
}
|
||||
|
||||
@action
|
||||
resetSelection() {
|
||||
this.selectedItemsIds.clear();
|
||||
}
|
||||
|
||||
@action
|
||||
reset() {
|
||||
this.resetSelection();
|
||||
this.items.clear();
|
||||
this.selectedItemsIds.clear();
|
||||
this.isLoaded = false;
|
||||
this.isLoading = false;
|
||||
}
|
||||
|
||||
async removeSelectedItems?(): Promise<any>;
|
||||
|
||||
async removeItems?(items: Item[]): Promise<void>;
|
||||
|
||||
* [Symbol.iterator]() {
|
||||
yield* this.items;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user