Files
flipper/src/plugins/navigation/index.js
Benjamin Elo ea8a6546c9 Added providers to the auto complete sheet
Summary:
Okay so the main changes here are integrating the providers into the auto complete sheet and getting the search bar to work with it also.

For instance, in the search bar, I want to update the value string to whatever the user has highlighted in the auto complete sheet, without executing a new query. So thus, I had to create a new state variable in the search bar component for this.

I've also moved the custom hook into its own file to keep the component short in size. It had to be mainly rewritten to support providers instead of only bookmarks. Same goes for the entire AutoCompleteSheet component.

The bookmarksProvider is stored in the persisted state as to not regenerate every-time on render. It is only updated if the bookmarks are updated which are also now stored in the persistedState for the same reason.

Lastly, a DefaultProvider object was also made for the initial persisted state object.

Reviewed By: danielbuechele

Differential Revision: D16581644

fbshipit-source-id: 88723a4081d96250f723a4cd7b1ade101bf3e8f3
2019-08-02 01:58:02 -07:00

166 lines
4.3 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
* @flow strict-local
*/
import {FlipperPlugin} from 'flipper';
import {
BookmarksSidebar,
SaveBookmarkDialog,
SearchBar,
Timeline,
ScrollableFlexColumn,
} from './components';
import {
removeBookmark,
readBookmarksFromDB,
writeBookmarkToDB,
} from './util/indexedDB';
import {
bookmarksToAutoCompleteProvider,
DefaultProvider,
} from './util/autoCompleteProvider';
import type {
State,
PersistedState,
Bookmark,
NavigationEvent,
} from './flow-types';
export default class extends FlipperPlugin<State, {}, PersistedState> {
static title = 'Navigation';
static id = 'Navigation';
static icon = 'directions';
static keyboardActions = ['clear'];
static defaultPersistedState: PersistedState = {
navigationEvents: [],
bookmarks: new Map<string, Bookmark>(),
bookmarksProvider: new DefaultProvider(),
};
state = {
shouldShowSaveBookmarkDialog: false,
saveBookmarkURI: null,
};
static persistedStateReducer = (
persistedState: PersistedState,
method: string,
payload: NavigationEvent,
): $Shape<PersistedState> => {
switch (method) {
case 'nav_event':
return {
...persistedState,
navigationEvents: [
{
uri: payload.uri === undefined ? null : payload.uri,
date: payload.date || new Date(),
},
...persistedState.navigationEvents,
],
};
default:
return {
...persistedState,
};
}
};
componentDidMount = () => {
readBookmarksFromDB().then(bookmarks => {
this.props.setPersistedState({
bookmarks: bookmarks,
bookmarksProvider: bookmarksToAutoCompleteProvider(bookmarks),
});
});
};
onKeyboardAction = (action: string) => {
if (action === 'clear') {
this.props.setPersistedState({navigationEvents: []});
}
};
navigateTo = (query: string) => {
this.getDevice().then(device => {
device.navigateToLocation(query);
});
};
onFavorite = (uri: string) => {
this.setState({shouldShowSaveBookmarkDialog: true, saveBookmarkURI: uri});
};
addBookmark = (bookmark: Bookmark) => {
const newBookmark = {
uri: bookmark.uri,
commonName:
bookmark.commonName.length > 0 ? bookmark.commonName : bookmark.uri,
};
writeBookmarkToDB(newBookmark);
const newMapRef = this.props.persistedState.bookmarks;
newMapRef.set(newBookmark.uri, newBookmark);
this.props.setPersistedState({
bookmarks: newMapRef,
bookmarksProvider: bookmarksToAutoCompleteProvider(newMapRef),
});
};
removeBookmark = (uri: string) => {
removeBookmark(uri);
const newMapRef = this.props.persistedState.bookmarks;
newMapRef.delete(uri);
this.props.setPersistedState({
bookmarks: newMapRef,
bookmarksProvider: bookmarksToAutoCompleteProvider(newMapRef),
});
};
render() {
const {saveBookmarkURI, shouldShowSaveBookmarkDialog} = this.state;
const {
bookmarks,
bookmarksProvider,
navigationEvents,
} = this.props.persistedState;
const autoCompleteProviders = [bookmarksProvider];
return (
<ScrollableFlexColumn>
<SearchBar
providers={autoCompleteProviders}
bookmarks={bookmarks}
onNavigate={this.navigateTo}
onFavorite={this.onFavorite}
/>
<Timeline
bookmarks={bookmarks}
events={navigationEvents}
onNavigate={this.navigateTo}
onFavorite={this.onFavorite}
/>
<BookmarksSidebar bookmarks={bookmarks} onNavigate={this.navigateTo} />
<SaveBookmarkDialog
shouldShow={shouldShowSaveBookmarkDialog}
uri={saveBookmarkURI}
onHide={() => this.setState({shouldShowSaveBookmarkDialog: false})}
edit={
saveBookmarkURI != null ? bookmarks.has(saveBookmarkURI) : false
}
onSubmit={this.addBookmark}
onRemove={this.removeBookmark}
/>
</ScrollableFlexColumn>
);
}
}
/* @scarf-info: do not remove, more info: https://fburl.com/scarf */
/* @scarf-generated: flipper-plugin index.js.template 0bfa32e5-fb15-4705-81f8-86260a1f3f8e */