summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/jira_connect/branches/components/source_branch_dropdown.vue
blob: 0e2d8821f362e819dca87510f00106a14dc34900 (plain)
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
<script>
import { GlDropdown, GlDropdownItem, GlSearchBoxByType, GlLoadingIcon } from '@gitlab/ui';
import { __ } from '~/locale';
import { BRANCHES_PER_PAGE } from '../constants';
import getProjectQuery from '../graphql/queries/get_project.query.graphql';

export default {
  BRANCHES_PER_PAGE,
  components: {
    GlDropdown,
    GlDropdownItem,
    GlSearchBoxByType,
    GlLoadingIcon,
  },
  props: {
    selectedProject: {
      type: Object,
      required: false,
      default: null,
    },
    selectedBranchName: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      sourceBranchSearchQuery: '',
      initialSourceBranchNamesLoading: false,
      sourceBranchNamesLoading: false,
      sourceBranchNames: [],
    };
  },
  computed: {
    hasSelectedProject() {
      return Boolean(this.selectedProject);
    },
    hasSelectedSourceBranch() {
      return Boolean(this.selectedBranchName);
    },
    branchDropdownText() {
      return this.selectedBranchName || __('Select a branch');
    },
  },
  watch: {
    selectedProject: {
      immediate: true,
      async handler(selectedProject) {
        if (!selectedProject) return;

        this.initialSourceBranchNamesLoading = true;
        await this.fetchSourceBranchNames({ projectPath: selectedProject.fullPath });
        this.initialSourceBranchNamesLoading = false;
      },
    },
  },
  methods: {
    onSourceBranchSelect(branchName) {
      this.$emit('change', branchName);
    },
    onSourceBranchSearchQuery(branchSearchQuery) {
      this.branchSearchQuery = branchSearchQuery;
      this.fetchSourceBranchNames({
        projectPath: this.selectedProject.fullPath,
        searchPattern: this.branchSearchQuery,
      });
    },
    onError({ message } = {}) {
      this.$emit('error', { message });
    },
    async fetchSourceBranchNames({ projectPath, searchPattern } = {}) {
      this.sourceBranchNamesLoading = true;
      try {
        const { data } = await this.$apollo.query({
          query: getProjectQuery,
          variables: {
            projectPath,
            branchNamesLimit: this.$options.BRANCHES_PER_PAGE,
            branchNamesOffset: 0,
            branchNamesSearchPattern: searchPattern ? `*${searchPattern}*` : '*',
          },
        });

        const { branchNames, rootRef } = data?.project.repository || {};
        this.sourceBranchNames = branchNames || [];

        // Use root ref as the default selection
        if (rootRef && !this.hasSelectedSourceBranch) {
          this.onSourceBranchSelect(rootRef);
        }
      } catch (err) {
        this.onError({
          message: __('Something went wrong while fetching source branches.'),
        });
      } finally {
        this.sourceBranchNamesLoading = false;
      }
    },
  },
};
</script>

<template>
  <gl-dropdown
    :text="branchDropdownText"
    :loading="initialSourceBranchNamesLoading"
    :disabled="!hasSelectedProject"
    :class="{ 'gl-font-monospace': hasSelectedSourceBranch }"
  >
    <template #header>
      <gl-search-box-by-type
        :debounce="250"
        :value="sourceBranchSearchQuery"
        @input="onSourceBranchSearchQuery"
      />
    </template>

    <gl-loading-icon v-show="sourceBranchNamesLoading" />
    <template v-if="!sourceBranchNamesLoading">
      <gl-dropdown-item
        v-for="branchName in sourceBranchNames"
        v-show="!sourceBranchNamesLoading"
        :key="branchName"
        :is-checked="branchName === selectedBranchName"
        is-check-item
        class="gl-font-monospace"
        @click="onSourceBranchSelect(branchName)"
      >
        {{ branchName }}
      </gl-dropdown-item>
    </template>
  </gl-dropdown>
</template>