All files / app/assets/javascripts/notes/components notes_app.vue

78.33% Statements 47/60
91.42% Branches 32/35
86.2% Functions 25/29
77.96% Lines 46/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321  2x                                           6x                                           28x                                                                   28x                                           29x     22x     51x   51x 35x   35x         51x 2x     49x     11x     29x     44x         33x 14x 14x 14x                           43x 16x               28x 28x 18x 1x 1x             28x         31x         28x                                   52x             1x   1x 1x       15x 15x   15x 1x   1x         15x               1x     2x         2x 2x       44x 44x                                                                                                                                                  
<script>
// eslint-disable-next-line no-restricted-imports
import { mapGetters, mapActions } from 'vuex';
import { v4 as uuidv4 } from 'uuid';
import { InternalEvents } from '~/tracking';
import { getDraft, getAutoSaveKeyFromDiscussion } from '~/lib/utils/autosave';
import highlightCurrentUser from '~/behaviors/markdown/highlight_current_user';
import TimelineEntryItem from '~/vue_shared/components/notes/timeline_entry_item.vue';
import OrderedLayout from '~/vue_shared/components/ordered_layout.vue';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import DraftNote from '~/batch_comments/components/draft_note.vue';
import { getLocationHash } from '~/lib/utils/url_utility';
import PlaceholderNote from '~/vue_shared/components/notes/placeholder_note.vue';
import PlaceholderSystemNote from '~/vue_shared/components/notes/placeholder_system_note.vue';
import SkeletonLoadingContainer from '~/vue_shared/components/notes/skeleton_note.vue';
import SystemNote from '~/vue_shared/components/notes/system_note.vue';
import * as constants from '../constants';
import eventHub from '../event_hub';
import CommentForm from './comment_form.vue';
import DiscussionFilterNote from './discussion_filter_note.vue';
import NoteableDiscussion from './noteable_discussion.vue';
import NoteableNote from './noteable_note.vue';
import SidebarSubscription from './sidebar_subscription.vue';
import NotesActivityHeader from './notes_activity_header.vue';
 
export default {
  name: 'NotesApp',
  components: {
    NotesActivityHeader,
    NoteableNote,
    NoteableDiscussion,
    SystemNote,
    CommentForm,
    PlaceholderNote,
    PlaceholderSystemNote,
    SkeletonLoadingContainer,
    DiscussionFilterNote,
    OrderedLayout,
    SidebarSubscription,
    DraftNote,
    TimelineEntryItem,
    AiSummary: () => import('ee_component/notes/components/ai_summary.vue'),
  },
  mixins: [glFeatureFlagsMixin(), InternalEvents.mixin()],
  provide() {
    return {
      summarizeClientSubscriptionId: uuidv4(),
    };
  },
  props: {
    noteableData: {
      type: Object,
      required: true,
    },
    notesData: {
      type: Object,
      required: true,
    },
    notesFilters: {
      type: Array,
      required: true,
    },
    notesFilterValue: {
      type: Number,
      default: undefined,
      required: false,
    },
    shouldShow: {
      type: Boolean,
      required: false,
      default: true,
    },
    helpPagePath: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      currentFilter: null,
      renderSkeleton: !this.shouldShow,
      aiLoading: null,
      isInitialEventTriggered: false,
    };
  },
  computed: {
    ...mapGetters([
      'isNotesFetched',
      'discussions',
      'convertedDisscussionIds',
      'getNotesDataByProp',
      'isLoading',
      'isFetching',
      'commentsDisabled',
      'getNoteableData',
      'userCanReply',
      'sortDirection',
      'timelineEnabled',
    ]),
    sortDirDesc() {
      return this.sortDirection === constants.DESC;
    },
    noteableType() {
      return this.noteableData.noteableType;
    },
    allDiscussions() {
      let skeletonNotes = [];
 
      if (this.renderSkeleton || this.isLoading) {
        const prerenderedNotesCount = parseInt(this.notesData.prerenderedNotesCount, 10) || 0;
 
        skeletonNotes = new Array(prerenderedNotesCount).fill({
          isSkeletonNote: true,
        });
      }
 
      if (this.sortDirDesc) {
        return skeletonNotes.concat(this.discussions);
      }
 
      return this.discussions.concat(skeletonNotes);
    },
    canReply() {
      return this.userCanReply && !this.commentsDisabled && !this.timelineEnabled;
    },
    slotKeys() {
      return this.sortDirDesc ? ['form', 'comments'] : ['comments', 'form'];
    },
    isAppReady() {
      return !this.isLoading && !this.renderSkeleton && this.shouldShow;
    },
  },
  watch: {
    async isFetching() {
      if (!this.isFetching) {
        await this.$nextTick();
        await this.startTaskList();
        await this.checkLocationHash();
      }
    },
    shouldShow() {
      Iif (!this.isNotesFetched) {
        this.fetchNotes();
      }
 
      setTimeout(() => {
        this.renderSkeleton = !this.shouldShow;
      });
    },
    isAppReady: {
      handler(isReady) {
        if (!isReady) return;
        this.$nextTick(() => {
          window.mrTabs?.eventHub.$emit('NotesAppReady');
        });
      },
      immediate: true,
    },
  },
  mounted() {
    const { parentElement } = this.$el;
    if (parentElement && parentElement.classList.contains('js-vue-notes-event')) {
      parentElement.addEventListener('toggleAward', (event) => {
        const { awardName, noteId } = event.detail;
        this.toggleAward({ awardName, noteId });
      });
    }
 
    eventHub.$on('noteFormAddToReview', this.handleReviewTracking);
    eventHub.$on('noteFormStartReview', this.handleReviewTracking);
 
    window.addEventListener('hashchange', this.handleHashChanged);
 
    eventHub.$on('notesApp.updateIssuableConfidentiality', this.setConfidentiality);
  },
  updated() {
    this.$nextTick(() => {
      highlightCurrentUser(this.$el.querySelectorAll('.gfm-project_member'));
    });
  },
  beforeDestroy() {
    window.removeEventListener('hashchange', this.handleHashChanged);
    eventHub.$off('notesApp.updateIssuableConfidentiality', this.setConfidentiality);
    eventHub.$off('noteFormStartReview', this.handleReviewTracking);
    eventHub.$off('noteFormAddToReview', this.handleReviewTracking);
  },
  methods: {
    ...mapActions([
      'toggleAward',
      'setLastFetchedAt',
      'setTargetNoteHash',
      'toggleDiscussion',
      'expandDiscussion',
      'startTaskList',
      'convertToDiscussion',
      'setConfidentiality',
      'fetchNotes',
    ]),
    discussionIsIndividualNoteAndNotConverted(discussion) {
      return (
        discussion.individual_note &&
        !this.convertedDisscussionIds.includes(discussion.id) &&
        !this.hasDraft(discussion)
      );
    },
    handleHashChanged() {
      const noteId = this.checkLocationHash();
 
      if (noteId) {
        this.setTargetNoteHash(getLocationHash());
      }
    },
    checkLocationHash() {
      const hash = getLocationHash();
      const noteId = hash && hash.replace(/^note_/, '');
 
      if (noteId) {
        const discussion = this.discussions.find((d) => d.notes.some(({ id }) => id === noteId));
 
        Iif (discussion) {
          this.expandDiscussion({ discussionId: discussion.id });
        }
      }
 
      return noteId;
    },
    startReplying(discussionId) {
      return this.convertToDiscussion(discussionId)
        .then(this.$nextTick)
        .then(() => eventHub.$emit('startReplying', discussionId));
    },
    setAiLoading(loading) {
      this.aiLoading = loading;
    },
    handleReviewTracking(event) {
      const types = {
        noteFormStartReview: 'merge_request_click_start_review_on_overview_tab',
        noteFormAddToReview: 'merge_request_click_add_to_review_on_overview_tab',
      };
 
      if (this.shouldShow && window.mrTabs && types[event.name]) {
        this.trackEvent(types[event.name]);
      }
    },
    hasDraft(discussion) {
      const autoSaveKey = getAutoSaveKeyFromDiscussion(discussion);
      return Boolean(getDraft(autoSaveKey));
    },
  },
  systemNote: constants.SYSTEM_NOTE,
};
</script>
 
