summaryrefslogtreecommitdiffstats
path: root/web_src/js/components/ContextPopup.vue
blob: 752a9a1d6b7e32cc74d37839d64ef28ea5a17fb2 (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
<script>
import {SvgIcon} from '../svg.js';
import {contrastColor} from '../utils/color.js';
import {GET} from '../modules/fetch.js';
import {emojiHTML} from '../features/emoji.js';
import {htmlEscape} from 'escape-goat';

const {appSubUrl, i18n} = window.config;

export default {
  components: {SvgIcon},
  data: () => ({
    loading: false,
    issue: null,
    i18nErrorOccurred: i18n.error_occurred,
    i18nErrorMessage: null,
  }),
  computed: {
    createdAt() {
      return new Date(this.issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'});
    },

    body() {
      const body = this.issue.body.replace(/\n+/g, ' ');
      if (body.length > 85) {
        return `${body.substring(0, 85)}…`;
      }
      return body;
    },

    icon() {
      if (this.issue.pull_request !== null) {
        if (this.issue.pull_request.merged === true) {
          return 'octicon-git-merge'; // Merged PR
        }

        if (this.issue.state === 'closed') {
          return 'octicon-git-pull-request-closed'; // Closed PR
        }

        if (this.issue.pull_request.draft === true) {
          return 'octicon-git-pull-request-draft'; // WIP PR
        }

        return 'octicon-git-pull-request'; // Open PR
      }

      if (this.issue.state === 'closed') {
        return 'octicon-issue-closed'; // Closed issue
      }

      return 'octicon-issue-opened'; // Open issue
    },

    color() {
      if (this.issue.pull_request !== null) {
        if (this.issue.pull_request.merged === true) {
          return 'purple'; // Merged PR
        }

        if (this.issue.pull_request.draft === true && this.issue.state === 'open') {
          return 'grey'; // WIP PR
        }
      }

      if (this.issue.state === 'closed') {
        return 'red'; // Closed issue
      }

      return 'green'; // Open issue
    },

    labels() {
      return this.issue.labels.map((label) => ({
        name: htmlEscape(label.name).replaceAll(/:[-+\w]+:/g, (emoji) => {
          return emojiHTML(emoji.substring(1, emoji.length - 1));
        }),
        color: `#${label.color}`,
        textColor: contrastColor(`#${label.color}`),
      }));
    },
  },
  mounted() {
    this.$refs.root.addEventListener('ce-load-context-popup', (e) => {
      const data = e.detail;
      if (!this.loading && this.issue === null) {
        this.load(data);
      }
    });
  },
  methods: {
    async load(data) {
      this.loading = true;
      this.i18nErrorMessage = null;

      try {
        const response = await GET(`${appSubUrl}/${data.owner}/${data.repo}/issues/${data.index}/info`);
        const respJson = await response.json();
        if (!response.ok) {
          this.i18nErrorMessage = respJson.message ?? i18n.network_error;
          return;
        }
        this.issue = respJson;
      } catch {
        this.i18nErrorMessage = i18n.network_error;
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>
<template>
  <div ref="root">
    <div v-if="loading" class="tw-h-12 tw-w-12 is-loading"/>
    <div v-if="!loading && issue !== null" id="issue-info-popup">
      <p><small>{{ issue.repository.full_name }} on {{ createdAt }}</small></p>
      <p><svg-icon :name="icon" :class="['text', color]"/> <strong>{{ issue.title }}</strong> #{{ issue.number }}</p>
      <p>{{ body }}</p>
      <div class="labels-list">
        <!-- eslint-disable-next-line vue/no-v-html -->
        <div v-for="label in labels" :key="label.name" class="ui label" :style="{ color: label.textColor, backgroundColor: label.color }" v-html="label.name"/>
      </div>
    </div>
    <div v-if="!loading && issue === null">
      <p><small>{{ i18nErrorOccurred }}</small></p>
      <p>{{ i18nErrorMessage }}</p>
    </div>
  </div>
</template>