Provide standardised MasterDetail

Summary:
Noticed in reviews during the convertathon there is still quite some boilerplate in things that happen on the boundary of UI and plugin state, such as setting up menu entries and providing common functionality like clear, master/detail layout, etc.

This diff introduces the `MasterDetail` component, which takes a higher level approach by merely needing to provide the state atoms and desired features, and taking care of the wiring.

Applied it to createTablePlugin, to prove that going from `createTablePlugin` to `MasterDetail` will be a much smaller step now.

Verified on the funnel logger plugin

Reviewed By: passy

Differential Revision: D28090362

fbshipit-source-id: 146f8c315fea903901ad4e3e46711642f16cf0e6
This commit is contained in:
Michel Weststrate
2021-04-29 07:30:56 -07:00
committed by Facebook GitHub Bot
parent e7cdbcbe85
commit e26a8c5ad0
9 changed files with 347 additions and 126 deletions

View File

@@ -77,6 +77,12 @@ export interface BasePluginClient {
*/
createPaste(input: string): Promise<string | undefined>;
/**
* Returns true if this is an internal Facebook build.
* Always returns `false` in open source
*/
readonly isFB: boolean;
/**
* Returns true if the user is taking part in the given gatekeeper.
* Always returns `false` in open source.
@@ -224,6 +230,8 @@ export abstract class BasePluginInstance {
batched(this.importHandler)(this.initialStates);
} catch (e) {
const msg = `Error occurred when importing date for plugin '${this.definition.id}': '${e}`;
// msg is already specific
// eslint-disable-next-line
console.error(msg, e);
message.error(msg);
}
@@ -275,20 +283,25 @@ export abstract class BasePluginInstance {
addMenuEntry: (...entries) => {
for (const entry of entries) {
const normalized = normalizeMenuEntry(entry);
if (
this.menuEntries.find(
(existing) =>
existing.label === normalized.label ||
existing.action === normalized.action,
)
) {
throw new Error(`Duplicate menu entry: '${normalized.label}'`);
const idx = this.menuEntries.findIndex(
(existing) =>
existing.label === normalized.label ||
existing.action === normalized.action,
);
if (idx !== -1) {
this.menuEntries[idx] = normalizeMenuEntry(entry);
} else {
this.menuEntries.push(normalizeMenuEntry(entry));
}
if (this.activated) {
// entries added after initial registration
this.flipperLib.enableMenuEntries(this.menuEntries);
}
this.menuEntries.push(normalizeMenuEntry(entry));
}
},
writeTextToClipboard: this.flipperLib.writeTextToClipboard,
createPaste: this.flipperLib.createPaste,
isFB: this.flipperLib.isFB,
GK: this.flipperLib.GK,
showNotification: (notification: Notification) => {
this.flipperLib.showNotification(this.pluginKey, notification);
@@ -301,8 +314,8 @@ export abstract class BasePluginInstance {
activate() {
this.assertNotDestroyed();
if (!this.activated) {
this.activated = true;
this.flipperLib.enableMenuEntries(this.menuEntries);
this.activated = true;
this.events.emit('activate');
this.flipperLib.logger.trackTimeSince(
`activePlugin-${this.definition.id}`,