Introduce favorite plugins

Summary: This diff lands improved sidebar navigation. The old functionality to order plugins based on last-recently-used, and cropping at 5 items has been removed. Instead, items can be starred and their position will be fixed. Together with the app switcher introduced this should lead to a cleaner, stabler, and more customizable UI.

Reviewed By: jknoxville

Differential Revision: D18299401

fbshipit-source-id: 29b7eb3a4130933c637f7c81834558bf738d5bf0
This commit is contained in:
Michel Weststrate
2019-11-05 09:13:31 -08:00
committed by Facebook Github Bot
parent 969a857fae
commit 3cee927674
9 changed files with 373 additions and 278 deletions

View File

@@ -22,7 +22,6 @@ import {registerPlugins} from './reducers/plugins';
import createTableNativePlugin from './plugins/TableNativePlugin'; import createTableNativePlugin from './plugins/TableNativePlugin';
import EventEmitter from 'events'; import EventEmitter from 'events';
import invariant from 'invariant'; import invariant from 'invariant';
import {Responder} from 'rsocket-types/ReactiveSocketTypes';
type Plugins = Array<string>; type Plugins = Array<string>;
@@ -98,11 +97,6 @@ const handleError = (
} }
}; };
export const MAX_MINIMUM_PLUGINS = 5;
export const SHOW_REMAINING_PLUGIN_IF_LESS_THAN = 3;
export const SAVED_PLUGINS_COUNT =
MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN;
export default class Client extends EventEmitter { export default class Client extends EventEmitter {
app: App | undefined; app: App | undefined;
connected: boolean; connected: boolean;
@@ -111,8 +105,6 @@ export default class Client extends EventEmitter {
sdkVersion: number; sdkVersion: number;
messageIdCounter: number; messageIdCounter: number;
plugins: Plugins; plugins: Plugins;
lessPlugins: Plugins | undefined;
showAllPlugins: boolean;
connection: RSocketClientSocket<any, any> | null | undefined; connection: RSocketClientSocket<any, any> | null | undefined;
store: Store; store: Store;
activePlugins: Set<string>; activePlugins: Set<string>;
@@ -146,7 +138,6 @@ export default class Client extends EventEmitter {
super(); super();
this.connected = true; this.connected = true;
this.plugins = plugins ? plugins : []; this.plugins = plugins ? plugins : [];
this.showAllPlugins = false;
this.connection = conn; this.connection = conn;
this.id = id; this.id = id;
this.query = query; this.query = query;
@@ -188,29 +179,6 @@ export default class Client extends EventEmitter {
} }
} }
/// Sort plugins by LRU order stored in lessPlugins; if not, sort by alphabet
byClientLRU(
pluginsCount: number,
a: typeof FlipperPlugin,
b: typeof FlipperPlugin,
): number {
// Sanity check
if (this.lessPlugins != null) {
const showPluginsCount =
pluginsCount >= MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN
? MAX_MINIMUM_PLUGINS
: pluginsCount;
let idxA = this.lessPlugins.indexOf(a.id);
idxA = idxA < 0 || idxA >= showPluginsCount ? showPluginsCount : idxA;
let idxB = this.lessPlugins.indexOf(b.id);
idxB = idxB < 0 || idxB >= showPluginsCount ? showPluginsCount : idxB;
if (idxA !== idxB) {
return idxA > idxB ? 1 : -1;
}
}
return (a.title || a.id) > (b.title || b.id) ? 1 : -1;
}
/* All clients should have a corresponding Device in the store. /* All clients should have a corresponding Device in the store.
However, clients can connect before a device is registered, so wait a However, clients can connect before a device is registered, so wait a
while for the device to be registered if it isn't already. */ while for the device to be registered if it isn't already. */

View File

@@ -7,7 +7,7 @@
* @format * @format
*/ */
import {styled, colors} from 'flipper'; import {styled, colors, Glyph} from 'flipper';
import React, {useState, memo} from 'react'; import React, {useState, memo} from 'react';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {FlipperError, dismissError} from '../reducers/connections'; import {FlipperError, dismissError} from '../reducers/connections';
@@ -51,7 +51,13 @@ const ErrorBar = memo(function ErrorBar(props: Props) {
<DismissAllErrors <DismissAllErrors
onClick={() => setCollapsed(c => !c)} onClick={() => setCollapsed(c => !c)}
title="Show / hide errors"> title="Show / hide errors">
{collapsed ? `${errorCount}` : '▲'} <Glyph
color={colors.white}
size={8}
name={collapsed ? 'chevron-down' : 'chevron-up'}
style={{marginRight: 4}}
/>
{collapsed && errorCount}
</DismissAllErrors> </DismissAllErrors>
</ErrorBarContainer> </ErrorBarContainer>
); );

View File

@@ -29,12 +29,13 @@ import {
FlipperDevicePlugin, FlipperDevicePlugin,
LoadingIndicator, LoadingIndicator,
Button, Button,
StarButton,
} from 'flipper'; } from 'flipper';
import React, {Component, PureComponent, Fragment} from 'react'; import React, {Component, PureComponent, Fragment} from 'react';
import NotificationsHub from '../NotificationsHub'; import NotificationsHub from '../NotificationsHub';
import { import {
selectPlugin, selectPlugin,
showMoreOrLessPlugins, starPlugin,
StaticView, StaticView,
setStaticView, setStaticView,
} from '../reducers/connections'; } from '../reducers/connections';
@@ -42,13 +43,12 @@ import {setActiveSheet} from '../reducers/application';
import UserAccount from './UserAccount'; import UserAccount from './UserAccount';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {BackgroundColorProperty} from 'csstype'; import {BackgroundColorProperty} from 'csstype';
import {
MAX_MINIMUM_PLUGINS,
SHOW_REMAINING_PLUGIN_IF_LESS_THAN,
} from '../Client';
import {StyledOtherComponent} from 'create-emotion-styled'; import {StyledOtherComponent} from 'create-emotion-styled';
import SupportRequestFormManager from '../fb-stubs/SupportRequestFormManager'; import SupportRequestFormManager from '../fb-stubs/SupportRequestFormManager';
type FlipperPlugins = (typeof FlipperPlugin)[];
type PluginsByCategory = [string, FlipperPlugins][];
const ListItem = styled('div')(({active}: {active?: boolean}) => ({ const ListItem = styled('div')(({active}: {active?: boolean}) => ({
paddingLeft: 10, paddingLeft: 10,
display: 'flex', display: 'flex',
@@ -64,19 +64,18 @@ const ListItem = styled('div')(({active}: {active?: boolean}) => ({
}, },
})); }));
const SidebarHeader = styled(FlexBox)({ const SidebarButton = styled(Button)(({small}: {small?: boolean}) => ({
display: 'block', fontWeight: 'bold',
alignItems: 'center', fontSize: small ? 11 : 14,
padding: 3, width: '100%',
color: colors.macOSSidebarSectionTitle,
fontSize: 11,
fontWeight: 500,
marginLeft: 7,
textOverflow: 'ellipsis',
overflow: 'hidden', overflow: 'hidden',
whiteSpace: 'nowrap', marginTop: small ? 0 : 20,
flexShrink: 0, pointer: 'cursor',
}); border: 'none',
background: 'none',
padding: 0,
justifyContent: 'left',
}));
const PluginShape = styled(FlexBox)( const PluginShape = styled(FlexBox)(
({backgroundColor}: {backgroundColor?: BackgroundColorProperty}) => ({ ({backgroundColor}: {backgroundColor?: BackgroundColorProperty}) => ({
@@ -130,23 +129,6 @@ const Plugins = styled(FlexColumn)({
overflow: 'auto', overflow: 'auto',
}); });
const PluginDebugger = styled(FlexBox)({
color: colors.blackAlpha50,
alignItems: 'center',
padding: 10,
flexShrink: 0,
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
});
const PluginShowMoreOrLess = styled(ListItem)({
color: colors.blue,
fontSize: 10,
lineHeight: '10px',
paddingBottom: 5,
});
function PluginIcon({ function PluginIcon({
isActive, isActive,
backgroundColor, backgroundColor,
@@ -170,9 +152,13 @@ class PluginSidebarListItem extends Component<{
isActive: boolean; isActive: boolean;
plugin: typeof FlipperBasePlugin; plugin: typeof FlipperBasePlugin;
app?: string | null | undefined; app?: string | null | undefined;
helpRef?: any;
provided?: any;
onFavorite?: () => void;
starred?: boolean;
}> { }> {
render() { render() {
const {isActive, plugin} = this.props; const {isActive, plugin, onFavorite, starred} = this.props;
const app = this.props.app || 'Facebook'; const app = this.props.app || 'Facebook';
let iconColor: string | undefined = (brandColors as any)[app]; let iconColor: string | undefined = (brandColors as any)[app];
@@ -201,6 +187,9 @@ class PluginSidebarListItem extends Component<{
color={colors.white} color={colors.white}
/> />
<PluginName>{plugin.title || plugin.id}</PluginName> <PluginName>{plugin.title || plugin.id}</PluginName>
{starred !== undefined && (
<StarButton onStar={onFavorite!} starred={starred} />
)}
</ListItem> </ListItem>
); );
} }
@@ -230,6 +219,7 @@ type StateFromProps = {
staticView: StaticView; staticView: StaticView;
selectedPlugin: string | null | undefined; selectedPlugin: string | null | undefined;
selectedApp: string | null | undefined; selectedApp: string | null | undefined;
userStarredPlugins: Store['connections']['userStarredPlugins'];
clients: Array<Client>; clients: Array<Client>;
uninitializedClients: Array<{ uninitializedClients: Array<{
client: UninitializedClient; client: UninitializedClient;
@@ -248,16 +238,21 @@ type DispatchFromProps = {
}) => void; }) => void;
setActiveSheet: (activeSheet: ActiveSheet) => void; setActiveSheet: (activeSheet: ActiveSheet) => void;
setStaticView: (payload: StaticView) => void; setStaticView: (payload: StaticView) => void;
showMoreOrLessPlugins: (payload: string) => void; starPlugin: typeof starPlugin;
}; };
type Props = OwnProps & StateFromProps & DispatchFromProps; type Props = OwnProps & StateFromProps & DispatchFromProps;
type State = {showSupportForm: boolean; selectedClientIndex: number}; type State = {
showSupportForm: boolean;
selectedClientIndex: number;
showAllPlugins: boolean;
};
class MainSidebar extends PureComponent<Props, State> { class MainSidebar extends PureComponent<Props, State> {
state: State = { state: State = {
showSupportForm: GK.get('flipper_support_requests'), showSupportForm: GK.get('flipper_support_requests'),
// Not to be confused with selectedApp prop, this one only used to remember the client drowdown selector // Not to be confused with selectedApp prop, this one only used to remember the client drowdown selector
selectedClientIndex: 0, selectedClientIndex: 0,
showAllPlugins: false,
}; };
static getDerivedStateFromProps(props: Props, state: State) { static getDerivedStateFromProps(props: Props, state: State) {
if ( if (
@@ -297,11 +292,6 @@ class MainSidebar extends PureComponent<Props, State> {
const client: Client | null = const client: Client | null =
clients[this.state.selectedClientIndex] || null; clients[this.state.selectedClientIndex] || null;
const byPluginNameOrId = (
a: typeof FlipperBasePlugin,
b: typeof FlipperBasePlugin,
) => ((a.title || a.id) > (b.title || b.id) ? 1 : -1);
return ( return (
<Sidebar <Sidebar
position="left" position="left"
@@ -310,6 +300,69 @@ class MainSidebar extends PureComponent<Props, State> {
process.platform === 'darwin' && windowIsFocused ? 'transparent' : '' process.platform === 'darwin' && windowIsFocused ? 'transparent' : ''
}> }>
<Plugins> <Plugins>
{selectedDevice && (
<ListItem>
<SidebarButton>{selectedDevice.title}</SidebarButton>
</ListItem>
)}
{selectedDevice &&
Array.from(this.props.devicePlugins.values())
.filter(plugin => plugin.supportsDevice(selectedDevice))
.sort(sortPluginsByName)
.map((plugin: typeof FlipperDevicePlugin) => (
<PluginSidebarListItem
key={plugin.id}
isActive={plugin.id === selectedPlugin}
onClick={() =>
selectPlugin({
selectedPlugin: plugin.id,
selectedApp: null,
deepLinkPayload: null,
})
}
plugin={plugin}
/>
))}
<ListItem>
<SidebarButton
title="Select an app to see available plugins"
compact={true}
dropdown={clients.map((c, index) => ({
checked: client === c,
label: c.query.app,
type: 'checkbox',
click: () => this.setState({selectedClientIndex: index}),
}))}>
{clients.length === 0 ? (
'(Not connected to app)'
) : this.state.selectedClientIndex >= clients.length ? (
'(Select app)'
) : (
<>
{client.query.app}
{clients.length > 1 && (
<Glyph
size={12}
name="chevron-down"
style={{marginLeft: 8}}
/>
)}
</>
)}
</SidebarButton>
</ListItem>
{this.renderClientPlugins(client)}
{uninitializedClients.map(entry => (
<ListItem key={JSON.stringify(entry.client)}>
{entry.client.appName}
{entry.errorMessage ? (
<ErrorIndicator name={'mobile-cross'} size={16} />
) : (
<Spinner size={16} />
)}
</ListItem>
))}
</Plugins>
{!GK.get('flipper_disable_notifications') && ( {!GK.get('flipper_disable_notifications') && (
<ListItem <ListItem
active={selectedPlugin === 'notifications'} active={selectedPlugin === 'notifications'}
@@ -319,7 +372,10 @@ class MainSidebar extends PureComponent<Props, State> {
selectedApp: null, selectedApp: null,
deepLinkPayload: null, deepLinkPayload: null,
}) })
}> }
style={{
borderTop: `1px solid ${colors.blackAlpha10}`,
}}>
<PluginIcon <PluginIcon
color={colors.light50} color={colors.light50}
name={ name={
@@ -357,104 +413,28 @@ class MainSidebar extends PureComponent<Props, State> {
</PluginName> </PluginName>
</ListItem> </ListItem>
)} )}
{selectedDevice && ( <ListItem
<SidebarHeader>{selectedDevice.title}</SidebarHeader>
)}
{selectedDevice &&
Array.from(this.props.devicePlugins.values())
.filter(plugin => plugin.supportsDevice(selectedDevice))
.sort(byPluginNameOrId)
.map((plugin: typeof FlipperDevicePlugin) => (
<PluginSidebarListItem
key={plugin.id}
isActive={plugin.id === selectedPlugin}
onClick={() =>
selectPlugin({
selectedPlugin: plugin.id,
selectedApp: null,
deepLinkPayload: null,
})
}
plugin={plugin}
/>
))}
<ListItem style={{marginTop: 20}}>
<Button
title="Select a client to see available plugins"
compact={true}
dropdown={clients.map((c, index) => ({
checked: client === c,
label: c.query.app,
type: 'checkbox',
click: () => this.setState({selectedClientIndex: index}),
}))}
style={{
fontSize: 11,
width: '100%',
overflow: 'hidden',
}}>
{clients.length === 0
? '(Not connected to client)'
: this.state.selectedClientIndex >= clients.length
? '(Select a client)'
: client.query.app}{' '}
</Button>
</ListItem>
{this.renderClientPlugins(client)}
{uninitializedClients.map(entry => (
<React.Fragment key={JSON.stringify(entry.client)}>
<SidebarHeader>{entry.client.appName}</SidebarHeader>
{entry.errorMessage ? (
<ErrorIndicator name={'mobile-cross'} size={16} />
) : (
<Spinner size={16} />
)}
</React.Fragment>
))}
</Plugins>
<PluginDebugger
onClick={() => this.props.setActiveSheet(ACTIVE_SHEET_PLUGINS)}> onClick={() => this.props.setActiveSheet(ACTIVE_SHEET_PLUGINS)}>
<Glyph <PluginIcon
name="question-circle" name="question-circle"
size={16} color={colors.light50}
variant="outline" isActive={false}
color={colors.blackAlpha30}
/> />
&nbsp;Manage Plugins... Manage Plugins
</PluginDebugger> </ListItem>
{config.showLogin && <UserAccount />} {config.showLogin && <UserAccount />}
</Sidebar> </Sidebar>
); );
} }
renderClientPlugins(client: Client | null) { renderPluginsByCategory(
if (!client) { client: Client,
return null; plugins: FlipperPlugins,
} starred: boolean,
onFavorite: (pluginId: string) => void,
) {
const {selectedPlugin, selectedApp, selectPlugin} = this.props; const {selectedPlugin, selectedApp, selectPlugin} = this.props;
const plugins = Array.from(this.props.clientPlugins.values()).filter( return groupPluginsByCategory(plugins).map(([category, plugins]) => (
(p: typeof FlipperPlugin) => client.plugins.indexOf(p.id) > -1,
);
const minShowPluginsCount =
plugins.length < MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN
? plugins.length
: MAX_MINIMUM_PLUGINS;
return (
<React.Fragment key={client.id}>
{groupPluginsByCategory(
plugins
.sort((a: typeof FlipperPlugin, b: typeof FlipperPlugin) =>
client.byClientLRU(plugins.length, a, b),
)
.slice(
0,
client.showAllPlugins
? client.plugins.length
: minShowPluginsCount,
),
).map(([category, plugins]) => (
<Fragment key={category}> <Fragment key={category}>
{category && ( {category && (
<ListItem> <ListItem>
@@ -464,9 +444,7 @@ class MainSidebar extends PureComponent<Props, State> {
{plugins.map(plugin => ( {plugins.map(plugin => (
<PluginSidebarListItem <PluginSidebarListItem
key={plugin.id} key={plugin.id}
isActive={ isActive={plugin.id === selectedPlugin && selectedApp === client.id}
plugin.id === selectedPlugin && selectedApp === client.id
}
onClick={() => onClick={() =>
selectPlugin({ selectPlugin({
selectedPlugin: plugin.id, selectedPlugin: plugin.id,
@@ -476,30 +454,117 @@ class MainSidebar extends PureComponent<Props, State> {
} }
plugin={plugin} plugin={plugin}
app={client.query.app} app={client.query.app}
onFavorite={() => onFavorite(plugin.id)}
starred={starred}
/> />
))} ))}
</Fragment> </Fragment>
))} ));
{plugins.length > minShowPluginsCount && ( }
<PluginShowMoreOrLess
onClick={() => this.props.showMoreOrLessPlugins(client.id)}> renderClientPlugins(client: Client | null) {
{client.showAllPlugins ? 'Show less' : 'Show more'} if (!client) {
</PluginShowMoreOrLess> return null;
}
const onFavorite = (plugin: string) => {
this.props.starPlugin({
selectedApp: client.id,
selectedPlugin: plugin,
});
};
const allPlugins = Array.from(this.props.clientPlugins.values());
const favoritePlugins: FlipperPlugins = getFavoritePlugins(
client,
allPlugins,
this.props.userStarredPlugins,
true,
);
return (
<>
{favoritePlugins.length === 0 ? (
<ListItem>
<div style={{textAlign: 'center', width: '100%'}}>
Star some plugins!
<hr style={{width: '100%'}} />
</div>
</ListItem>
) : (
<>
{this.renderPluginsByCategory(
client,
favoritePlugins,
true,
onFavorite,
)} )}
</React.Fragment> <ListItem>
<SidebarButton
small
compact
onClick={() =>
this.setState(state => ({
...state,
showAllPlugins: !state.showAllPlugins,
}))
}>
{this.state.showAllPlugins ? 'Show less' : 'Show more'}
<Glyph
size={8}
name={
this.state.showAllPlugins ? 'chevron-up' : 'chevron-down'
}
style={{
marginLeft: 4,
}}
/>
</SidebarButton>
</ListItem>
</>
)}
<div
style={{
flex: 'auto' /*scroll this region, not the entire thing*/,
overflow: 'auto',
height: 'auto',
}}>
{this.state.showAllPlugins || favoritePlugins.length === 0
? this.renderPluginsByCategory(
client,
getFavoritePlugins(
client,
allPlugins,
this.props.userStarredPlugins,
false,
),
false,
onFavorite,
)
: null}
</div>
</>
); );
} }
} }
type PluginsByCategory = [string, (typeof FlipperPlugin)[]][]; function getFavoritePlugins(
client: Client,
allPlugins: FlipperPlugins,
userStarredPlugins: Props['userStarredPlugins'],
favorite: boolean,
): FlipperPlugins {
const appName = client.id;
return allPlugins.filter(plugin => {
const idx = userStarredPlugins[appName]
? userStarredPlugins[appName].indexOf(plugin.id)
: -1;
return idx === -1 ? !favorite : favorite;
});
}
function groupPluginsByCategory( function groupPluginsByCategory(plugins: FlipperPlugins): PluginsByCategory {
plugins: (typeof FlipperPlugin)[], const sortedPlugins = plugins.slice().sort(sortPluginsByName);
): PluginsByCategory { const byCategory: {[cat: string]: FlipperPlugins} = {};
// Pre condition: plugins are already sorted globally
const byCategory: {[cat: string]: (typeof FlipperPlugin)[]} = {};
const res: PluginsByCategory = []; const res: PluginsByCategory = [];
plugins.forEach(plugin => { sortedPlugins.forEach(plugin => {
const category = plugin.category || ''; const category = plugin.category || '';
(byCategory[category] || (byCategory[category] = [])).push(plugin); (byCategory[category] || (byCategory[category] = [])).push(plugin);
}); });
@@ -512,6 +577,13 @@ function groupPluginsByCategory(
return res; return res;
} }
function sortPluginsByName(
a: typeof FlipperBasePlugin,
b: typeof FlipperBasePlugin,
): number {
return (a.title || a.id) > (b.title || b.id) ? 1 : -1;
}
export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>( export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
({ ({
application: {windowIsFocused}, application: {windowIsFocused},
@@ -519,6 +591,7 @@ export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
selectedDevice, selectedDevice,
selectedPlugin, selectedPlugin,
selectedApp, selectedApp,
userStarredPlugins,
clients, clients,
uninitializedClients, uninitializedClients,
staticView, staticView,
@@ -537,6 +610,7 @@ export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
staticView, staticView,
selectedPlugin, selectedPlugin,
selectedApp, selectedApp,
userStarredPlugins,
clients, clients,
uninitializedClients, uninitializedClients,
devicePlugins, devicePlugins,
@@ -546,6 +620,6 @@ export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
selectPlugin, selectPlugin,
setStaticView, setStaticView,
setActiveSheet, setActiveSheet,
showMoreOrLessPlugins, starPlugin,
}, },
)(MainSidebar); )(MainSidebar);

View File

@@ -14,7 +14,6 @@ import {UninitializedClient} from '../UninitializedClient';
import {isEqual} from 'lodash'; import {isEqual} from 'lodash';
import iosUtil from '../fb-stubs/iOSContainerUtility'; import iosUtil from '../fb-stubs/iOSContainerUtility';
import {performance} from 'perf_hooks'; import {performance} from 'perf_hooks';
import {SAVED_PLUGINS_COUNT} from '../Client';
import isHeadless from '../utils/isHeadless'; import isHeadless from '../utils/isHeadless';
import {Actions} from '.'; import {Actions} from '.';
const WelcomeScreen = isHeadless() const WelcomeScreen = isHeadless()
@@ -43,7 +42,7 @@ export type State = {
userPreferredDevice: null | string; userPreferredDevice: null | string;
userPreferredPlugin: null | string; userPreferredPlugin: null | string;
userPreferredApp: null | string; userPreferredApp: null | string;
userLRUPlugins: {[key: string]: Array<string>}; userStarredPlugins: {[key: string]: Array<string>};
errors: FlipperError[]; errors: FlipperError[];
clients: Array<Client>; clients: Array<Client>;
uninitializedClients: Array<{ uninitializedClients: Array<{
@@ -116,11 +115,6 @@ export type Action =
type: 'CLIENT_SETUP_ERROR'; type: 'CLIENT_SETUP_ERROR';
payload: {client: UninitializedClient; error: FlipperError}; payload: {client: UninitializedClient; error: FlipperError};
} }
| {
type: 'CLIENT_SHOW_MORE_OR_LESS';
payload: string;
}
| {type: 'CLEAR_LRU_PLUGINS_HISTORY'}
| { | {
type: 'SET_STATIC_VIEW'; type: 'SET_STATIC_VIEW';
payload: StaticView; payload: StaticView;
@@ -128,6 +122,13 @@ export type Action =
| { | {
type: 'DISMISS_ERROR'; type: 'DISMISS_ERROR';
payload: number; payload: number;
}
| {
type: 'STAR_PLUGIN';
payload: {
selectedPlugin: string;
selectedApp: string;
};
}; };
const DEFAULT_PLUGIN = 'DeviceLogs'; const DEFAULT_PLUGIN = 'DeviceLogs';
@@ -141,7 +142,7 @@ const INITAL_STATE: State = {
userPreferredDevice: null, userPreferredDevice: null,
userPreferredPlugin: null, userPreferredPlugin: null,
userPreferredApp: null, userPreferredApp: null,
userLRUPlugins: {}, userStarredPlugins: {},
errors: [], errors: [],
clients: [], clients: [],
uninitializedClients: [], uninitializedClients: [],
@@ -259,45 +260,43 @@ const reducer = (state: State = INITAL_STATE, action: Actions): State => {
} }
const userPreferredApp = selectedApp || state.userPreferredApp; const userPreferredApp = selectedApp || state.userPreferredApp;
const selectedAppName = extractAppNameFromAppId(userPreferredApp);
// Need to recreate an array to make sure that it doesn't refer to the
// array that is showed in on the screen and the array that is kept for
// least recently used plugins reference
const LRUPlugins = [
...((selectedAppName && state.userLRUPlugins[selectedAppName]) || []),
];
const idxLRU =
(selectedPlugin && LRUPlugins.indexOf(selectedPlugin)) || -1;
if (idxLRU >= 0) {
LRUPlugins.splice(idxLRU, 1);
}
selectedPlugin && LRUPlugins.unshift(selectedPlugin);
LRUPlugins.splice(SAVED_PLUGINS_COUNT);
return { return {
...state, ...state,
...payload, ...payload,
staticView: null, staticView: null,
userPreferredApp: userPreferredApp, userPreferredApp: userPreferredApp,
userPreferredPlugin: selectedPlugin, userPreferredPlugin: selectedPlugin,
userLRUPlugins: selectedAppName
? {
...state.userLRUPlugins,
[selectedAppName]: LRUPlugins,
}
: {...state.userLRUPlugins},
}; };
} }
case 'STAR_PLUGIN': {
const {selectedPlugin, selectedApp} = action.payload;
const starredPluginsForApp = [
...(state.userStarredPlugins[selectedApp] || []),
];
const idx = starredPluginsForApp.indexOf(selectedPlugin);
if (idx === -1) {
starredPluginsForApp.push(selectedPlugin);
} else {
starredPluginsForApp.splice(idx, 1);
}
return {
...state,
userStarredPlugins: {
...state.userStarredPlugins,
[selectedApp]: starredPluginsForApp,
},
};
}
case 'SELECT_USER_PREFERRED_PLUGIN': { case 'SELECT_USER_PREFERRED_PLUGIN': {
const {payload} = action; const {payload} = action;
return {...state, userPreferredPlugin: payload}; return {...state, userPreferredPlugin: payload};
} }
case 'NEW_CLIENT': { case 'NEW_CLIENT': {
const {payload} = action; const {payload} = action;
const {userPreferredApp, userPreferredPlugin, userLRUPlugins} = state; const {userPreferredApp, userPreferredPlugin} = state;
let {selectedApp, selectedPlugin} = state; let {selectedApp, selectedPlugin} = state;
const appName = extractAppNameFromAppId(payload.id);
payload.lessPlugins = (appName && userLRUPlugins[appName]) || [];
if ( if (
userPreferredApp && userPreferredApp &&
userPreferredPlugin && userPreferredPlugin &&
@@ -317,10 +316,6 @@ const reducer = (state: State = INITAL_STATE, action: Actions): State => {
c.client.appName !== payload.query.app c.client.appName !== payload.query.app
); );
}), }),
userLRUPlugins: {
...state.userLRUPlugins,
[payload.id]: payload.lessPlugins,
},
selectedApp, selectedApp,
selectedPlugin, selectedPlugin,
}; };
@@ -420,33 +415,6 @@ const reducer = (state: State = INITAL_STATE, action: Actions): State => {
}), }),
}; };
} }
case 'CLIENT_SHOW_MORE_OR_LESS': {
const {payload} = action;
const appName = extractAppNameFromAppId(payload);
return {
...state,
clients: state.clients.map((client: Client) => {
if (appName && extractAppNameFromAppId(client.id) === appName) {
client.showAllPlugins = !client.showAllPlugins;
client.lessPlugins = state.userLRUPlugins[appName] || [];
}
return client;
}),
};
}
case 'CLEAR_LRU_PLUGINS_HISTORY': {
const clearLRUPlugins: {[key: string]: Array<string>} = {};
Object.keys(state.userLRUPlugins).forEach((key: string) => {
if (key !== null) {
clearLRUPlugins[key] = [];
}
});
return {
...state,
userLRUPlugins: clearLRUPlugins,
};
}
case 'DISMISS_ERROR': { case 'DISMISS_ERROR': {
const errors = state.errors.slice(); const errors = state.errors.slice();
errors.splice(action.payload, 1); errors.splice(action.payload, 1);
@@ -531,8 +499,11 @@ export const selectPlugin = (payload: {
payload, payload,
}); });
export const showMoreOrLessPlugins = (payload: string): Action => ({ export const starPlugin = (payload: {
type: 'CLIENT_SHOW_MORE_OR_LESS', selectedPlugin: string;
selectedApp: string;
}): Action => ({
type: 'STAR_PLUGIN',
payload, payload,
}); });

View File

@@ -96,7 +96,7 @@ export default combineReducers<State, Actions>({
'userPreferredDevice', 'userPreferredDevice',
'userPreferredPlugin', 'userPreferredPlugin',
'userPreferredApp', 'userPreferredApp',
'userLRUPlugins', 'userStarredPlugins',
], ],
}, },
connections, connections,

View File

@@ -43,12 +43,13 @@ function ColoredIcon(
size?: number; size?: number;
className?: string; className?: string;
color?: string; color?: string;
style?: React.CSSProperties;
}, },
context: { context: {
glyphColor?: string; glyphColor?: string;
}, },
) { ) {
const {color = context.glyphColor, name, size = 16, src} = props; const {color = context.glyphColor, name, size = 16, src, style} = props;
const isBlack = const isBlack =
color == null || color == null ||
@@ -63,6 +64,7 @@ function ColoredIcon(
src={src} src={src}
size={size} size={size}
className={props.className} className={props.className}
style={style}
/> />
); );
} else { } else {
@@ -72,6 +74,7 @@ function ColoredIcon(
size={size} size={size}
src={src} src={src}
className={props.className} className={props.className}
style={style}
/> />
); );
} }
@@ -87,9 +90,10 @@ export default class Glyph extends React.PureComponent<{
variant?: 'filled' | 'outline'; variant?: 'filled' | 'outline';
className?: string; className?: string;
color?: string; color?: string;
style?: React.CSSProperties;
}> { }> {
render() { render() {
const {name, size = 16, variant, color, className} = this.props; const {name, size = 16, variant, color, className, style} = this.props;
return ( return (
<ColoredIcon <ColoredIcon
@@ -102,6 +106,7 @@ export default class Glyph extends React.PureComponent<{
size, size,
typeof window !== 'undefined' ? window.devicePixelRatio : 1, typeof window !== 'undefined' ? window.devicePixelRatio : 1,
)} )}
style={style}
/> />
); );
} }

View File

@@ -0,0 +1,62 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import React, {useState, useCallback} from 'react';
import {colors} from './colors';
import Glyph from './Glyph';
import styled from 'react-emotion';
const DownscaledGlyph = styled(Glyph)({
maskSize: '12px 12px',
WebkitMaskSize: '12px 12px',
height: 12,
width: 12,
});
export function StarButton({
starred,
onStar,
}: {
starred: boolean;
onStar: () => void;
}) {
const [hovered, setHovered] = useState(false);
const handleClick = useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
onStar();
},
[onStar],
);
const handleMouseEnter = useCallback(setHovered.bind(null, true), []);
const handleMouseLeave = useCallback(setHovered.bind(null, false), []);
return (
<button
style={{
border: 'none',
background: 'none',
cursor: 'pointer',
padding: 0,
paddingLeft: 4,
flex: 0,
}}
onClick={handleClick}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}>
<DownscaledGlyph
size={
16 /* the icons used below are not available in smaller sizes :-/ */
}
name={hovered ? (starred ? 'star-slash' : 'life-event-major') : 'star'}
color={hovered ? colors.lemonDark1 : colors.macOSTitleBarIconBlur}
variant={hovered || starred ? 'filled' : 'outline'}
/>
</button>
);
}

