1- import React , { useState , useEffect , useDeferredValue } from 'react'
1+ import React , { useState , useEffect , useReducer , useDeferredValue } from 'react'
22import styled from '@emotion/styled'
33import { ThemeProvider } from '@emotion/react'
44import { 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.
126126const 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+
128134const 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 />
0 commit comments