diff --git a/src/__tests__/utils.spec.ts b/src/__tests__/utils.spec.ts index d01996b3..260af063 100644 --- a/src/__tests__/utils.spec.ts +++ b/src/__tests__/utils.spec.ts @@ -2,6 +2,7 @@ import { PACKAGE_NAMES } from '../constants' import '../releases/__mocks__/index' import { getVersionsContentInDiff, + removeAppPathPrefix, replaceAppDetails, getChangelogURL, } from '../utils' @@ -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' + ) + }) +}) diff --git a/src/components/pages/Home.tsx b/src/components/pages/Home.tsx index c21ef618..5cc60482 100644 --- a/src/components/pages/Home.tsx +++ b/src/components/pages/Home.tsx @@ -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' @@ -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() @@ -138,8 +144,8 @@ const Home = () => { [`${SHOW_LATEST_RCS}`]: false, }) - const [appName, setAppName] = useState('') - const [appPackage, setAppPackage] = useState('') + const [appName, setAppName] = useDefaultState(DEFAULT_APP_NAME) + const [appPackage, setAppPackage] = useDefaultState(DEFAULT_APP_PACKAGE) // Avoid UI lag when typing. const deferredAppName = useDeferredValue(appName) @@ -276,8 +282,8 @@ const Home = () => { setAppName((value) => target.value)} + value={appName === DEFAULT_APP_NAME ? '' : appName} + onChange={({ target }) => setAppName(target.value)} /> @@ -289,10 +295,8 @@ const Home = () => { - setAppPackage((value) => target.value) - } + value={appPackage === DEFAULT_APP_PACKAGE ? '' : appPackage} + onChange={({ target }) => setAppPackage(target.value)} /> @@ -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} /> diff --git a/src/utils.ts b/src/utils.ts index 1ae109e2..a22c1711 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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