Skip to content

Commit

Permalink
Proposal for handling of missing/removed modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Sep 13, 2023
1 parent 64467e8 commit 81e9ef6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
5 changes: 1 addition & 4 deletions 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 Expand Up @@ -99,14 +99,11 @@ export class Module<StateType extends StateBaseType> {
return this._syncableSettingKeys;
}


hasSyncableSettingKey(key: SyncSettingKey): boolean {
return this._syncableSettingKeys.includes(key);
}


makeInstance(instanceNumber: number): ModuleInstance<StateType> {

if (!this._workbench) {
throw new Error("Module must be added to a workbench before making an instance");
}
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 @@ -12,8 +13,19 @@ export type RegisterModuleOptions = {
preview?: DrawPreviewFunc;
};

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 @@ -42,15 +54,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>
);
};
}

0 comments on commit 81e9ef6

Please sign in to comment.