-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathlink_preview.jsx
183 lines (164 loc) · 6.57 KB
/
link_preview.jsx
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
import {GitMergeIcon, GitPullRequestIcon, IssueClosedIcon, IssueOpenedIcon} from '@primer/octicons-react';
import PropTypes from 'prop-types';
import React, {useEffect, useState} from 'react';
import ReactMarkdown from 'react-markdown';
import './preview.css';
import Client from 'client';
import {getLabelFontColor} from '../../utils/styles';
import {isUrlCanPreview} from 'src/utils/github_utils';
const maxTicketDescriptionLength = 160;
export const LinkPreview = ({embed: {url}, connected}) => {
const [data, setData] = useState(null);
useEffect(() => {
const initData = async () => {
if (isUrlCanPreview(url)) {
const [owner, repo, type, number] = url.split('github.com/')[1].split('/');
let res;
switch (type) {
case 'issues':
res = await Client.getIssue(owner, repo, number);
break;
case 'pull':
res = await Client.getPullRequest(owner, repo, number);
break;
}
if (res) {
res.owner = owner;
res.repo = repo;
res.type = type;
}
setData(res);
}
};
// show is not provided for Mattermost Server < 5.28
if (!connected || data) {
return;
}
initData();
}, [connected, data, url]);
const getIconElement = () => {
const iconProps = {
size: 'small',
verticalAlign: 'text-bottom',
};
let icon;
let color;
switch (data.type) {
case 'pull':
icon = <GitPullRequestIcon {...iconProps}/>;
color = '#28a745';
if (data.state === 'closed') {
if (data.merged) {
color = '#6f42c1';
icon = <GitMergeIcon {...iconProps}/>;
} else {
color = '#cb2431';
}
}
break;
case 'issues':
color = data.state === 'open' ? '#28a745' : '#cb2431';
if (data.state === 'open') {
icon = <IssueOpenedIcon {...iconProps}/>;
} else {
icon = <IssueClosedIcon {...iconProps}/>;
}
break;
}
return (
<span style={{color}}>
{icon}
</span>
);
};
if (data) {
let date = new Date(data.created_at);
date = date.toDateString();
let description = '';
if (data.body) {
description = data.body.substring(0, maxTicketDescriptionLength).trim();
if (data.body.length > maxTicketDescriptionLength) {
description += '...';
}
}
return (
<div className='github-preview github-preview--large p-4 mt-1 mb-1'>
<div className='header'>
<span className='repo'>
{data.repo}
</span>
{' on '}
<span>{date}</span>
</div>
<div className='body d-flex'>
{/* info */}
<div className='preview-info mt-1'>
<a
href={url}
target='_blank'
rel='noopener noreferrer'
>
<h5 className='mr-1'>
<span className='pr-2'>
{ getIconElement() }
</span>
{data.title}
</h5>
<span>{'#' + data.number}</span>
</a>
<div className='markdown-text mt-1 mb-1'>
<ReactMarkdown linkTarget='_blank'>{description}</ReactMarkdown>
</div>
<div className='sub-info mt-1'>
{/* base <- head */}
{data.type === 'pull' && (
<div className='sub-info-block'>
<h6 className='mt-0 mb-1'>{'Base ← Head'}</h6>
<div className='base-head'>
<span
title={data.base.ref}
className='commit-ref'
>{data.base.ref}
</span> <span className='mx-1'>{'←'}</span>{' '}
<span
title={data.head.ref}
className='commit-ref'
>{data.head.ref}
</span>
</div>
</div>
)}
{/* Labels */}
{data.labels && data.labels.length > 0 && (
<div className='sub-info-block'>
<h6 className='mt-0 mb-1'>{'Labels'}</h6>
<div className='labels'>
{data.labels.map((label, idx) => {
return (
<span
key={`${label.name}-${idx}`}
className='label'
title={label.description}
style={{backgroundColor: '#' + label.color, color: getLabelFontColor(label.color)}}
>
<span>{label.name}</span>
</span>
);
})}
</div>
</div>
)}
</div>
</div>
</div>
</div>
);
}
return null;
};
LinkPreview.propTypes = {
embed: {
url: PropTypes.string.isRequired,
},
connected: PropTypes.bool.isRequired,
};