<template>
  <div v-show="shouldShow" id="notes">
    <sidebar-subscription :iid="noteableData.iid" :noteable-data="noteableData" />
    <notes-activity-header
      :notes-filters="notesFilters"
      :notes-filter-value="notesFilterValue"
      :ai-loading="aiLoading"
      @set-ai-loading="setAiLoading"
    />
    <ai-summary v-if="aiLoading !== null" :ai-loading="aiLoading" @set-ai-loading="setAiLoading" />
    <ordered-layout :slot-keys="slotKeys">
      <template #form>
        <comment-form
          v-if="!(commentsDisabled || timelineEnabled)"
          class="js-comment-form"
          :noteable-type="noteableType"
        />
      </template>
      <template #comments>
        <ul id="notes-list" class="notes main-notes-list timeline">
          <template v-for="discussion in allDiscussions">
            <skeleton-loading-container
              v-if="discussion.isSkeletonNote"
              :key="discussion.id"
              class="note-skeleton"
            />
            <timeline-entry-item v-else-if="discussion.isDraft" :key="discussion.id">
              <draft-note :draft="discussion" />
            </timeline-entry-item>
            <template v-else-if="discussion.isPlaceholderNote">
              <placeholder-system-note
                v-if="discussion.placeholderType === $options.systemNote"
                :key="discussion.id"
                :note="discussion.notes[0]"
              />
              <placeholder-note v-else :key="discussion.id" :note="discussion.notes[0]" />
            </template>
            <template v-else-if="discussionIsIndividualNoteAndNotConverted(discussion)">
              <system-note
                v-if="discussion.notes[0].system"
                :key="discussion.id"
                :note="discussion.notes[0]"
              />
              <noteable-note
                v-else
                :key="discussion.id"
                :note="discussion.notes[0]"
                :show-reply-button="canReply"
                @startReplying="startReplying(discussion.id)"
              />
            </template>
            <noteable-discussion
              v-else
              :key="discussion.id"
              :discussion="discussion"
              :render-diff-file="true"
              is-overview-tab
              :help-page-path="helpPagePath"
            />
          </template>
          <discussion-filter-note v-if="commentsDisabled" />
        </ul>
      </template>
    </ordered-layout>
  </div>
</template>