mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* 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>
120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
// Manage observable param from document's location.search
|
|
import { action, makeObservable } from "mobx";
|
|
import type { ObservableHistory } from "mobx-observable-history";
|
|
|
|
export interface PageParamInit<Value = any> {
|
|
name: string;
|
|
defaultValue?: Value; // multi-values param must be defined with array-value, e.g. []
|
|
parse?(value: string | string[]): Value; // from URL
|
|
stringify?(value: Value): string | string[]; // to URL
|
|
}
|
|
|
|
export interface PageParamDependencies {
|
|
readonly history: ObservableHistory<unknown>;
|
|
}
|
|
|
|
// TODO: write tests
|
|
export class PageParam<Value = any> {
|
|
readonly name: string;
|
|
readonly isMulti: boolean;
|
|
|
|
constructor(protected readonly dependencies: PageParamDependencies, private init: PageParamInit<Value>) {
|
|
makeObservable(this);
|
|
const { name, defaultValue } = init;
|
|
|
|
this.name = name;
|
|
this.isMulti = Array.isArray(defaultValue); // multi-values param
|
|
}
|
|
|
|
// should be a getter since `init.defaultValue` could be a getter too
|
|
get defaultValue(): Value | undefined {
|
|
return this.init.defaultValue;
|
|
}
|
|
|
|
parse(values: string | string[]): Value {
|
|
const { parse } = this.init;
|
|
|
|
if (parse) {
|
|
return parse(values);
|
|
}
|
|
|
|
// return as-is ("string"-value based params)
|
|
return values as unknown as Value;
|
|
}
|
|
|
|
stringify(value: Value = this.get()): string[] {
|
|
const { stringify } = this.init;
|
|
|
|
if (stringify) {
|
|
return [stringify(value)].flat();
|
|
}
|
|
|
|
return [value].flat().map(String);
|
|
}
|
|
|
|
get(): Value {
|
|
// TODO: cleanup
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
return this.parse(this.getRaw()) ?? this.defaultValue!;
|
|
}
|
|
|
|
@action
|
|
set(value: Value, { mergeGlobals = true, replaceHistory = false } = {}): void {
|
|
const search = this.toString({ mergeGlobals, value });
|
|
|
|
this.dependencies.history.merge({ search }, replaceHistory);
|
|
}
|
|
|
|
/**
|
|
* Set stringified raw value(s) and update `document.location.search`
|
|
* @param {string | string[]} value
|
|
*/
|
|
@action
|
|
setRaw(value: string | string[]): void {
|
|
const values: string[] = [value].flat();
|
|
|
|
if (this.isMulti) {
|
|
this.clear();
|
|
values.forEach(value => {
|
|
this.dependencies.history.searchParams.append(this.name, value);
|
|
});
|
|
} else {
|
|
this.dependencies.history.searchParams.set(this.name, values[0]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get stringified raw value(s) from `document.location.search`
|
|
*/
|
|
getRaw(): string | string[] {
|
|
const values: string[] = this.dependencies.history.searchParams.getAll(this.name);
|
|
|
|
return this.isMulti ? values : values[0];
|
|
}
|
|
|
|
@action
|
|
clear() {
|
|
this.dependencies.history.searchParams.delete(this.name);
|
|
}
|
|
|
|
toString({ mergeGlobals = true, value = this.get() } = {}): string {
|
|
let searchParams = new URLSearchParams();
|
|
|
|
if (mergeGlobals) {
|
|
searchParams = new URLSearchParams(this.dependencies.history.searchParams);
|
|
searchParams.delete(this.name);
|
|
}
|
|
|
|
this.stringify(value).forEach(value => {
|
|
searchParams.append(this.name, value);
|
|
});
|
|
|
|
return searchParams.toString();
|
|
}
|
|
}
|