Skip to content

Commit

Permalink
Keep custom path open if user selected one (#787)
Browse files Browse the repository at this point in the history
* Keep custom path open if user selected one

* Update tests to account for latest changes

* Fix linter error

---------

Co-authored-by: Bero <[email protected]>
  • Loading branch information
wojtekn and bgrgicak authored Jan 9, 2025
1 parent f8c5420 commit a0aacb7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/components/tests/add-site.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe( 'AddSite', () => {
await user.click( screen.getByRole( 'button', { name: 'Advanced settings' } ) );
await user.click( screen.getByTestId( 'select-path-button' ) );

expect( mockShowOpenFolderDialog ).toHaveBeenCalledWith( 'Choose folder for site' );
expect( mockShowOpenFolderDialog ).toHaveBeenCalledWith( 'Choose folder for site', '' );
await user.click( screen.getByRole( 'button', { name: 'Add site' } ) );

await waitFor( () => {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe( 'AddSite', () => {
await user.click( screen.getByRole( 'button', { name: 'Advanced settings' } ) );
await user.click( screen.getByTestId( 'select-path-button' ) );

expect( mockShowOpenFolderDialog ).toHaveBeenCalledWith( 'Choose folder for site' );
expect( mockShowOpenFolderDialog ).toHaveBeenCalledWith( 'Choose folder for site', '' );

await waitFor( () => {
expect( screen.getByRole( 'button', { name: 'Add site' } ) ).toBeDisabled();
Expand Down Expand Up @@ -136,7 +136,7 @@ describe( 'AddSite', () => {
await user.click( screen.getByRole( 'button', { name: 'Advanced settings' } ) );
await user.click( screen.getByTestId( 'select-path-button' ) );

expect( mockShowOpenFolderDialog ).toHaveBeenCalledWith( 'Choose folder for site' );
expect( mockShowOpenFolderDialog ).toHaveBeenCalledWith( 'Choose folder for site', '' );

await waitFor( () => {
expect( screen.getByRole( 'button', { name: 'Add site' } ) ).not.toBeDisabled();
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/use-add-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ export function useAddSite() {
);

const handlePathSelectorClick = useCallback( async () => {
const response = await getIpcApi().showOpenFolderDialog( __( 'Choose folder for site' ) );
const response = await getIpcApi().showOpenFolderDialog(
__( 'Choose folder for site' ),
sitePath
);
if ( response?.path ) {
const { path, name, isEmpty, isWordPress } = response;
setDoesPathContainWordPress( false );
setError( '' );
const pathResetToDefaultSitePath =
path === proposedSitePath.substring( 0, proposedSitePath.lastIndexOf( '/' ) );

setSitePath( pathResetToDefaultSitePath ? '' : path );
if ( siteWithPathAlreadyExists( path ) ) {
return;
Expand All @@ -50,7 +54,7 @@ export function useAddSite() {
setSiteName( name ?? null );
}
}
}, [ __, siteWithPathAlreadyExists, siteName, proposedSitePath ] );
}, [ __, siteWithPathAlreadyExists, siteName, proposedSitePath, sitePath ] );

const handleAddSiteClick = useCallback( async () => {
try {
Expand Down
5 changes: 3 additions & 2 deletions src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ export async function showSaveAsDialog( event: IpcMainInvokeEvent, options: Save

export async function showOpenFolderDialog(
event: IpcMainInvokeEvent,
title: string
title: string,
defaultDialogPath: string
): Promise< FolderDialogResponse | null > {
const parentWindow = BrowserWindow.fromWebContents( event.sender );
if ( ! parentWindow ) {
Expand All @@ -482,7 +483,7 @@ export async function showOpenFolderDialog(

const { canceled, filePaths } = await dialog.showOpenDialog( parentWindow, {
title,
defaultPath: DEFAULT_SITE_PATH,
defaultPath: defaultDialogPath !== '' ? defaultDialogPath : DEFAULT_SITE_PATH,
properties: [
'openDirectory',
'createDirectory', // allow user to create new directories; macOS only
Expand Down
3 changes: 2 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const api: IpcApi = {
{ autoLogin = true }: { autoLogin?: boolean } = {}
) => ipcRenderer.invoke( 'openSiteURL', id, relativeURL, { autoLogin } ),
openURL: ( url: string ) => ipcRenderer.invoke( 'openURL', url ),
showOpenFolderDialog: ( title: string ) => ipcRenderer.invoke( 'showOpenFolderDialog', title ),
showOpenFolderDialog: ( title: string, defaultDialogPath: string ) =>
ipcRenderer.invoke( 'showOpenFolderDialog', title, defaultDialogPath ),
showSaveAsDialog: ( options: SaveDialogOptions ) =>
ipcRenderer.invoke( 'showSaveAsDialog', options ),
saveUserLocale: ( locale: string ) => ipcRenderer.invoke( 'saveUserLocale', locale ),
Expand Down

0 comments on commit a0aacb7

Please sign in to comment.