Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: replace RnDiffApp with entered AppName #111

Merged
27 changes: 21 additions & 6 deletions src/components/common/DiffViewer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useCallback } from 'react'
import styled from 'styled-components'
import { Alert } from 'antd'
import { parseDiff, withChangeSelect } from 'react-diff-view'
Expand All @@ -8,7 +8,6 @@ import DiffSection from './Diff/DiffSection'
import DiffLoading from './Diff/DiffLoading'
import UsefulContentSection from './UsefulContentSection'
import ViewStyleOptions from './Diff/DiffViewStyleOptions'

import CompletedFilesCounter from './CompletedFilesCounter'
import { AppNameWarning } from './AppNameWarning'

Expand All @@ -24,7 +23,8 @@ const DiffViewer = ({
fromVersion,
toVersion,
selectedChanges,
onToggleChangeSelection
onToggleChangeSelection,
appName
}) => {
const [isLoading, setLoading] = useState(true)
const [diff, setDiff] = useState(null)
Expand Down Expand Up @@ -62,6 +62,16 @@ const DiffViewer = ({
localStorage.setItem('viewStyle', newViewStyle)
}

const replaceAppName = useCallback(
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
text => {
if (!appName) return text
return text
.replace(/RnDiffApp/g, appName)
.replace(/rndiffapp/g, appName.toLowerCase())
},
[appName]
)

useEffect(() => {
if (!showDiff) {
return
Expand All @@ -75,7 +85,7 @@ const DiffViewer = ({
).text()

setDiff(
parseDiff(response).sort(({ newPath }) =>
parseDiff(replaceAppName(response)).sort(({ newPath }) =>
newPath.includes('package.json') ? -1 : 1
)
)
Expand All @@ -85,8 +95,13 @@ const DiffViewer = ({
setLoading(false)
}

fetchDiff()
}, [fromVersion, showDiff, toVersion])
const debounce = setTimeout(() => {
fetchDiff()
}, 750)
return () => {
clearTimeout(debounce)
}
}, [appName, fromVersion, replaceAppName, showDiff, toVersion])

if (!showDiff) {
return null
Expand Down
30 changes: 23 additions & 7 deletions src/components/common/Settings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React, { useState } from 'react'
import { Popover, Button, Checkbox } from 'antd'
import { Popover, Button, Checkbox, Input } from 'antd'
import { SHOW_LATEST_RCS } from '../../utils'
import styled from 'styled-components'

const Settings = ({ handleSettingsChange }) => {
const InputContainer = styled.div({
marginTop: '16px'
})
const Settings = ({ handleSettingsChange, appName, setAppName }) => {
const [popoverVisibility, setVisibility] = useState(false)

const handleClickChange = visibility => setVisibility(visibility)
Expand All @@ -14,11 +18,23 @@ const Settings = ({ handleSettingsChange }) => {
<Popover
placement="bottomRight"
content={
<Checkbox.Group onChange={updateCheckboxValues}>
<div>
<Checkbox value={SHOW_LATEST_RCS}>{SHOW_LATEST_RCS}</Checkbox>
</div>
</Checkbox.Group>
<>
<Checkbox.Group onChange={updateCheckboxValues}>
<div>
<Checkbox value={SHOW_LATEST_RCS}>{SHOW_LATEST_RCS}</Checkbox>
</div>
</Checkbox.Group>
<InputContainer>
<h4>What's your app name?</h4>
<Input
value={appName}
onChange={e => {
setAppName(e.target.value)
}}
placeholder="MyAwesomeApp"
/>
</InputContainer>
</>
}
trigger="click"
visible={popoverVisibility}
Expand Down
8 changes: 7 additions & 1 deletion src/components/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const Home = () => {
const [settings, setSettings] = useState({
[`${SHOW_LATEST_RCS}`]: false
})
const [appName, setAppName] = useState('')
lucasbento marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
if (process.env.NODE_ENV === 'production') {
Expand Down Expand Up @@ -105,7 +106,11 @@ const Home = () => {
Star
</StarButton>

<Settings handleSettingsChange={handleSettingsChange} />
<Settings
handleSettingsChange={handleSettingsChange}
appName={appName}
setAppName={setAppName}
/>
</TitleContainer>

<VersionSelector
Expand All @@ -118,6 +123,7 @@ const Home = () => {
showDiff={showDiff}
fromVersion={fromVersion}
toVersion={toVersion}
appName={appName}
/>
</Page>
)
Expand Down