Restore computeNotifications

Summary:
Restore the logic for setting and computing notifications that was
partially removed with D10300838.

Reviewed By: jknoxville

Differential Revision: D10361547

fbshipit-source-id: 4d229d5f4dbeda3139463e1c348909b9c5dba66f
This commit is contained in:
Pascal Hartig
2018-10-13 04:16:12 -07:00
committed by Facebook Github Bot
parent 4889f5dc6a
commit 01020edbf2
4 changed files with 37 additions and 17 deletions

View File

@@ -11,6 +11,7 @@ import type BaseDevice from './devices/BaseDevice.js';
import type {Props as PluginProps} from './plugin'; import type {Props as PluginProps} from './plugin';
import {FlipperDevicePlugin} from './plugin.js'; import {FlipperDevicePlugin} from './plugin.js';
import type {Notification} from './plugin.js';
import { import {
ErrorBoundary, ErrorBoundary,
Component, Component,
@@ -52,6 +53,10 @@ type Props = {
pluginKey: string, pluginKey: string,
state: Object, state: Object,
}) => void, }) => void,
setActiveNotifications: ({
pluginId: string,
notifications: Array<Notification>,
}) => void,
deepLinkPayload: ?string, deepLinkPayload: ?string,
}; };
@@ -125,7 +130,7 @@ class PluginContainer extends Component<Props, State> {
}; };
render() { render() {
const {pluginStates, setPluginState} = this.props; const {pluginStates, setPluginState, setActiveNotifications} = this.props;
const {activePlugin, pluginKey, target} = this.state; const {activePlugin, pluginKey, target} = this.state;
if (!activePlugin || !target) { if (!activePlugin || !target) {
@@ -143,6 +148,11 @@ class PluginContainer extends Component<Props, State> {
// same time. // same time.
setTimeout(() => setPluginState({pluginKey, state}), 0); setTimeout(() => setPluginState({pluginKey, state}), 0);
}, },
setActiveNotifications: (notifications: Array<Notification>) =>
setActiveNotifications({
pluginId: pluginKey,
notifications: notifications,
}),
target, target,
deepLinkPayload: this.props.deepLinkPayload, deepLinkPayload: this.props.deepLinkPayload,
ref: this.refChanged, ref: this.refChanged,

View File

@@ -24,10 +24,21 @@ export type PluginClient = {|
type PluginTarget = BaseDevice | Client; type PluginTarget = BaseDevice | Client;
export type Notification = {|
id: string,
title: string,
message: string,
severity: 'warning' | 'error',
timestamp?: number,
category?: string,
action?: string,
|};
export type Props<T> = { export type Props<T> = {
logger: Logger, logger: Logger,
persistedState: T, persistedState: T,
setPersistedState: (state: $Shape<T>) => void, setPersistedState: (state: $Shape<T>) => void,
setActiveNotifications: (Array<Notification>) => void,
target: PluginTarget, target: PluginTarget,
deepLinkPayload: ?string, deepLinkPayload: ?string,
}; };
@@ -64,6 +75,9 @@ export class FlipperBasePlugin<
// methods to be overriden by plugins // methods to be overriden by plugins
init(): void {} init(): void {}
teardown(): void {} teardown(): void {}
computeNotifications(props: Props<*>, state: State): Array<Notification> {
return [];
}
// methods to be overridden by subclasses // methods to be overridden by subclasses
_init(): void {} _init(): void {}
_teardown(): void {} _teardown(): void {}
@@ -83,6 +97,10 @@ export class FlipperBasePlugin<
throw new TypeError(`Reducer ${actionData.type} isn't a function`); throw new TypeError(`Reducer ${actionData.type} isn't a function`);
} }
} }
componentDidUpdate(props: Props<*>, state: State): void {
props.setActiveNotifications(this.computeNotifications(props, state));
}
} }
export class FlipperDevicePlugin<S = *, A = *, P = *> extends FlipperBasePlugin< export class FlipperDevicePlugin<S = *, A = *, P = *> extends FlipperBasePlugin<

View File

@@ -22,6 +22,7 @@ import {
} from 'flipper'; } from 'flipper';
import RequestDetails from './RequestDetails.js'; import RequestDetails from './RequestDetails.js';
import {URL} from 'url'; import {URL} from 'url';
import type {Notification} from '../../plugin';
type RequestId = string; type RequestId = string;
@@ -159,20 +160,21 @@ export default class extends FlipperPlugin<State, *, PersistedState> {
}; };
computeNotifications(props: *, state: State) { computeNotifications(props: *, state: State) {
const notifications = {}; const notifications: Array<Notification> = [];
const persistedState = props.persistedState; const persistedState = props.persistedState;
for (const response in persistedState.responses) { for (const response in persistedState.responses) {
const status = persistedState.responses[response].status; const status = persistedState.responses[response].status;
if (status >= 400) { const id = persistedState.requests[response]?.id;
if (status >= 400 && id != null) {
const url = persistedState.requests[response]?.url; const url = persistedState.requests[response]?.url;
const startTime = persistedState.requests[response]?.timestamp;
const endTime = persistedState.responses[response].timestamp; const endTime = persistedState.responses[response].timestamp;
notifications[`${url}-${startTime}`] = { notifications.push({
id,
timestamp: endTime, timestamp: endTime,
title: 'Failed network request', title: 'Failed network request',
message: `Response for ${url} failed with status code ${status}`, message: `Response for ${url} failed with status code ${status}`,
severity: 'error', severity: 'error',
}; });
} }
} }
return notifications; return notifications;

View File

@@ -4,17 +4,7 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
* @format * @format
*/ */
import type {Node} from 'react'; import type {Notification} from '../plugin';
export type Notification = {|
id: string,
title: string,
message: Node,
severity: 'warning' | 'error',
timestamp?: number,
category?: string,
action?: string,
|};
export type PluginNotification = {| export type PluginNotification = {|
notification: Notification, notification: Notification,