1
|
|
|
import { isEmpty } from 'ramda'; |
2
|
|
|
import React, { useEffect, useRef } from 'react'; |
3
|
|
|
import Moment from 'react-moment'; |
4
|
|
|
import PropTypes from 'prop-types'; |
5
|
|
|
import { ExternalLink } from 'react-external-link'; |
6
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
7
|
|
|
import { faCopy as copyIcon } from '@fortawesome/free-regular-svg-icons'; |
8
|
|
|
import { CopyToClipboard } from 'react-copy-to-clipboard'; |
9
|
|
|
import { shortUrlsListParamsType } from '../reducers/shortUrlsListParams'; |
10
|
|
|
import { serverType } from '../../servers/prop-types'; |
11
|
|
|
import { shortUrlType } from '../reducers/shortUrlsList'; |
12
|
|
|
import Tag from '../../tags/helpers/Tag'; |
13
|
|
|
import ShortUrlVisitsCount from './ShortUrlVisitsCount'; |
14
|
|
|
import './ShortUrlsRow.scss'; |
15
|
|
|
|
16
|
1 |
|
const propTypes = { |
17
|
|
|
refreshList: PropTypes.func, |
18
|
|
|
shortUrlsListParams: shortUrlsListParamsType, |
19
|
|
|
selectedServer: serverType, |
20
|
|
|
shortUrl: shortUrlType, |
21
|
|
|
}; |
22
|
|
|
|
23
|
1 |
|
const ShortUrlsRow = ( |
24
|
|
|
ShortUrlsRowMenu, |
25
|
|
|
colorGenerator, |
26
|
|
|
useStateFlagTimeout |
27
|
|
|
) => { |
28
|
7 |
|
const ShortUrlsRowComp = ({ shortUrl, selectedServer, refreshList, shortUrlsListParams }) => { |
29
|
8 |
|
const [ copiedToClipboard, setCopiedToClipboard ] = useStateFlagTimeout(); |
30
|
8 |
|
const [ active, setActive ] = useStateFlagTimeout(false, 500); |
31
|
8 |
|
const isFirstRun = useRef(true); |
32
|
|
|
|
33
|
8 |
|
const renderTags = (tags) => { |
34
|
8 |
|
if (isEmpty(tags)) { |
35
|
1 |
|
return <i className="indivisible"><small>No tags</small></i>; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
const selectedTags = shortUrlsListParams.tags || []; |
39
|
|
|
|
40
|
7 |
|
return tags.map((tag) => ( |
41
|
14 |
|
<Tag |
42
|
|
|
colorGenerator={colorGenerator} |
43
|
|
|
key={tag} |
44
|
|
|
text={tag} |
45
|
|
|
onClick={() => refreshList({ tags: [ ...selectedTags, tag ] })} |
46
|
|
|
/> |
47
|
|
|
)); |
48
|
|
|
}; |
49
|
|
|
|
50
|
8 |
|
useEffect(() => { |
51
|
2 |
|
if (isFirstRun.current) { |
52
|
|
|
isFirstRun.current = false; |
53
|
|
|
} else { |
54
|
|
|
setActive(true); |
55
|
|
|
} |
56
|
|
|
}, [ shortUrl.visitsCount ]); |
57
|
|
|
|
58
|
8 |
|
return ( |
59
|
|
|
<tr className="short-urls-row"> |
60
|
|
|
<td className="indivisible short-urls-row__cell" data-th="Created at: "> |
61
|
|
|
<Moment format="YYYY-MM-DD HH:mm">{shortUrl.dateCreated}</Moment> |
62
|
|
|
</td> |
63
|
|
|
<td className="short-urls-row__cell" data-th="Short URL: "> |
64
|
|
|
<span className="indivisible short-urls-row__cell--relative"> |
65
|
|
|
<ExternalLink href={shortUrl.shortUrl} /> |
66
|
|
|
<CopyToClipboard text={shortUrl.shortUrl} onCopy={setCopiedToClipboard}> |
67
|
|
|
<FontAwesomeIcon icon={copyIcon} className="ml-2 short-urls-row__copy-btn" /> |
68
|
|
|
</CopyToClipboard> |
69
|
|
|
<span className="badge badge-warning short-urls-row__copy-hint" hidden={!copiedToClipboard}> |
70
|
|
|
Copied short URL! |
71
|
|
|
</span> |
72
|
|
|
</span> |
73
|
|
|
</td> |
74
|
|
|
<td className="short-urls-row__cell short-urls-row__cell--break" data-th="Long URL: "> |
75
|
|
|
<ExternalLink href={shortUrl.longUrl} /> |
76
|
|
|
</td> |
77
|
|
|
<td className="short-urls-row__cell" data-th="Tags: ">{renderTags(shortUrl.tags)}</td> |
78
|
|
|
<td className="short-urls-row__cell text-md-right" data-th="Visits: "> |
79
|
|
|
<ShortUrlVisitsCount |
80
|
|
|
visitsCount={shortUrl.visitsCount} |
81
|
|
|
shortUrl={shortUrl} |
82
|
|
|
selectedServer={selectedServer} |
83
|
|
|
active={active} |
84
|
|
|
/> |
85
|
|
|
</td> |
86
|
|
|
<td className="short-urls-row__cell"> |
87
|
|
|
<ShortUrlsRowMenu selectedServer={selectedServer} shortUrl={shortUrl} /> |
88
|
|
|
</td> |
89
|
|
|
</tr> |
90
|
|
|
); |
91
|
|
|
}; |
92
|
|
|
|
93
|
7 |
|
ShortUrlsRowComp.propTypes = propTypes; |
94
|
|
|
|
95
|
7 |
|
return ShortUrlsRowComp; |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
export default ShortUrlsRow; |
99
|
|
|
|