-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathuse-add-site.ts
160 lines (152 loc) · 4.37 KB
/
use-add-site.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as Sentry from '@sentry/electron/renderer';
import { useI18n } from '@wordpress/react-i18n';
import { useCallback, useMemo, useState } from 'react';
import { getIpcApi } from '../lib/get-ipc-api';
import { useImportExport } from './use-import-export';
import { useSiteDetails } from './use-site-details';
export function useAddSite() {
const { __ } = useI18n();
const { createSite, data: sites, loadingSites, startServer } = useSiteDetails();
const { importFile, clearImportState } = useImportExport();
const [ error, setError ] = useState( '' );
const [ siteName, setSiteName ] = useState< string | null >( null );
const [ sitePath, setSitePath ] = useState( '' );
const [ proposedSitePath, setProposedSitePath ] = useState( '' );
const [ doesPathContainWordPress, setDoesPathContainWordPress ] = useState( false );
const [ fileForImport, setFileForImport ] = useState< File | null >( null );
const usedSiteNames = sites.map( ( site ) => site.name );
const siteWithPathAlreadyExists = useCallback(
( path: string ) => {
return sites.some( ( site ) => site.path === path );
},
[ sites ]
);
const handlePathSelectorClick = useCallback( async () => {
const response = await getIpcApi().showOpenFolderDialog( __( 'Choose folder for site' ) );
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;
}
if ( ! isEmpty && ! isWordPress && ! pathResetToDefaultSitePath ) {
setError(
__(
'This directory is not empty. Please select an empty directory or an existing WordPress folder.'
)
);
return;
}
setDoesPathContainWordPress( ! isEmpty && isWordPress );
if ( ! siteName ) {
setSiteName( name ?? null );
}
}
}, [ __, siteWithPathAlreadyExists, siteName, proposedSitePath ] );
const handleAddSiteClick = useCallback( async () => {
try {
const path = sitePath ? sitePath : proposedSitePath;
await createSite( path, siteName ?? '', async ( newSite ) => {
if ( newSite ) {
if ( fileForImport ) {
await importFile( fileForImport, newSite, {
showImportNotification: false,
isNewSite: true,
} );
clearImportState( newSite.id );
}
await startServer( newSite.id );
getIpcApi().showNotification( {
title: newSite.name,
body: __( 'Your new site is up and running' ),
} );
}
} );
} catch ( e ) {
Sentry.captureException( e );
}
}, [
__,
clearImportState,
createSite,
fileForImport,
importFile,
proposedSitePath,
siteName,
sitePath,
startServer,
] );
const handleSiteNameChange = useCallback(
async ( name: string ) => {
setSiteName( name );
if ( sitePath ) {
return;
}
setError( '' );
const {
path: proposedPath,
isEmpty,
isWordPress,
} = await getIpcApi().generateProposedSitePath( name );
setProposedSitePath( proposedPath );
if ( siteWithPathAlreadyExists( proposedPath ) ) {
return;
}
if ( ! isEmpty && ! isWordPress ) {
setError(
__(
'This directory is not empty. Please select an empty directory or an existing WordPress folder.'
)
);
return;
}
setDoesPathContainWordPress( ! isEmpty && isWordPress );
},
[ __, sitePath, siteWithPathAlreadyExists ]
);
return useMemo(
() => ( {
handleAddSiteClick,
handlePathSelectorClick,
handleSiteNameChange,
error: siteWithPathAlreadyExists( sitePath ? sitePath : proposedSitePath )
? __(
'Another site already exists at this path. Please select an empty directory to create a site.'
)
: error,
sitePath: sitePath ? sitePath : proposedSitePath,
siteName,
doesPathContainWordPress,
siteWithPathAlreadyExists,
setSiteName,
proposedSitePath,
setProposedSitePath,
setSitePath,
setError,
setDoesPathContainWordPress,
usedSiteNames,
loadingSites,
fileForImport,
setFileForImport,
} ),
[
__,
doesPathContainWordPress,
error,
handleAddSiteClick,
handlePathSelectorClick,
siteWithPathAlreadyExists,
handleSiteNameChange,
siteName,
sitePath,
proposedSitePath,
usedSiteNames,
loadingSites,
fileForImport,
]
);
}