|
1 | | -<script lang="ts"> |
| 1 | +<script lang="ts" setup> |
2 | 2 | import {SvgIcon} from '../svg.ts'; |
3 | 3 | import {GET} from '../modules/fetch.ts'; |
| 4 | +import {computed, onMounted, ref} from 'vue'; |
4 | 5 |
|
5 | 6 | const {appSubUrl, i18n} = window.config; |
6 | 7 |
|
7 | | -export default { |
8 | | - components: {SvgIcon}, |
9 | | - data: () => ({ |
10 | | - loading: false, |
11 | | - issue: null, |
12 | | - renderedLabels: '', |
13 | | - i18nErrorOccurred: i18n.error_occurred, |
14 | | - i18nErrorMessage: null, |
15 | | - }), |
16 | | - computed: { |
17 | | - createdAt() { |
18 | | - return new Date(this.issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'}); |
19 | | - }, |
| 8 | +const loading = ref(false); |
| 9 | +const issue = ref(null); |
| 10 | +const renderedLabels = ref(''); |
| 11 | +const i18nErrorOccurred = i18n.error_occurred; |
| 12 | +const i18nErrorMessage = ref(null); |
20 | 13 |
|
21 | | - body() { |
22 | | - const body = this.issue.body.replace(/\n+/g, ' '); |
23 | | - if (body.length > 85) { |
24 | | - return `${body.substring(0, 85)}…`; |
25 | | - } |
26 | | - return body; |
27 | | - }, |
| 14 | +const createdAt = computed(() => new Date(issue.value.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'})); |
| 15 | +const body = computed(() => { |
| 16 | + const body = issue.value.body.replace(/\n+/g, ' '); |
| 17 | + if (body.length > 85) { |
| 18 | + return `${body.substring(0, 85)}…`; |
| 19 | + } |
| 20 | + return body; |
| 21 | +}); |
28 | 22 |
|
29 | | - icon() { |
30 | | - if (this.issue.pull_request !== null) { |
31 | | - if (this.issue.state === 'open') { |
32 | | - if (this.issue.pull_request.draft === true) { |
33 | | - return 'octicon-git-pull-request-draft'; // WIP PR |
34 | | - } |
35 | | - return 'octicon-git-pull-request'; // Open PR |
36 | | - } else if (this.issue.pull_request.merged === true) { |
37 | | - return 'octicon-git-merge'; // Merged PR |
38 | | - } |
39 | | - return 'octicon-git-pull-request'; // Closed PR |
40 | | - } else if (this.issue.state === 'open') { |
41 | | - return 'octicon-issue-opened'; // Open Issue |
42 | | - } |
43 | | - return 'octicon-issue-closed'; // Closed Issue |
44 | | - }, |
| 23 | +type Issue = {id: number; title: string; state: 'open' | 'closed'; pull_request?: {draft: boolean; merged: boolean}}; |
45 | 24 |
|
46 | | - color() { |
47 | | - if (this.issue.pull_request !== null) { |
48 | | - if (this.issue.pull_request.draft === true) { |
49 | | - return 'grey'; // WIP PR |
50 | | - } else if (this.issue.pull_request.merged === true) { |
51 | | - return 'purple'; // Merged PR |
52 | | - } |
53 | | - } |
54 | | - if (this.issue.state === 'open') { |
55 | | - return 'green'; // Open Issue |
| 25 | +function getIssueIcon(issue: Issue) { |
| 26 | + if (issue.pull_request) { |
| 27 | + if (issue.state === 'open') { |
| 28 | + if (issue.pull_request.draft === true) { |
| 29 | + return 'octicon-git-pull-request-draft'; // WIP PR |
56 | 30 | } |
57 | | - return 'red'; // Closed Issue |
58 | | - }, |
59 | | - }, |
60 | | - mounted() { |
61 | | - this.$refs.root.addEventListener('ce-load-context-popup', (e) => { |
62 | | - const data = e.detail; |
63 | | - if (!this.loading && this.issue === null) { |
64 | | - this.load(data); |
65 | | - } |
66 | | - }); |
67 | | - }, |
68 | | - methods: { |
69 | | - async load(data) { |
70 | | - this.loading = true; |
71 | | - this.i18nErrorMessage = null; |
| 31 | + return 'octicon-git-pull-request'; // Open PR |
| 32 | + } else if (issue.pull_request.merged === true) { |
| 33 | + return 'octicon-git-merge'; // Merged PR |
| 34 | + } |
| 35 | + return 'octicon-git-pull-request'; // Closed PR |
| 36 | + } else if (issue.state === 'open') { |
| 37 | + return 'octicon-issue-opened'; // Open Issue |
| 38 | + } |
| 39 | + return 'octicon-issue-closed'; // Closed Issue |
| 40 | +} |
72 | 41 |
|
73 | | - try { |
74 | | - const response = await GET(`${appSubUrl}/${data.owner}/${data.repo}/issues/${data.index}/info`); // backend: GetIssueInfo |
75 | | - const respJson = await response.json(); |
76 | | - if (!response.ok) { |
77 | | - this.i18nErrorMessage = respJson.message ?? i18n.network_error; |
78 | | - return; |
79 | | - } |
80 | | - this.issue = respJson.convertedIssue; |
81 | | - this.renderedLabels = respJson.renderedLabels; |
82 | | - } catch { |
83 | | - this.i18nErrorMessage = i18n.network_error; |
84 | | - } finally { |
85 | | - this.loading = false; |
86 | | - } |
87 | | - }, |
88 | | - }, |
89 | | -}; |
| 42 | +function getIssueColor(issue: Issue) { |
| 43 | + if (issue.pull_request) { |
| 44 | + if (issue.pull_request.draft === true) { |
| 45 | + return 'grey'; // WIP PR |
| 46 | + } else if (issue.pull_request.merged === true) { |
| 47 | + return 'purple'; // Merged PR |
| 48 | + } |
| 49 | + } |
| 50 | + if (issue.state === 'open') { |
| 51 | + return 'green'; // Open Issue |
| 52 | + } |
| 53 | + return 'red'; // Closed Issue |
| 54 | +} |
| 55 | +
|
| 56 | +const root = ref<HTMLElement | null>(null); |
| 57 | +
|
| 58 | +onMounted(() => { |
| 59 | + root.value.addEventListener('ce-load-context-popup', (e: CustomEvent) => { |
| 60 | + const data = e.detail; |
| 61 | + if (!loading.value && issue.value === null) { |
| 62 | + load(data); |
| 63 | + } |
| 64 | + }); |
| 65 | +}); |
| 66 | +
|
| 67 | +async function load(data) { |
| 68 | + loading.value = true; |
| 69 | + i18nErrorMessage.value = null; |
| 70 | +
|
| 71 | + try { |
| 72 | + const response = await GET(`${appSubUrl}/${data.owner}/${data.repo}/issues/${data.index}/info`); // backend: GetIssueInfo |
| 73 | + const respJson = await response.json(); |
| 74 | + if (!response.ok) { |
| 75 | + i18nErrorMessage.value = respJson.message ?? i18n.network_error; |
| 76 | + return; |
| 77 | + } |
| 78 | + issue.value = respJson.convertedIssue; |
| 79 | + renderedLabels.value = respJson.renderedLabels; |
| 80 | + } catch { |
| 81 | + i18nErrorMessage.value = i18n.network_error; |
| 82 | + } finally { |
| 83 | + loading.value = false; |
| 84 | + } |
| 85 | +} |
90 | 86 | </script> |
| 87 | + |
91 | 88 | <template> |
92 | 89 | <div ref="root"> |
93 | 90 | <div v-if="loading" class="tw-h-12 tw-w-12 is-loading"/> |
94 | 91 | <div v-if="!loading && issue !== null" class="tw-flex tw-flex-col tw-gap-2"> |
95 | 92 | <div class="tw-text-12">{{ issue.repository.full_name }} on {{ createdAt }}</div> |
96 | 93 | <div class="flex-text-block"> |
97 | | - <svg-icon :name="icon" :class="['text', color]"/> |
| 94 | + <svg-icon :name="getIssueIcon(issue)" :class="['text', getIssueColor(issue)]"/> |
98 | 95 | <span class="issue-title tw-font-semibold tw-break-anywhere"> |
99 | 96 | {{ issue.title }} |
100 | 97 | <span class="index">#{{ issue.number }}</span> |
|
0 commit comments