View File

@@ -175,3 +175,4 @@ export {InspectorSidebar} from './components/elements-inspector/sidebar';
export {Console} from './components/console'; export {Console} from './components/console';
export {default as Sheet} from './components/Sheet'; export {default as Sheet} from './components/Sheet';
export {StarButton} from './components/StarButton';

View File

@@ -24,7 +24,8 @@ const ICONS = {
'caution-octagon': [16], 'caution-octagon': [16],
'caution-triangle': [16], 'caution-triangle': [16],
'chevron-down-outline': [10], 'chevron-down-outline': [10],
'chevron-down': [8], 'chevron-down': [8, 12],
'chevron-up': [8, 12],
'chevron-right': [8], 'chevron-right': [8],
'dots-3-circle-outline': [16], 'dots-3-circle-outline': [16],
'info-circle': [16], 'info-circle': [16],
@@ -52,6 +53,8 @@ const ICONS = {
rocket: [20], rocket: [20],
settings: [12], settings: [12],
star: [16, 24], star: [16, 24],
'star-slash': [16],
'life-event-major': [16],
target: [12, 16], target: [12, 16],
tools: [20], tools: [20],
}; };
@@ -77,7 +80,12 @@ function buildLocalIconPath(name, size, density) {
// $FlowFixMe not using flow in this file // $FlowFixMe not using flow in this file
function buildIconURL(name, size, density) { function buildIconURL(name, size, density) {
const icon = getIconPartsFromName(name); const icon = getIconPartsFromName(name);
const url = `https://external.xx.fbcdn.net/assets/?name=${icon.trimmedName}&variant=${icon.variant}&size=${size}&set=facebook_icons&density=${density}x`; // eslint-disable-next-line prettier/prettier
const url = `https://external.xx.fbcdn.net/assets/?name=${
icon.trimmedName
}&variant=${
icon.variant
}&size=${size}&set=facebook_icons&density=${density}x`;
if ( if (
typeof window !== 'undefined' && typeof window !== 'undefined' &&
(!ICONS[name] || !ICONS[name].includes(size)) (!ICONS[name] || !ICONS[name].includes(size))