Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for handling of missing/removed modules #310

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/framework/Module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Module<StateType extends StateBaseType> {
private _defaultTitle: string;
public viewFC: ModuleFC<StateType>;
public settingsFC: ModuleFC<StateType>;
private _importState: ImportState;
protected _importState: ImportState;
private _moduleInstances: ModuleInstance<StateType>[];
private _defaultState: StateType | null;
private _stateOptions: StateOptions<StateType> | undefined;
Expand Down
21 changes: 19 additions & 2 deletions frontend/src/framework/ModuleRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Module } from "./Module";
import { DrawPreviewFunc } from "./Preview";
import { StateBaseType, StateOptions } from "./StateStore";
import { SyncSettingKey } from "./SyncSettings";
import { ModuleNotFoundPlaceholder } from "./internal/ModuleNotFoundPlaceholder";

export type RegisterModuleOptions = {
moduleName: string;
Expand All @@ -13,8 +14,19 @@ export type RegisterModuleOptions = {
description?: string;
};

export class ModuleNotFoundError extends Error {
readonly moduleName: string;
constructor(moduleName: string) {
super(
`Module '${moduleName}' not found. Did you forget to register your module in 'src/modules/registerAllModules.ts'?`
);
this.moduleName = moduleName;
}
}

export class ModuleRegistry {
private static _registeredModules: Record<string, Module<any>> = {};
private static _moduleNotFoundPlaceholders: Record<string, Module<any>> = {};

/* eslint-disable-next-line @typescript-eslint/no-empty-function */
private constructor() {}
Expand Down Expand Up @@ -44,15 +56,20 @@ export class ModuleRegistry {
module.setDefaultState(defaultState, options);
return module as Module<ModuleStateType>;
}
throw "Did you forget to register your module in 'src/modules/registerAllModules.ts'?";
throw new ModuleNotFoundError(moduleName);
}

static getModule(moduleName: string): Module<any> {
const module = this._registeredModules[moduleName];
if (module) {
return module as Module<any>;
}
throw "Did you forget to register your module in 'src/modules/registerAllModules.ts'?";
const placeholder = this._moduleNotFoundPlaceholders[moduleName];
if (placeholder) {
return placeholder as Module<any>;
}
this._moduleNotFoundPlaceholders[moduleName] = new ModuleNotFoundPlaceholder(moduleName);
return this._moduleNotFoundPlaceholders[moduleName] as Module<any>;
}

static getRegisteredModules(): Record<string, Module<any>> {
Expand Down
61 changes: 61 additions & 0 deletions frontend/src/framework/internal/ModuleNotFoundPlaceholder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ImportState, Module } from "@framework/Module";
import { ModuleInstance } from "@framework/ModuleInstance";
import { BugAntIcon, ChatBubbleLeftRightIcon, NoSymbolIcon } from "@heroicons/react/20/solid";
import { Button } from "@lib/components/Button";
import { Tag } from "@lib/components/Tag";

export class ModuleNotFoundPlaceholder extends Module<Record<string, never>> {
constructor(moduleName: string) {
super(moduleName, moduleName, [], {}, null);
this._importState = ImportState.Imported;
}

makeInstance(instanceNumber: number): ModuleInstance<Record<string, never>> {
const instance = super.makeInstance(instanceNumber);
instance.setDefaultState({});
return instance;
}

viewFC = () => {
function reportIssue() {
window.open("https://github.com/equinor/webviz/issues/new", "_blank");
}

function startDiscussion() {
window.open(
"https://github.com/equinor/webviz/discussions/new?category=announcements&welcome_text=true",
"_blank"
);
}

return (
<div className="w-full h-full flex flex-col items-center justify-center gap-6">
<NoSymbolIcon className="w-16 h-16 text-gray-400" />
<span className="text-lg text-red-500">
Module <Tag label={this.getName()} /> not found.
</span>
<span className="text-sm text-gray-600 text-center">
The module is no longer available and might have been removed from the application. You can safely
remove the module instance by clicking on the cross in its header. If you are wondering why this
module has been removed, please get in touch with us on GitHub.
</span>
<div className="flex gap-4">
<Button startIcon={<BugAntIcon className="w-4 h-4" />} onClick={reportIssue}>
Report issue
</Button>
<Button startIcon={<ChatBubbleLeftRightIcon className="w-4 h-4" />} onClick={startDiscussion}>
Start discussion
</Button>
</div>
</div>
);
};

settingsFC = () => {
return (
<div>
Module <Tag label={this.getName()} /> not found.
</div>
);
};
}
Loading