The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
197 lines
6.1 KiB
TypeScript
197 lines
6.1 KiB
TypeScript
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
import {
|
|
lazy as reactLazy,
|
|
type LazyExoticComponent,
|
|
type ComponentType,
|
|
type FC,
|
|
} from "react";
|
|
|
|
const DEFAULT_STORAGE_KEY = "reactLazyReloadedFunctions";
|
|
|
|
class ReloadStorage {
|
|
public readonly storageKey: string;
|
|
|
|
constructor(storageKey = DEFAULT_STORAGE_KEY) {
|
|
this.storageKey = storageKey;
|
|
}
|
|
|
|
getMap(): Map<string, number> {
|
|
const stored = sessionStorage.getItem(this.storageKey);
|
|
|
|
try {
|
|
const parsed: unknown = stored ? JSON.parse(stored) : [];
|
|
return new Map(
|
|
Array.isArray(parsed)
|
|
? parsed.filter(
|
|
(value) =>
|
|
Array.isArray(value) &&
|
|
typeof value[0] === "string" &&
|
|
typeof value[1] === "number",
|
|
)
|
|
: [],
|
|
);
|
|
} catch (error) {
|
|
console.error("Error parsing react-lazy storage:", error);
|
|
return new Map();
|
|
}
|
|
}
|
|
|
|
addFunction(functionName: string): void {
|
|
const stored = this.getMap();
|
|
stored.set(functionName, (stored.get(functionName) ?? 0) + 1);
|
|
this.save(stored);
|
|
}
|
|
|
|
removeFunction(functionName: string): void {
|
|
const stored = this.getMap();
|
|
stored.delete(functionName);
|
|
this.save(stored);
|
|
}
|
|
|
|
protected save(stored: Map<string, number>): void {
|
|
sessionStorage.setItem(
|
|
this.storageKey,
|
|
JSON.stringify([...stored.entries()]),
|
|
);
|
|
}
|
|
}
|
|
|
|
type ForceReloadConfig = {
|
|
maxRetries: number;
|
|
storageKey?: string;
|
|
};
|
|
|
|
type LazyConfigInit = {
|
|
forceReload?: false | Partial<ForceReloadConfig>;
|
|
importRetries?: number;
|
|
retryDelay?: number;
|
|
onImportError?: (error: unknown) => void;
|
|
};
|
|
|
|
type LazyConfig = {
|
|
forceReload: ForceReloadConfig;
|
|
importRetries: number;
|
|
retryDelay: number;
|
|
onImportError?: (error: unknown) => void;
|
|
};
|
|
|
|
const createConfig = (init: LazyConfigInit = {}): LazyConfig => ({
|
|
forceReload: {
|
|
maxRetries: 1,
|
|
storageKey: DEFAULT_STORAGE_KEY,
|
|
...(typeof init.forceReload === "object"
|
|
? init.forceReload
|
|
: init.forceReload === false
|
|
? { maxRetries: 0 }
|
|
: {}),
|
|
},
|
|
importRetries:
|
|
typeof init.importRetries === "number"
|
|
? Math.max(0, init.importRetries)
|
|
: 0,
|
|
retryDelay:
|
|
typeof init.retryDelay === "number"
|
|
? Math.max(0, init.retryDelay)
|
|
: 300,
|
|
onImportError: init.onImportError,
|
|
});
|
|
|
|
export function createLazy<P = {}>(config: LazyConfigInit = {}) {
|
|
const { forceReload, importRetries, retryDelay, onImportError } =
|
|
createConfig(config);
|
|
const reloadStorage = new ReloadStorage(forceReload.storageKey);
|
|
|
|
function enhancedLazy<T = P>(
|
|
importFunction: () => Promise<{ default: ComponentType<T> }>,
|
|
): LazyExoticComponent<ComponentType<T>> {
|
|
const functionString = importFunction.toString();
|
|
|
|
const importWithRetry = async () => {
|
|
let retryCount = 0;
|
|
|
|
const tryImport = async (): Promise<{
|
|
default: ComponentType<T>;
|
|
}> => {
|
|
try {
|
|
return await importFunction();
|
|
} catch (error) {
|
|
if (onImportError) {
|
|
onImportError(error);
|
|
} else {
|
|
console.error("React-lazy import error:", error);
|
|
}
|
|
|
|
if (retryCount < importRetries) {
|
|
retryCount++;
|
|
|
|
if (retryDelay > 0) {
|
|
await new Promise((resolve) =>
|
|
setTimeout(resolve, retryDelay),
|
|
);
|
|
}
|
|
|
|
return tryImport();
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
return tryImport();
|
|
};
|
|
|
|
return reactLazy(async () => {
|
|
try {
|
|
const component = await importWithRetry();
|
|
if (forceReload.maxRetries > 0) {
|
|
reloadStorage.removeFunction(functionString);
|
|
}
|
|
return component;
|
|
} catch (error) {
|
|
if (
|
|
forceReload.maxRetries > 0 &&
|
|
(reloadStorage.getMap().get(functionString) ?? 0) <
|
|
forceReload.maxRetries
|
|
) {
|
|
reloadStorage.addFunction(functionString);
|
|
window.location.reload();
|
|
|
|
const EmptyComponent: FC<T> = () => null;
|
|
return { default: EmptyComponent };
|
|
}
|
|
throw error;
|
|
}
|
|
});
|
|
}
|
|
|
|
return enhancedLazy;
|
|
}
|
|
|
|
export const lazy = createLazy({
|
|
forceReload: {
|
|
maxRetries: 3,
|
|
storageKey: DEFAULT_STORAGE_KEY,
|
|
},
|
|
importRetries: 3,
|
|
retryDelay: 300,
|
|
});
|