MCPcopy Create free account
hub / github.com/CommE2E/comm / componentDidUpdate

Method componentDidUpdate

native/chat/chat-list.react.js:136–221  ·  view source on GitHub ↗
(prevProps: Props)

Source from the content-addressed store, hash-verified

134 }
135
136 componentDidUpdate(prevProps: Props) {
137 const { flatList } = this;
138 if (!flatList || this.props.data === prevProps.data) {
139 return;
140 }
141
142 if (this.props.data.length < prevProps.data.length) {
143 // This should only happen due to MessageStorePruner,
144 // which will only prune a thread when it is off-screen
145 flatList.scrollToOffset({ offset: 0, animated: false });
146 return;
147 }
148
149 const { scrollPos } = this;
150 const { viewerID } = this.props;
151
152 let curDataIndex = 0,
153 prevDataIndex = 0,
154 heightSoFar = 0;
155 let adjustScrollPos = 0,
156 newLocalMessage = false,
157 newRemoteMessageCount = 0;
158 while (prevDataIndex < prevProps.data.length && heightSoFar <= scrollPos) {
159 const prevItem = prevProps.data[prevDataIndex];
160 invariant(prevItem, 'prevItem should exist');
161 const prevItemKey = chatMessageItemKey(prevItem);
162 const prevItemHeight = chatMessageItemHeight(prevItem);
163
164 let curItem = this.props.data[curDataIndex];
165 while (curItem) {
166 const curItemKey = chatMessageItemKey(curItem);
167 if (curItemKey === prevItemKey) {
168 break;
169 }
170
171 if (curItemKey.startsWith(localIDPrefix)) {
172 newLocalMessage = true;
173 } else if (chatMessageItemHasNonViewerMessage(curItem, viewerID)) {
174 newRemoteMessageCount++;
175 }
176 adjustScrollPos += chatMessageItemHeight(curItem);
177
178 curDataIndex++;
179 curItem = this.props.data[curDataIndex];
180 }
181 if (!curItem) {
182 // The only case in which we would expect the length of data to
183 // decrease, but find that an item was removed, is if the start
184 // of the chat is reached. In that case, the spinner at the top
185 // will no longer be rendered. We break here as we expect the
186 // spinner to be the last item.
187 if (prevItemKey === 'loader') {
188 break;
189 }
190 console.log(
191 `items not removed from ChatList, but ${prevItemKey} now missing`,
192 );
193 return;

Callers

nothing calls this directly

Calls 5

toggleNewMessagesPillMethod · 0.95
chatMessageItemKeyFunction · 0.90
chatMessageItemHeightFunction · 0.90
logMethod · 0.45

Tested by

no test coverage detected