Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent fetching topics list if we already have them #4351

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/autocomplete/TopicAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { connect } from '../react-redux';
import { getTopicsForNarrow } from '../selectors';
import { Popup, RawLabel, Touchable } from '../common';
import AnimatedScaleComponent from '../animation/AnimatedScaleComponent';
import { fetchTopicsForStream } from '../topics/topicActions';

const styles = createStyleSheet({
topic: {
Expand All @@ -31,6 +32,23 @@ type Props = $ReadOnly<{|
|}>;

class TopicAutocomplete extends PureComponent<Props> {
componentDidMount() {
const { dispatch, narrow } = this.props;
// The following should be sufficient to ensure we're up-to-date
// with the complete list of topics at all times that we need to
// be:
//
// - When we first expect to see the list, fetch all topics for
// the stream.
//
// - Afterwards, update the list when a new message arrives, if it
// introduces a new topic.
//
// The latter is already taken care of (see handling of
// EVENT_NEW_MESSAGE in topicsReducer). So, do the former here.
dispatch(fetchTopicsForStream(narrow));
}

render() {
const { isFocused, topics, text, onAutocomplete } = this.props;

Expand Down
10 changes: 1 addition & 9 deletions src/compose/ComposeBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ import type {
} from '../types';
import { connect } from '../react-redux';
import { withGetText } from '../boot/TranslationProvider';
import {
addToOutbox,
draftUpdate,
fetchTopicsForStream,
sendTypingStart,
sendTypingStop,
} from '../actions';
import { addToOutbox, draftUpdate, sendTypingStart, sendTypingStop } from '../actions';
import * as api from '../api';
import { FloatingActionButton, Input } from '../common';
import { showErrorAlert } from '../utils/info';
Expand Down Expand Up @@ -287,13 +281,11 @@ class ComposeBox extends PureComponent<Props, State> {
};

handleTopicFocus = () => {
const { dispatch, narrow } = this.props;
this.setState({
isTopicFocused: true,
isFocused: true,
isMenuExpanded: false,
});
dispatch(fetchTopicsForStream(narrow));
};

handleTopicBlur = () => {
Expand Down
1 change: 1 addition & 0 deletions src/topics/topicActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const fetchTopicsForStream = (narrow: Narrow) => async (
const streamName = streamNameOfNarrow(narrow);

const streams = getStreams(state);
// TODO (#4333): Look for the stream by its ID, not its name.
const stream = streams.find(sub => streamName === sub.name);
if (!stream) {
return;
Expand Down
5 changes: 5 additions & 0 deletions src/topics/topicSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const getTopicsForNarrow: Selector<string[], Narrow> = createSelector(
}
const streamName = streamNameOfNarrow(narrow);

// TODO (#4333): Look for the stream by its ID, not its name. One
// expected consequence of the current code is that
// `TopicAutocomplete` would stop showing any topics, if someone
// changed the stream name while you were looking at
// `TopicAutocomplete`.
const stream = streams.find(x => x.name === streamName);
if (!stream || !topics[stream.stream_id]) {
return NULL_ARRAY;
Expand Down