{
state: State = {
showSupportForm: GK.get('support_requests_v2'),
showAllPlugins: false,
};
static getDerivedStateFromProps(props: Props, state: State) {
if (
!state.showSupportForm &&
props.staticView === SupportRequestFormManager
) {
// Show SupportForm option even when GK is false and support form is shown.
// That means the user has used deeplink to open support form.
// Once the variable is true, it will be true for the whole session.
return {showSupportForm: true};
}
return state;
}
render() {
const {
selectedDevice,
selectClient,
selectedPlugin,
selectedApp,
staticView,
selectPlugin,
setStaticView,
uninitializedClients,
} = this.props;
const clients = getAvailableClients(selectedDevice, this.props.clients);
const client: Client | undefined = getClientById(clients, selectedApp);
return (
{selectedDevice ? (
<>
{selectedDevice.title}
{this.showArchivedDeviceDetails(selectedDevice)}
{selectedDevice.devicePlugins.map(pluginName => {
const plugin = this.props.devicePlugins.get(pluginName)!;
return (
selectPlugin({
selectedPlugin: plugin.id,
selectedApp: null,
deepLinkPayload: null,
})
}
plugin={plugin}
/>
);
})}
({
checked: client === c,
label: c.query.app,
type: 'checkbox',
click: () => selectClient(c.id),
}))}>
{clients.length === 0 ? (
<>
No clients connected
>
) : !client ? (
'Select client'
) : (
<>
{client.query.app}
>
)}
{this.renderClientPlugins(client)}
{uninitializedClients.map(entry => (
{entry.client.appName}
{entry.errorMessage ? (
) : (
)}
))}
>
) : (
Select a device to get started
)}
{this.renderNotificationsEntry()}
{this.state.showSupportForm &&
(function() {
const active = isStaticViewActive(staticView, SupportRequestFormV2);
return (
setStaticView(SupportRequestFormV2)}>
Litho Support Request
);
})()}
this.props.setActiveSheet(ACTIVE_SHEET_PLUGINS)}>
Manage Plugins
{config.showLogin && }
);
}
showArchivedDeviceDetails(selectedDevice: BaseDevice) {
if (!selectedDevice.isArchived || !selectedDevice.source) {
return null;
}
const {staticView, setStaticView} = this.props;
const supportRequestDetailsactive = isStaticViewActive(
staticView,
SupportRequestDetails,
);
return (
<>
{selectedDevice.source ? 'Imported device' : 'Archived device'}
{this.state.showSupportForm &&
(selectedDevice as ArchivedDevice).supportRequestDetails && (
setStaticView(SupportRequestDetails)}>
Support Request Details
)}
>
);
}
renderPluginsByCategory(
client: Client,
plugins: FlipperPlugins,
starred: boolean,
onFavorite: (pluginId: string) => void,
) {
const {selectedPlugin, selectedApp, selectPlugin} = this.props;
return groupPluginsByCategory(plugins).map(([category, plugins]) => (
{category && (
{category}
)}
{plugins.map(plugin => (
selectPlugin({
selectedPlugin: plugin.id,
selectedApp: client.id,
deepLinkPayload: null,
})
}
plugin={plugin}
app={client.query.app}
onFavorite={() => onFavorite(plugin.id)}
starred={starred}
/>
))}
));
}
renderClientPlugins(client?: Client) {
if (!client) {
return null;
}
const onFavorite = (plugin: string) => {
this.props.starPlugin({
selectedApp: client.query.app,
selectedPlugin: plugin,
});
};
const allPlugins = Array.from(this.props.clientPlugins.values()).filter(
(p: typeof FlipperPlugin) => client.plugins.indexOf(p.id) > -1,
);
const favoritePlugins: FlipperPlugins = getFavoritePlugins(
allPlugins,
this.props.userStarredPlugins[client.query.app],
true,
);
const showAllPlugins =
this.state.showAllPlugins ||
favoritePlugins.length === 0 ||
// If the plugin is part of the hidden section, make sure sidebar is expanded
(client.plugins.includes(this.props.selectedPlugin!) &&
!favoritePlugins.find(
plugin => plugin.id === this.props.selectedPlugin,
));
return (
<>
{favoritePlugins.length === 0 ? (
Star your favorite plugins!
) : (
<>
{this.renderPluginsByCategory(
client,
favoritePlugins,
true,
onFavorite,
)}
this.setState(state => ({
...state,
showAllPlugins: !state.showAllPlugins,
}))
}>
{showAllPlugins ? 'Show less' : 'Show more'}
>
)}
{showAllPlugins
? this.renderPluginsByCategory(
client,
getFavoritePlugins(
allPlugins,
this.props.userStarredPlugins[client.query.app],
false,
),
false,
onFavorite,
)
: null}
>
);
}
renderNotificationsEntry() {
if (GK.get('flipper_disable_notifications')) {
return null;
}
const active = isStaticViewActive(
this.props.staticView,
NotificationScreen,
);
return (
this.props.setStaticView(NotificationScreen)}
style={{
borderTop: `1px solid ${colors.blackAlpha10}`,
}}>
0 ? 'bell' : 'bell-null'}
isActive={active}
/>
Notifications
);
}
}
function isStaticViewActive(
current: StaticView,
selected: StaticView,
): boolean {
return current && selected && current === selected;
}
function getFavoritePlugins(
allPlugins: FlipperPlugins,
starredPlugins: undefined | string[],
favorite: boolean,
): FlipperPlugins {
if (!starredPlugins || !starredPlugins.length) {
return favorite ? [] : allPlugins;
}
return allPlugins.filter(plugin => {
const idx = starredPlugins.indexOf(plugin.id);
return idx === -1 ? !favorite : favorite;
});
}
function groupPluginsByCategory(plugins: FlipperPlugins): PluginsByCategory {
const sortedPlugins = plugins.slice().sort(sortPluginsByName);
const byCategory: {[cat: string]: FlipperPlugins} = {};
const res: PluginsByCategory = [];
sortedPlugins.forEach(plugin => {
const category = plugin.category || '';
(byCategory[category] || (byCategory[category] = [])).push(plugin);
});
// Sort categories
Object.keys(byCategory)
.sort()
.forEach(category => {
res.push([category, byCategory[category]]);
});
return res;
}
export default connect(
({
application: {windowIsFocused},
connections: {
selectedDevice,
selectedPlugin,
selectedApp,
userStarredPlugins,
clients,
uninitializedClients,
staticView,
},
notifications: {activeNotifications, blacklistedPlugins},
plugins: {devicePlugins, clientPlugins},
}) => ({
numNotifications: (() => {
const blacklist = new Set(blacklistedPlugins);
return activeNotifications.filter(
(n: PluginNotification) => !blacklist.has(n.pluginId),
).length;
})(),
windowIsFocused,
selectedDevice,
staticView,
selectedPlugin,
selectedApp,
userStarredPlugins,
clients,
uninitializedClients,
devicePlugins,
clientPlugins,
}),
{
selectPlugin,
selectClient,
setStaticView,
setActiveSheet,
starPlugin,
},
)(MainSidebar);