Skip to content

Commit 76a228f

Browse files
committed
Allow copying the whole file with replaced appName
1 parent 08721b6 commit 76a228f

File tree

5 files changed

+92
-38
lines changed

5 files changed

+92
-38
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { useState } from 'react'
2+
import styled from '@emotion/styled'
3+
import { Button, Popover } from 'antd'
4+
import { getFileApiURL, replaceWithProvidedAppName } from '../../utils'
5+
import { CopyOutlined } from '@ant-design/icons'
6+
import copy from 'copy-to-clipboard'
7+
8+
const popoverContentOpts = {
9+
default: 'Copy raw contents',
10+
copied: 'Copied!',
11+
}
12+
13+
const CopyFileButton = styled(
14+
({ open, version, path, packageName, appName, ...props }) => {
15+
const [popoverContent, setPopoverContent] = useState(
16+
popoverContentOpts.default
17+
)
18+
19+
if (!open) {
20+
return null
21+
}
22+
23+
return (
24+
<Popover content={popoverContent} trigger="hover">
25+
<Button
26+
{...props}
27+
type="ghost"
28+
icon={<CopyOutlined />}
29+
onBlur={() => {
30+
setPopoverContent(popoverContentOpts.default)
31+
}}
32+
onClick={() => {
33+
fetch(getFileApiURL({ packageName, version, path }))
34+
.then((response) => response.json())
35+
.then((json) => window.atob(json.content))
36+
.then((content) => replaceWithProvidedAppName(content, appName))
37+
.then((content) => copy(content))
38+
.then(() => setPopoverContent(popoverContentOpts.copied))
39+
}}
40+
/>
41+
</Popover>
42+
)
43+
}
44+
)`
45+
font-size: 13px;
46+
margin-left: 5px;
47+
`
48+
49+
export default CopyFileButton

src/components/common/Diff/DiffHeader.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import styled from '@emotion/styled'
3-
import { Tag, Button, Popover } from 'antd'
3+
import { Tag, Button, Popover, Space } from 'antd'
44
import {
55
CheckOutlined,
66
DownOutlined,
@@ -14,6 +14,7 @@ import { CopyToClipboard } from 'react-copy-to-clipboard'
1414
import DiffCommentReminder from './DiffCommentReminder'
1515
import DownloadFileButton from '../DownloadFileButton'
1616
import ViewFileButton from '../ViewFileButton'
17+
import CopyFileButton from '../CopyFileButton'
1718

1819
export const testIDs = {
1920
collapseClickableArea: 'collapseClickableArea',
@@ -98,12 +99,6 @@ const BinaryBadge = ({ open, ...props }) =>
9899

99100
const defaultIconButtonStyle = `
100101
font-size: 13px;
101-
line-height: 0;
102-
border-width: 0px;
103-
width: 22px;
104-
height: 22px;
105-
margin: 5px 0;
106-
border-radius: 50%;
107102
`
108103

109104
const CompleteDiffButton = styled(({ open, onClick, ...props }) =>
@@ -307,6 +302,13 @@ const DiffHeader = ({
307302
path={newPath}
308303
packageName={packageName}
309304
/>
305+
<CopyFileButton
306+
open={hasDiff && type !== 'delete'}
307+
version={toVersion}
308+
path={newPath}
309+
packageName={packageName}
310+
appName={appName}
311+
/>
310312
<DownloadFileButton
311313
open={!hasDiff && type !== 'delete'}
312314
version={toVersion}
Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
import React from 'react'
2-
import styled from '@emotion/styled'
32
import { Button } from 'antd'
43
import { DownloadOutlined } from '@ant-design/icons'
54
import { getBinaryFileURL } from '../../utils'
65

7-
const DownloadFileButton = styled(
8-
({ open, version, path, packageName, ...props }) => {
9-
return open ? (
10-
<Button
11-
{...props}
12-
type="ghost"
13-
shape="circle"
14-
icon={<DownloadOutlined />}
15-
target="_blank"
16-
href={getBinaryFileURL({ packageName, version, path })}
17-
/>
18-
) : null
19-
}
20-
)`
21-
color: #24292e;
22-
font-size: 12px;
23-
border-width: 0;
24-
&:hover,
25-
&:focus {
26-
color: #24292e;
27-
}
28-
`
6+
const DownloadFileButton = ({ open, version, path, packageName, ...props }) => {
7+
return open ? (
8+
<Button
9+
{...props}
10+
type="ghost"
11+
shape="circle"
12+
icon={<DownloadOutlined />}
13+
target="_blank"
14+
href={getBinaryFileURL({ packageName, version, path })}
15+
/>
16+
) : null
17+
}
2918

3019
export default DownloadFileButton

src/components/common/Settings.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { Popover, Button, Checkbox, Input, Radio } from 'antd'
33
import { SHOW_LATEST_RCS } from '../../utils'
44
import styled from '@emotion/styled'
55
import { WindowsFilled } from '@ant-design/icons'
6-
import { PACKAGE_NAMES, LANGUAGE_NAMES } from '../../constants'
6+
import {
7+
DEFAULT_APP_NAME,
8+
PACKAGE_NAMES,
9+
LANGUAGE_NAMES,
10+
} from '../../constants'
711

812
const InputContainer = styled.div({
913
marginTop: '16px',
@@ -85,7 +89,7 @@ const Settings = ({
8589
<Input
8690
value={newAppName}
8791
onChange={({ target }) => setNewAppName(target.value)}
88-
placeholder="MyAwesomeApp"
92+
placeholder={DEFAULT_APP_NAME}
8993
/>
9094
</InputContainer>
9195
<PlatformsContainer>

src/utils.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,30 @@ export const getDiffURL = ({
3333
})}/diffs/diffs/${languageDir}${fromVersion}..${toVersion}.diff`
3434
}
3535

36+
const getBranch = ({ packageName, language, version }) =>
37+
packageName === PACKAGE_NAMES.RNM
38+
? `mac/${version}`
39+
: packageName === PACKAGE_NAMES.RNW
40+
? `${language}/${version}`
41+
: version
42+
3643
// `path` must contain `RnDiffApp` prefix
3744
export const getBinaryFileURL = ({ packageName, language, version, path }) => {
38-
const branch =
39-
packageName === PACKAGE_NAMES.RNM
40-
? `mac/${version}`
41-
: packageName === PACKAGE_NAMES.RNW
42-
? `${language}/${version}`
43-
: version
45+
const branch = getBranch({ packageName, language, version })
4446

4547
return `https://github.com/${getRNDiffRepository({
4648
packageName,
4749
})}/raw/release/${branch}/${path}`
4850
}
4951

52+
export const getFileApiURL = ({ packageName, language, version, path }) => {
53+
const branch = getBranch({ packageName, language, version })
54+
55+
return `https://api.github.com/repos/${getRNDiffRepository({
56+
packageName,
57+
})}/contents/${path}?ref=${encodeURIComponent(`release/${branch}`)}`
58+
}
59+
5060
export const removeAppPathPrefix = (path, appName) =>
5161
path.replace(new RegExp(`${appName || DEFAULT_APP_NAME}/`), '')
5262

0 commit comments

Comments
 (0)