Skip to content

Commit 7ef8fb7

Browse files
committed
Fix issue with AppName stripping incorrectly.
When appName or appPackage are empty, set the state to the default values. This makes the down-tree props simpler to understand. Also more strictly strip the prefix AppName from the filepath to avoid edgecases.
1 parent 3ebded0 commit 7ef8fb7

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/__tests__/utils.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PACKAGE_NAMES } from '../constants'
22
import '../releases/__mocks__/index'
33
import {
44
getVersionsContentInDiff,
5+
removeAppPathPrefix,
56
replaceAppDetails,
67
getChangelogURL,
78
} from '../utils'
@@ -132,3 +133,21 @@ describe('replaceAppDetails ', () => {
132133
}
133134
)
134135
})
136+
137+
describe('removeAppPathPrefix', () => {
138+
test.each([
139+
['RnDiffApp/package.json', 'package.json'],
140+
['RnDiffApp/RnDiffApp.ts', 'RnDiffApp.ts'],
141+
])('removeAppPathPrefix("%s") -> "%s"', (path, newPath) => {
142+
expect(removeAppPathPrefix(path)).toEqual(newPath)
143+
})
144+
145+
test('removeAppPathPrefix custom AppName', () => {
146+
expect(removeAppPathPrefix('RnDiffApp/package.json', '')).toEqual(
147+
'RnDiffApp/package.json'
148+
)
149+
expect(removeAppPathPrefix('Foobar/package.json', 'Foobar')).toEqual(
150+
'package.json'
151+
)
152+
})
153+
})

src/components/pages/Home.tsx

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useDeferredValue } from 'react'
1+
import React, { useState, useEffect, useReducer, useDeferredValue } from 'react'
22
import styled from '@emotion/styled'
33
import { ThemeProvider } from '@emotion/react'
44
import { Card, Input, Typography, ConfigProvider, theme } from 'antd'
@@ -125,6 +125,12 @@ const StarButton = styled(({ className, ...props }: StarButtonProps) => (
125125
// will have dark mode automatically if they've selected it previously.
126126
const useDarkModeState = createPersistedState('darkMode')
127127

128+
// The value returns to `defaultValue` when an empty string is entered,
129+
// this makes our down-tree props behaviour simpler without having to swap
130+
// empty values for the default.
131+
const useDefaultState = (defaultValue: string) =>
132+
useReducer((_, v: string) => (v === '' ? defaultValue : v), defaultValue)
133+
128134
const Home = () => {
129135
const { packageName: defaultPackageName, isPackageNameDefinedInURL } =
130136
useGetPackageNameFromURL()
@@ -138,8 +144,8 @@ const Home = () => {
138144
[`${SHOW_LATEST_RCS}`]: false,
139145
})
140146

141-
const [appName, setAppName] = useState<string>('')
142-
const [appPackage, setAppPackage] = useState<string>('')
147+
const [appName, setAppName] = useDefaultState(DEFAULT_APP_NAME)
148+
const [appPackage, setAppPackage] = useDefaultState(DEFAULT_APP_PACKAGE)
143149

144150
// Avoid UI lag when typing.
145151
const deferredAppName = useDeferredValue(appName)
@@ -276,8 +282,8 @@ const Home = () => {
276282
<Input
277283
size="large"
278284
placeholder={DEFAULT_APP_NAME}
279-
value={appName}
280-
onChange={({ target }) => setAppName((value) => target.value)}
285+
value={appName === DEFAULT_APP_NAME ? '' : appName}
286+
onChange={({ target }) => setAppName(target.value)}
281287
/>
282288
</AppNameField>
283289

@@ -289,10 +295,8 @@ const Home = () => {
289295
<Input
290296
size="large"
291297
placeholder={DEFAULT_APP_PACKAGE}
292-
value={appPackage}
293-
onChange={({ target }) =>
294-
setAppPackage((value) => target.value)
295-
}
298+
value={appPackage === DEFAULT_APP_PACKAGE ? '' : appPackage}
299+
onChange={({ target }) => setAppPackage(target.value)}
296300
/>
297301
</AppPackageField>
298302
</AppDetailsContainer>
@@ -315,14 +319,8 @@ const Home = () => {
315319
shouldShowDiff={shouldShowDiff}
316320
fromVersion={fromVersion}
317321
toVersion={toVersion}
318-
appName={
319-
deferredAppName !== DEFAULT_APP_NAME ? deferredAppName : ''
320-
}
321-
appPackage={
322-
deferredAppPackage !== DEFAULT_APP_PACKAGE
323-
? deferredAppPackage
324-
: ''
325-
}
322+
appName={deferredAppName}
323+
appPackage={deferredAppPackage}
326324
packageName={packageName}
327325
language={language}
328326
/>

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const getBinaryFileURL = ({
7575
}
7676

7777
export const removeAppPathPrefix = (path: string, appName = DEFAULT_APP_NAME) =>
78-
path.replace(new RegExp(`${appName}/`), '')
78+
path.replace(new RegExp(`^${appName}/`), '')
7979

8080
/**
8181
* Replaces DEFAULT_APP_PACKAGE and DEFAULT_APP_NAME in str with custom

0 commit comments

Comments
 (0)