]> cat aescling's git repositories - mastodon.git/commitdiff
Fix frontend crash when deleting announcements (#13312)
authorThibG <thib@sitedethib.com>
Wed, 25 Mar 2020 21:39:55 +0000 (22:39 +0100)
committerGitHub <noreply@github.com>
Wed, 25 Mar 2020 21:39:55 +0000 (22:39 +0100)
Refactor and fix #13283, which only worked in some cases.

app/javascript/mastodon/features/getting_started/components/announcements.js

index ec57c41b2b0879f2d200d140a1d72ef1860f6ef4..1896994daf013bb0efdcb6e1f30d4288dae3dfa4 100644 (file)
@@ -378,6 +378,14 @@ class Announcements extends ImmutablePureComponent {
     index: 0,
   };
 
+  static getDerivedStateFromProps(props, state) {
+    if (props.announcements.size > 0 && state.index >= props.announcements.size) {
+      return { index: props.announcements.size - 1 };
+    } else {
+      return null;
+    }
+  }
+
   componentDidMount () {
     this._markAnnouncementAsRead();
   }
@@ -389,7 +397,7 @@ class Announcements extends ImmutablePureComponent {
   _markAnnouncementAsRead () {
     const { dismissAnnouncement, announcements } = this.props;
     const { index } = this.state;
-    const announcement = announcements.get(index) || announcements.get(index - 1);
+    const announcement = announcements.get(index);
     if (!announcement.get('read')) dismissAnnouncement(announcement.get('id'));
   }
 
@@ -407,7 +415,7 @@ class Announcements extends ImmutablePureComponent {
 
   render () {
     const { announcements, intl } = this.props;
-    const index = this.state.index < announcements.size ? this.state.index : announcements.size - 1;
+    const { index } = this.state;
 
     if (announcements.isEmpty()) {
       return null;