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

Update network inspector to have smarter scroll stickiness #21952

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 41 additions & 6 deletions Libraries/Inspector/NetworkOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ class NetworkOverlay extends React.Component<Props, State> {
_requestsListView: ?React.ElementRef<typeof FlatList>;
_detailScrollView: ?React.ElementRef<typeof ScrollView>;

// Metrics are used to decide when if the request list should be sticky, and
// scroll to the bottom as new network requests come in, or if the user has
// intentionally scrolled away from the bottom - to instead flash the scroll bar
// and keep the current position
_requestsListViewScrollMetrics = {
offset: 0,
visibleLength: 0,
contentLength: 0,
};

// Map of `socketId` -> `index in `this.state.requests`.
_socketIdMap = {};
// Map of `xhr._index` -> `index in `this.state.requests`.
Expand Down Expand Up @@ -121,7 +131,7 @@ class NetworkOverlay extends React.Component<Props, State> {
{
requests: this.state.requests.concat(_xhr),
},
this._scrollRequestsToBottom,
this._indicateAdditionalRequests,
);
});

Expand Down Expand Up @@ -214,7 +224,7 @@ class NetworkOverlay extends React.Component<Props, State> {
{
requests: this.state.requests.concat(_webSocket),
},
this._scrollRequestsToBottom,
this._indicateAdditionalRequests,
);
},
);
Expand Down Expand Up @@ -383,11 +393,35 @@ class NetworkOverlay extends React.Component<Props, State> {
);
}

_scrollRequestsToBottom(): void {
_indicateAdditionalRequests = (): void => {
if (this._requestsListView) {
this._requestsListView.scrollToEnd();
const distanceFromEndThreshold = LISTVIEW_CELL_HEIGHT * 2;
const {
offset,
visibleLength,
contentLength,
} = this._requestsListViewScrollMetrics;
const distanceFromEnd = contentLength - visibleLength - offset;
const isCloseToEnd = distanceFromEnd <= distanceFromEndThreshold;
if (isCloseToEnd) {
this._requestsListView.scrollToEnd();
} else {
this._requestsListView.flashScrollIndicators();
}
}
}
};

_captureRequestsListView = (listRef: ?FlatList<NetworkRequestInfo>): void => {
this._requestsListView = listRef;
};

_requestsListViewOnScroll = (e: Object): void => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about?

Suggested change
_requestsListViewOnScroll = (e: Object): void => {
_requestsListViewOnScroll = (e: Object): void => {
this._requestsListViewScrollMetrics = {
offset: e.nativeEvent.contentOffset.y,
visibleLength: e.nativeEvent.layoutMeasurement.height,
contentLength: e.nativeEvent.contentSize.height,
};

this._requestsListViewScrollMetrics.offset = e.nativeEvent.contentOffset.y;
this._requestsListViewScrollMetrics.visibleLength =
e.nativeEvent.layoutMeasurement.height;
this._requestsListViewScrollMetrics.contentLength =
e.nativeEvent.contentSize.height;
};

/**
* Popup a scrollView to dynamically show detailed information of
Expand Down Expand Up @@ -446,7 +480,8 @@ class NetworkOverlay extends React.Component<Props, State> {
</View>

<FlatList
ref={listRef => (this._requestsListView = listRef)}
ref={this._captureRequestsListView}
onScroll={this._requestsListViewOnScroll}
style={styles.listView}
data={requests}
renderItem={this._renderItem}
Expand Down