Fixes an issue with no database selected

Summary:
^

Not exactly sure how to reproduce the issue. Having said that:
- A database id is a non-zero number (1..n)
- If there's no selected database and/or there's no databases, then selectedDatabase is '0', which is an invalid database id.
- It is safer to check if the selected database id falls within bounds before attempting to obtain the tables from it.

From Logview, if the database id is '0', which is invalid, then we attempt to access a database at index -1 (database[selectedDatabase - 1]) which is definitely invalid. The returned object is undefined and hence the error.

Changelog: Fixes an issue on the databases plugin when there is no selected database.

Reviewed By: mweststrate

Differential Revision: D35810827

fbshipit-source-id: 4c9f112eebcd0aa3fcd5df316749f48f3922381c
This commit is contained in:
Lorenzo Blasa
2022-04-22 03:59:00 -07:00
committed by Facebook GitHub Bot
parent e7c56045f5
commit 0327282313

View File

@@ -116,6 +116,8 @@ export function plugin(client: PluginClient<Events, Methods>) {
(Object.values(databases)[0] ? Object.values(databases)[0].id : 0);
const selectedTable =
state.selectedDatabaseTable &&
selectedDatabase > 0 &&
databases.length >= selectedDatabase &&
databases[selectedDatabase - 1].tables.includes(
state.selectedDatabaseTable,
)