mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Turn on strict mode in tsconfig.json, some helpful lints, and required cleanup where strictness necessitates it (#5195)
* Turn on strict mode in tsconfig.json - Add route, clusterRoute, and payloadValidatedClusterRoute helper functions to improve types with backend routes - Turn on the following new lints: - react/jsx-first-prop-new-line - react/jsx-wrap-multilines - react/jsx-one-expression-per-line - react/jsx-max-props-per-line - react/jsx-indent - react/jsx-indent-props Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix build Signed-off-by: Sebastian Malton <sebastian@malton.name> * Replace KubeObject scope strings with enum Signed-off-by: Sebastian Malton <sebastian@malton.name> * Revert package.json version changes Signed-off-by: Sebastian Malton <sebastian@malton.name> * revert move hostedCluster(Id) Signed-off-by: Sebastian Malton <sebastian@malton.name> * change some type param names to be not single letters Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove copy-extension-themes Signed-off-by: Sebastian Malton <sebastian@malton.name> * add new make clean action Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix build to not use webpack for generating types Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix kube-object-menu.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix select.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix catalog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * revert move fileNameMigration to index Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix ref logic error Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix log-resource-selector.test.tsx tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix dock-store.test.ts test by overriding createStorage to not touch file system Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix cluster.test.ts tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix kube=api.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fixed hotbar-store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix kubeconfig-manager.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix cluster-role-bindings/__tests__/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix role-bindings/__tests__/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix pods.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix delete-cluster-dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix daemonset.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix replicaset.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix statefulsets/dialog/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix replicasets/scale-dialog/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix deployments.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix deployments/scale/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix cronjob.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix stateful-set.api.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix deployment.api.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix api-manager.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix statefulset.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix job.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix pods.store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix scroll-spy.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix hotbar-remove-command.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix catalog-entity-registry.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix welcome.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix verify-that-all-routes-have-route-component.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix pod-tolerations.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * better fix for previous 3 fixes, plus also select.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix kube-object-menu.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix app-paths.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix dock-tabs.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix isReactNode typing Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix sub-title.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix drawer.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix list-layout.tsx and header.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix error-boundary.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix upgrade-chart/store.ts and dock-tab.store.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix install-chart/store.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix edit-resource/store.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix create-resource/store.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix namespace-select.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix namespace-select-filter.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix crd-list.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix wrong types for extensions Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix circular dependency Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix circular dependency on catalogCategoryRegistry Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix api-kube Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix type errors, most <Select /> errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * fixing more type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * some more fixing type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * convert all KubeApis to injectable with legacy global backups Signed-off-by: Sebastian Malton <sebastian@malton.name> * factor out into a common file all the exports Signed-off-by: Sebastian Malton <sebastian@malton.name> * convert all KubeObjectStores to injectable with legacy global backups Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix lint Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove unused legacy KubeApi globals Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix bad previous commit Signed-off-by: Sebastian Malton <sebastian@malton.name> * more crash fixing Signed-off-by: Sebastian Malton <sebastian@malton.name> * try and fix behavioural tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix sidebar-and-tab-navigation-for-core.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix sidebar-and-tab-navigation-for-extensions.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-using-application-menu.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix catalog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * Make ThemeStore non-singleton and fix navigation-to-terminal-preferences.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * extensions.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix catalog-entity-registry.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-using-application-menu.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix log-resource-selector.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix dock-tabs.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix delete-cluster-dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-kubernetes-preferences.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-editor-preferences.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-proxy-preferences.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-using-application-menu.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-application-preferences.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix dock-store.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix select.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix role-bindings/__tests__/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix hotbar-remove-command.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix cluster-role-bindings/__tests__/dialog.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-extension-specific-preferences.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-telemetry-preferences.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix closing-preferences.test.tsx Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-editor-preferences.test.ts Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix navigation-to-proxy-preferences.test.ts - Fix other type errors too Signed-off-by: Sebastian Malton <sebastian@malton.name> * final tweaks Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add more tsconfig files, fix bug in <Catalog> - Make all of history, navigation injectable Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Convert all of kube-details-params/ and navigate/ to injectable - This fixes a runtime error that was encountered during testing Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix runtime errors on renderer - remove all static uses of `createPageParam` (and then removed the legacy global) - Made LensRendererExtension and LensMainExtension just used dependencies and not the getLegacyDi - Fixed circular dep in extension-loader Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move registerStore calls to after injectMany Signed-off-by: Sebastian Malton <sebastian@malton.name> * replace all the rest of the legacy uses of apiManager Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix stack overflow and cycles in DI Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix NamespaceSelectFilter not opening Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix WizardStep and AddNamespaceDialog Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix KubeApi's not being registered Signed-off-by: Sebastian Malton <sebastian@malton.name> * cleanup WindowManager Signed-off-by: Sebastian Malton <sebastian@malton.name> * Proper fix for Wizard, fix NamespaceStore.subscribe Signed-off-by: Sebastian Malton <sebastian@malton.name> * Rewrite withTooltip to be more type correct - Fixes mobx related "too many recursive actions" error - Change all the uses of withTooltips to be functional components Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add e2e test to cover kube api registration Signed-off-by: Sebastian Malton <sebastian@malton.name> * cleanup internal-commands Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove cast in <Animate> Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix command-palette e2e test Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type error after rebase Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix test name Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix lint Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix code to help CodeQL scanner Signed-off-by: Sebastian Malton <sebastian@malton.name> * update intree extension lock files Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix build-extensions picking wrong @types/react Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix tests from rebase Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix type error Signed-off-by: Sebastian Malton <sebastian@malton.name> * Make KubeconfigSyncManager more injectable Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix crash in test mode for Dialog Signed-off-by: Sebastian Malton <sebastian@malton.name> * make Select snapshots deterministic Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix new type error Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix kube-object.store.test.ts typing Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix merge build issues Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix snapshots after merge Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix lint after merge Signed-off-by: Sebastian Malton <sebastian@malton.name> * reexport BaseKubeJsonApiObjectMetadata Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix typo in terminalSpawningPool Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove duplicate license header Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix typo to waitUntilDefined Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove iter use from getLegacyGlobalDiForExtensionApi Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove complex createStorage override Signed-off-by: Sebastian Malton <sebastian@malton.name> * override logger with mocks only when needed for tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove specialized overrideStore flags for getDiForUnitTesting Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove unnecessary | undefined types from the exactOptionalFieldTypes experiment Signed-off-by: Sebastian Malton <sebastian@malton.name> * use more descriptive name for local test mocks Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove unnecessary addition to 'make clean' target Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove oddity of KubeObjectStore.getById(undefined) being allowed Signed-off-by: Sebastian Malton <sebastian@malton.name> * rename KubeObject.getDescriptor in favour of name without fundemental JS meaning Signed-off-by: Sebastian Malton <sebastian@malton.name> * Simplify legacyRegisterApi when working in behaviour unit tests - Don't emit within main environment as there should be no auto registering there Signed-off-by: Sebastian Malton <sebastian@malton.name> * change confusing variable name in ReactiveDuration Signed-off-by: Sebastian Malton <sebastian@malton.name> * make visitor pattern more explicit for Entity contextMenuOpen Signed-off-by: Sebastian Malton <sebastian@malton.name> * toggleDetails -> toggleKubeDetailsPane is more specific Signed-off-by: Sebastian Malton <sebastian@malton.name> * remove outdated comment Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix bug where LensExtension dependencies are not set Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix tests from the revert of react 18 Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix more tests from merge Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix typings with new is-compatible-extension tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * more type fixing Signed-off-by: Sebastian Malton <sebastian@malton.name> * Revert in-tree extension versions Signed-off-by: Sebastian Malton <sebastian@malton.name> * Improve name of guarding injectable for stores and apis - New name better implies that it is just a guard state and does not do anything Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add helper for <Select>.isMulti for storing in a Set<Value> Signed-off-by: Sebastian Malton <sebastian@malton.name> * fix is-compatible-extension.test.ts types Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
381d77c633
commit
dfcb7c3330
1062 files changed
+26512
-28689
No files matched your search
@@ -54,7 +54,7 @@ describe("parseJsonPath", () => {
|
||||
expect(res).toBe("$.metadata.labels[0].foo[0]");
|
||||
});
|
||||
|
||||
it("converts ending .. to ..*", () => {
|
||||
it("removes trailing ..", () => {
|
||||
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels[]..");
|
||||
|
||||
expect(res).toBe("$.metadata.labels[0]");
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
import { observable, reaction } from "mobx";
|
||||
import { StorageHelper } from "../storageHelper";
|
||||
import { delay } from "../../../common/utils/delay";
|
||||
import { toJS } from "../../../common/utils";
|
||||
|
||||
interface StorageModel {
|
||||
[prop: string]: any /*json-serializable*/;
|
||||
@@ -103,7 +104,7 @@ describe("renderer/utils/StorageHelper", () => {
|
||||
|
||||
// returning object modifies `draft` as well
|
||||
storageHelper.merge(draft => ({
|
||||
message: draft.message.replace("2", "3"),
|
||||
message: draft.message?.replace("2", "3"),
|
||||
}));
|
||||
expect(storageHelper.get()).toEqual({ ...storageHelper.defaultValue, message: "updated3" });
|
||||
});
|
||||
@@ -131,7 +132,7 @@ describe("renderer/utils/StorageHelper", () => {
|
||||
it("storage.get() is observable", () => {
|
||||
expect(storageHelper.get()).toEqual(defaultValue);
|
||||
|
||||
reaction(() => storageHelper.toJSON(), change => {
|
||||
reaction(() => toJS(storageHelper.get()), change => {
|
||||
observedChanges.push(change);
|
||||
});
|
||||
|
||||
|
||||
@@ -10,28 +10,24 @@ import writeJsonFileInjectable from "../../../common/fs/write-json-file.injectab
|
||||
import { observable } from "mobx";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
import getAbsolutePathInjectable from "../../../common/path/get-absolute-path.injectable";
|
||||
import hostedClusterIdInjectable from "../../../common/cluster-store/hosted-cluster-id.injectable";
|
||||
|
||||
const createStorageInjectable = getInjectable({
|
||||
id: "create-storage",
|
||||
|
||||
instantiate: (di) =>
|
||||
createStorage({
|
||||
storage: observable({
|
||||
initialized: false,
|
||||
loaded: false,
|
||||
data: {} as Record<string /*key*/, any>, // json-serializable
|
||||
}),
|
||||
|
||||
readJsonFile: di.inject(readJsonFileInjectable),
|
||||
writeJsonFile: di.inject(writeJsonFileInjectable),
|
||||
logger: di.inject(loggerInjectable),
|
||||
|
||||
directoryForLensLocalStorage: di.inject(
|
||||
directoryForLensLocalStorageInjectable,
|
||||
),
|
||||
|
||||
getAbsolutePath: di.inject(getAbsolutePathInjectable),
|
||||
instantiate: (di) => createStorage({
|
||||
storage: observable({
|
||||
initialized: false,
|
||||
loaded: false,
|
||||
data: {},
|
||||
}),
|
||||
readJsonFile: di.inject(readJsonFileInjectable),
|
||||
writeJsonFile: di.inject(writeJsonFileInjectable),
|
||||
logger: di.inject(loggerInjectable),
|
||||
directoryForLensLocalStorage: di.inject(directoryForLensLocalStorageInjectable),
|
||||
getAbsolutePath: di.inject(getAbsolutePathInjectable),
|
||||
hostedClusterId: di.inject(hostedClusterIdInjectable),
|
||||
}),
|
||||
});
|
||||
|
||||
export default createStorageInjectable;
|
||||
@@ -6,10 +6,9 @@
|
||||
// Keeps window.localStorage state in external JSON-files.
|
||||
// Because app creates random port between restarts => storage session wiped out each time.
|
||||
import { comparer, reaction, toJS, when } from "mobx";
|
||||
import type { StorageLayer } from "../storageHelper";
|
||||
import { StorageHelper } from "../storageHelper";
|
||||
import { isTestEnv } from "../../../common/vars";
|
||||
|
||||
import { getHostedClusterId } from "../../../common/utils";
|
||||
import type { JsonObject, JsonValue } from "type-fest";
|
||||
import type { Logger } from "../../../common/logger";
|
||||
import type { GetAbsolutePath } from "../../../common/path/get-absolute-path.injectable";
|
||||
@@ -21,28 +20,36 @@ interface Dependencies {
|
||||
readJsonFile: (filePath: string) => Promise<JsonValue>;
|
||||
writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise<void>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
hostedClusterId: string | undefined;
|
||||
}
|
||||
|
||||
export type CreateStorage = <T>(key: string, defaultValue: T) => StorageLayer<T>;
|
||||
|
||||
/**
|
||||
* Creates a helper for saving data under the "key" intended for window.localStorage
|
||||
*/
|
||||
export const createStorage = ({ storage, getAbsolutePath, logger, directoryForLensLocalStorage, readJsonFile, writeJsonFile }: Dependencies) => <T>(key: string, defaultValue: T) => {
|
||||
export const createStorage = ({
|
||||
storage,
|
||||
getAbsolutePath,
|
||||
logger,
|
||||
directoryForLensLocalStorage,
|
||||
readJsonFile,
|
||||
writeJsonFile,
|
||||
hostedClusterId,
|
||||
}: Dependencies): CreateStorage => (key, defaultValue) => {
|
||||
const { logPrefix } = StorageHelper;
|
||||
|
||||
if (!storage.initialized) {
|
||||
storage.initialized = true;
|
||||
|
||||
(async () => {
|
||||
const filePath = getAbsolutePath(directoryForLensLocalStorage, `${getHostedClusterId() || "app"}.json`);
|
||||
const filePath = getAbsolutePath(directoryForLensLocalStorage, `${hostedClusterId || "app"}.json`);
|
||||
|
||||
try {
|
||||
storage.data = (await readJsonFile(filePath)) as JsonObject;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-empty
|
||||
catch {}
|
||||
|
||||
finally {
|
||||
} catch {
|
||||
// do nothing
|
||||
} finally {
|
||||
if (!isTestEnv) {
|
||||
logger.info(`${logPrefix} loading finished for ${filePath}`);
|
||||
}
|
||||
@@ -71,7 +78,7 @@ export const createStorage = ({ storage, getAbsolutePath, logger, directoryForLe
|
||||
.catch(error => logger.error(`${logPrefix} Failed to initialize storage: ${error}`));
|
||||
}
|
||||
|
||||
return new StorageHelper<T>(key, {
|
||||
return new StorageHelper(key, {
|
||||
autoInit: true,
|
||||
defaultValue,
|
||||
storage: {
|
||||
|
||||
@@ -3,25 +3,32 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
// Helper for combining css classes inside components
|
||||
import { iter } from "../../common/utils";
|
||||
|
||||
export type IClassName = string | string[] | IClassNameMap;
|
||||
export type IClassNameMap = Record<string, any>;
|
||||
export type IgnoredClassNames = number | symbol | Function;
|
||||
|
||||
export function cssNames(...args: IClassName[]): string {
|
||||
const map: IClassNameMap = {};
|
||||
export type IClassName = string | string[] | IClassNameMap | undefined | null | false | IgnoredClassNames;
|
||||
export type IClassNameMap = object;
|
||||
|
||||
args.forEach(className => {
|
||||
if (typeof className === "string" || Array.isArray(className)) {
|
||||
[].concat(className).forEach(name => map[name] = true);
|
||||
export function cssNames(...classNames: IClassName[]): string {
|
||||
const classNamesEnabled = new Map<string, boolean>();
|
||||
|
||||
for (const className of classNames) {
|
||||
if (typeof className === "string") {
|
||||
classNamesEnabled.set(className, true);
|
||||
} else if (Array.isArray(className)) {
|
||||
for (const name of className) {
|
||||
classNamesEnabled.set(name, true);
|
||||
}
|
||||
} else if (className && typeof className === "object") {
|
||||
for (const [name, value] of Object.entries(className)) {
|
||||
classNamesEnabled.set(name, Boolean(value));
|
||||
}
|
||||
}
|
||||
else {
|
||||
Object.assign(map, className);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Object.entries(map)
|
||||
return iter.pipeline(classNamesEnabled.entries())
|
||||
.filter(([, isActive]) => !!isActive)
|
||||
.map(([className]) => className.trim())
|
||||
.filterMap(([className]) => className.trim())
|
||||
.join(" ");
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type React from "react";
|
||||
|
||||
export function displayBooleans(shouldShow: boolean, from: React.ReactNode): React.ReactNode {
|
||||
export function displayBooleans(shouldShow: boolean | undefined, from: React.ReactNode): React.ReactNode {
|
||||
if (shouldShow) {
|
||||
if (typeof from === "boolean") {
|
||||
return from.toString();
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type React from "react";
|
||||
|
||||
/**
|
||||
* Returns `true` if `node` is a falsy value
|
||||
*/
|
||||
export function isNodeFalsy(node: React.ReactNode): boolean {
|
||||
return !isNodeRenderable(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return `true` if React would render this
|
||||
*/
|
||||
export function isNodeRenderable(node: React.ReactNode): boolean {
|
||||
return Boolean(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first react node provided that is would be rendered by react
|
||||
*/
|
||||
export function foldNodes(...nodes: React.ReactNode[]): React.ReactNode {
|
||||
return nodes.find(isNodeRenderable);
|
||||
}
|
||||
@@ -6,9 +6,10 @@
|
||||
// Type guard for checking valid react node to use in render
|
||||
import type { ReactNode } from "react";
|
||||
import React from "react";
|
||||
import { isObject } from "../../common/utils";
|
||||
|
||||
export function isReactNode(node: ReactNode): node is ReactNode {
|
||||
return React.isValidElement(node)
|
||||
export function isReactNode(node: unknown): node is ReactNode {
|
||||
return (isObject(node) && React.isValidElement(node))
|
||||
|| Array.isArray(node) && node.every(isReactNode)
|
||||
|| node == null
|
||||
|| typeof node !== "object";
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
*/
|
||||
|
||||
import { JSONPath } from "@astronautlabs/jsonpath";
|
||||
import { TypedRegEx } from "typed-regex";
|
||||
|
||||
const slashDashSearch = /[\\-]/g;
|
||||
const pathByBareDots = /(?<=\w)\./;
|
||||
const textBeforeFirstSquare = /^.*(?=\[)/g;
|
||||
const backSlash = /\\/g;
|
||||
const kubectlOptionPrefix = /^\$?\.?(?<pathExpression>.*)/;
|
||||
const kubectlOptionPrefix = TypedRegEx("^\\$?\\.?(?<pathExpression>.*)");
|
||||
const sliceVersion = /\[]/g;
|
||||
const tripleDotName = /\.\.\.(?<trailing>.)/g;
|
||||
const trailingDotDot = /\.\.$/;
|
||||
@@ -28,14 +29,14 @@ const trailingDotDot = /\.\.$/;
|
||||
* - Allow `...foo` as well as `..foo`
|
||||
*/
|
||||
export function convertKubectlJsonPathToNodeJsonPath(jsonPath: string) {
|
||||
const startMatch = jsonPath.match(kubectlOptionPrefix);
|
||||
const captures = kubectlOptionPrefix.captures(jsonPath);
|
||||
let start = "$";
|
||||
|
||||
if (!startMatch) {
|
||||
if (!captures) {
|
||||
return start;
|
||||
}
|
||||
|
||||
let { pathExpression } = startMatch.groups;
|
||||
let { pathExpression } = captures;
|
||||
|
||||
if (pathExpression.match(slashDashSearch)) {
|
||||
const [first, ...rest] = pathExpression.split(pathByBareDots);
|
||||
|
||||
@@ -4,18 +4,24 @@
|
||||
*/
|
||||
|
||||
// Helper for working with storages (e.g. window.localStorage, NodeJS/file-system, etc.)
|
||||
import { action, comparer, computed, makeObservable, observable, toJS, when } from "mobx";
|
||||
import { action, comparer, computed, makeObservable, observable, observe, toJS, when } from "mobx";
|
||||
import type { Draft } from "immer";
|
||||
import { produce, isDraft } from "immer";
|
||||
import { isEqual, isPlainObject } from "lodash";
|
||||
import logger from "../../main/logger";
|
||||
|
||||
export interface StorageChange<T> {
|
||||
key: string;
|
||||
value: T | undefined;
|
||||
oldValue: T | undefined;
|
||||
}
|
||||
|
||||
export interface StorageAdapter<T> {
|
||||
[metadata: string]: any;
|
||||
[metadata: string]: unknown;
|
||||
getItem(key: string): T | Promise<T>;
|
||||
setItem(key: string, value: T): void;
|
||||
removeItem(key: string): void;
|
||||
onChange?(change: { key: string; value: T; oldValue?: T }): void;
|
||||
onChange?(change: StorageChange<T>): void;
|
||||
}
|
||||
|
||||
export interface StorageHelperOptions<T> {
|
||||
@@ -24,11 +30,21 @@ export interface StorageHelperOptions<T> {
|
||||
defaultValue: T;
|
||||
}
|
||||
|
||||
export class StorageHelper<T> {
|
||||
export interface StorageLayer<T> {
|
||||
isDefaultValue(val: T): boolean;
|
||||
get(): T;
|
||||
readonly value: T;
|
||||
readonly whenReady: Promise<void>;
|
||||
set(value: T): void;
|
||||
reset(): void;
|
||||
merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)): void;
|
||||
}
|
||||
|
||||
export class StorageHelper<T> implements StorageLayer<T> {
|
||||
static logPrefix = "[StorageHelper]:";
|
||||
readonly storage: StorageAdapter<T>;
|
||||
|
||||
private data = observable.box<T>(undefined, {
|
||||
private data = observable.box<T | undefined>(undefined, {
|
||||
deep: true,
|
||||
equals: comparer.structural,
|
||||
});
|
||||
@@ -51,9 +67,8 @@ export class StorageHelper<T> {
|
||||
|
||||
this.storage = storage;
|
||||
|
||||
// TODO: This code uses undocumented MobX internal to criminally permit exotic mutations without encapsulation.
|
||||
this.data.observe_(({ newValue, oldValue }) => {
|
||||
this.onChange(newValue as T, oldValue as T);
|
||||
observe(this.data, (change) => {
|
||||
this.onChange(change.newValue as T | undefined, change.oldValue as T | undefined);
|
||||
});
|
||||
|
||||
if (autoInit) {
|
||||
@@ -99,7 +114,7 @@ export class StorageHelper<T> {
|
||||
return isEqual(value, this.defaultValue);
|
||||
}
|
||||
|
||||
protected onChange(value: T, oldValue?: T) {
|
||||
protected onChange(value: T | undefined, oldValue: T | undefined) {
|
||||
if (!this.initialized) return;
|
||||
|
||||
try {
|
||||
@@ -115,9 +130,6 @@ export class StorageHelper<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Switch to using value for being reactive
|
||||
*/
|
||||
get(): T {
|
||||
return this.value;
|
||||
}
|
||||
@@ -143,7 +155,7 @@ export class StorageHelper<T> {
|
||||
|
||||
@action
|
||||
merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)) {
|
||||
const nextValue = produce<T>(this.toJSON(), (draft: Draft<T>) => {
|
||||
const nextValue = produce<T>(toJS(this.get()), (draft) => {
|
||||
|
||||
if (typeof value == "function") {
|
||||
const newValue = value(draft);
|
||||
@@ -162,8 +174,4 @@ export class StorageHelper<T> {
|
||||
|
||||
this.set(nextValue);
|
||||
}
|
||||
|
||||
toJSON(): T {
|
||||
return toJS(this.get());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user