Skip to content

Commit

Permalink
Fix issue with AppName stripping incorrectly.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
blakef committed Feb 26, 2024
1 parent 3ebded0 commit 7ef8fb7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
19 changes: 19 additions & 0 deletions src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PACKAGE_NAMES } from '../constants'
import '../releases/__mocks__/index'
import {
getVersionsContentInDiff,
removeAppPathPrefix,
replaceAppDetails,
getChangelogURL,
} from '../utils'
Expand Down Expand Up @@ -132,3 +133,21 @@ describe('replaceAppDetails ', () => {
}
)
})

describe('removeAppPathPrefix', () => {
test.each([
['RnDiffApp/package.json', 'package.json'],
['RnDiffApp/RnDiffApp.ts', 'RnDiffApp.ts'],
])('removeAppPathPrefix("%s") -> "%s"', (path, newPath) => {
expect(removeAppPathPrefix(path)).toEqual(newPath)
})

test('removeAppPathPrefix custom AppName', () => {
expect(removeAppPathPrefix('RnDiffApp/package.json', '')).toEqual(
'RnDiffApp/package.json'
)
expect(removeAppPathPrefix('Foobar/package.json', 'Foobar')).toEqual(
'package.json'
)
})
})
32 changes: 15 additions & 17 deletions src/components/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useDeferredValue } from 'react'
import React, { useState, useEffect, useReducer, useDeferredValue } from 'react'
import styled from '@emotion/styled'
import { ThemeProvider } from '@emotion/react'
import { Card, Input, Typography, ConfigProvider, theme } from 'antd'
Expand Down Expand Up @@ -125,6 +125,12 @@ const StarButton = styled(({ className, ...props }: StarButtonProps) => (
// will have dark mode automatically if they've selected it previously.
const useDarkModeState = createPersistedState('darkMode')

// The value returns to `defaultValue` when an empty string is entered,
// this makes our down-tree props behaviour simpler without having to swap
// empty values for the default.
const useDefaultState = (defaultValue: string) =>
useReducer((_, v: string) => (v === '' ? defaultValue : v), defaultValue)

const Home = () => {
const { packageName: defaultPackageName, isPackageNameDefinedInURL } =
useGetPackageNameFromURL()
Expand All @@ -138,8 +144,8 @@ const Home = () => {
[`${SHOW_LATEST_RCS}`]: false,
})

const [appName, setAppName] = useState<string>('')
const [appPackage, setAppPackage] = useState<string>('')
const [appName, setAppName] = useDefaultState(DEFAULT_APP_NAME)
const [appPackage, setAppPackage] = useDefaultState(DEFAULT_APP_PACKAGE)

// Avoid UI lag when typing.
const deferredAppName = useDeferredValue(appName)
Expand Down Expand Up @@ -276,8 +282,8 @@ const Home = () => {
<Input
size="large"
placeholder={DEFAULT_APP_NAME}
value={appName}
onChange={({ target }) => setAppName((value) => target.value)}
value={appName === DEFAULT_APP_NAME ? '' : appName}
onChange={({ target }) => setAppName(target.value)}
/>
</AppNameField>

Expand All @@ -289,10 +295,8 @@ const Home = () => {
<Input
size="large"
placeholder={DEFAULT_APP_PACKAGE}
value={appPackage}
onChange={({ target }) =>
setAppPackage((value) => target.value)
}
value={appPackage === DEFAULT_APP_PACKAGE ? '' : appPackage}
onChange={({ target }) => setAppPackage(target.value)}
/>
</AppPackageField>
</AppDetailsContainer>
Expand All @@ -315,14 +319,8 @@ const Home = () => {
shouldShowDiff={shouldShowDiff}
fromVersion={fromVersion}
toVersion={toVersion}
appName={
deferredAppName !== DEFAULT_APP_NAME ? deferredAppName : ''
}
appPackage={
deferredAppPackage !== DEFAULT_APP_PACKAGE
? deferredAppPackage
: ''
}
appName={deferredAppName}
appPackage={deferredAppPackage}
packageName={packageName}
language={language}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const getBinaryFileURL = ({
}

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

/**
* Replaces DEFAULT_APP_PACKAGE and DEFAULT_APP_NAME in str with custom
Expand Down

0 comments on commit 7ef8fb7

Please sign in to comment.