-
Notifications
You must be signed in to change notification settings - Fork 269
/
index.tsx
302 lines (277 loc) · 8.35 KB
/
index.tsx
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import type { PlaygroundClient } from '@wp-playground/client';
import css from './style.module.css';
import Button from '../button';
import { FormEvent, useEffect, useRef, useState } from 'react';
import Modal, { defaultStyles } from '../modal';
import { playgroundAvailableInOpfs } from './playground-available-in-opfs';
import { PlaygroundConfiguration, PlaygroundConfigurationForm } from './form';
import { reloadWithNewConfiguration } from './reload-with-new-configuration';
import {
getIndexedDB,
loadDirectoryHandle,
saveDirectoryHandle,
} from './idb-opfs';
import { OPFSButton } from './opfs-button';
import { usePlaygroundContext } from '../playground-viewport/context';
import { SyncLocalFilesButton } from './sync-local-files-button';
import { StartOverButton } from './start-over-button';
interface SiteSetupGroupProps {
initialConfiguration: PlaygroundConfiguration;
}
const canUseLocalDirectory = !!(window as any).showDirectoryPicker;
let idb: IDBDatabase | null,
lastDirectoryHandle: FileSystemDirectoryHandle | null;
try {
idb = await getIndexedDB();
lastDirectoryHandle = await loadDirectoryHandle(idb);
} catch (e) {
// Ignore errors.
}
export default function PlaygroundConfigurationGroup({
initialConfiguration,
}: SiteSetupGroupProps) {
const [isOpen, setOpen] = useState(false);
const openModal = () => setOpen(true);
const closeModal = () => setOpen(false);
const playgroundRef = useRef<{
promise: Promise<PlaygroundClient>;
resolve: any;
}>();
const { playground } = usePlaygroundContext();
useEffect(() => {
if (!playgroundRef.current) {
let resolve;
const promise = new Promise<PlaygroundClient>((_resolve) => {
resolve = _resolve;
});
playgroundRef.current = {
promise,
resolve,
};
}
if (playground) {
playgroundRef.current!.resolve(playground);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [!!playground]);
const [isResumeLastDirOpen, setResumeLastDirOpen] = useState(
initialConfiguration.storage === 'opfs-host' && !!lastDirectoryHandle
);
const closeResumeLastDirModal = () => setResumeLastDirOpen(false);
const [dirName, setDirName] = useState<string | undefined>(
lastDirectoryHandle?.name
);
const [mounting, setMounting] = useState(false);
const [mountProgress, setMountProgress] = useState<any | null>(null);
const [currentConfiguration, setCurrentConfiguration] =
useState(initialConfiguration);
const runningWp = useCurrentlyRunningWordPressVersion();
const isSameOriginAsPlayground = useIsSameOriginAsPlayground(playground);
async function handleSelectLocalDirectory() {
let dirHandle: FileSystemDirectoryHandle;
try {
// Request permission to access the directory.
// https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker
dirHandle = await (window as any).showDirectoryPicker({
// By specifying an ID, the browser can remember different directories for
// different IDs.If the same ID is used for another picker, the picker opens
// in the same directory.
id: 'playground-directory',
mode: 'readwrite',
});
} catch (e) {
// No directory selected but log the error just in case.
console.error(e);
return;
}
setDirName(dirHandle.name);
await handleMountLocalDirectory(dirHandle);
closeModal();
}
async function handleResumeLastDir(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
const perm = await (lastDirectoryHandle as any).requestPermission({
mode: 'readwrite',
});
if (perm === 'granted') {
await handleMountLocalDirectory(lastDirectoryHandle!);
} else {
alert('Switching to temporary site mode.');
}
closeResumeLastDirModal();
}
async function handleMountLocalDirectory(
dirHandle: FileSystemDirectoryHandle
) {
const playground = await playgroundRef.current!.promise;
if (idb) {
await saveDirectoryHandle(idb, dirHandle);
}
setMounting(true);
try {
const isPlaygroundDir = await playgroundAvailableInOpfs(dirHandle);
if (!isPlaygroundDir) {
// Check if it's an empty directory.
for await (const _ of dirHandle.values()) {
if (_.name.startsWith('.')) continue;
alert(
'You need to select either an empty directory or a pre-existing Playground directory.'
);
return;
}
}
await playground.bindOpfs(dirHandle, (progress) => {
setMountProgress(progress);
});
setCurrentConfiguration({
...currentConfiguration,
storage: 'opfs-host',
});
await playground.goTo('/');
// Read current querystring and replace storage=opfs-browser with storage=opfs-host.
const url = new URL(window.location.href);
url.searchParams.set('storage', 'opfs-host');
window.history.pushState({}, '', url.toString());
alert('You are now using WordPress from your local directory.');
} finally {
setMounting(false);
}
}
async function handleSubmit(config: PlaygroundConfiguration) {
const playground = await playgroundRef.current!.promise;
if (config.resetSite && config.storage === 'opfs-browser') {
if (
!window.confirm(
'This will wipe out all stored data and start a new site. Do you want to proceed?'
)
) {
return;
}
}
reloadWithNewConfiguration(playground!, config);
}
return (
<>
<Button onClick={openModal}>
PHP {currentConfiguration.php} {' - '}
WP {runningWp || currentConfiguration.wp} {' - '}
{currentConfiguration.storage === 'opfs-host'
? `Local (${dirName})`
: currentConfiguration.storage === 'opfs-browser'
? 'Persistent'
: '⚠️ Temporary'}
</Button>
{currentConfiguration.storage === 'opfs-host' ? (
<SyncLocalFilesButton />
) : null}
{currentConfiguration.storage === 'opfs-browser' ? (
<StartOverButton />
) : null}
{isResumeLastDirOpen ? (
<Modal
isOpen={isResumeLastDirOpen}
onRequestClose={closeResumeLastDirModal}
style={{
...defaultStyles,
content: {
...defaultStyles.content,
width: 550,
},
}}
>
<h2 style={{ textAlign: 'center', marginTop: 0 }}>
Resume working in local directory?
</h2>
<p>
It seems like you were working a local directory called{' '}
<b>"{lastDirectoryHandle!.name}"</b> during your last
visit. Would you like to resume working in this
directory or start fresh?
</p>
<form
onSubmit={handleResumeLastDir}
className={css.buttonsWrapper}
>
<Button
size="large"
onClick={() => {
reloadWithNewConfiguration(playground!, {
...initialConfiguration,
storage: 'temporary',
});
}}
>
Start a fresh site
</Button>
<OPFSButton
isMounting={mounting}
mountProgress={mountProgress}
type="submit"
variant="primary"
size="large"
autoFocus
>
Resume working in "{lastDirectoryHandle!.name}"
</OPFSButton>
</form>
</Modal>
) : null}
<Modal
isOpen={isOpen}
onRequestClose={closeModal}
style={{
...defaultStyles,
content: {
...defaultStyles.content,
width: 550,
},
}}
>
<PlaygroundConfigurationForm
currentlyRunningWordPressVersion={runningWp}
initialData={currentConfiguration}
onSubmit={handleSubmit}
isMountingLocalDirectory={mounting}
mountProgress={mountProgress}
onSelectLocalDirectory={
canUseLocalDirectory
? isSameOriginAsPlayground
? handleSelectLocalDirectory
: 'origin-mismatch'
: 'not-available'
}
/>
</Modal>
</>
);
}
function useCurrentlyRunningWordPressVersion(playground?: PlaygroundClient) {
const [
currentlyRunningWordPressVersion,
setCurrentlyRunningWordPressVersion,
] = useState<string | undefined>();
useEffect(() => {
if (playground) {
playground.getWordPressModuleDetails().then((details) => {
setCurrentlyRunningWordPressVersion(details.majorVersion);
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [!playground]);
return currentlyRunningWordPressVersion;
}
function useIsSameOriginAsPlayground(playground?: PlaygroundClient) {
const [isSameOriginAsPlayground, setIsSameOriginAsPlayground] = useState<
null | boolean
>(null);
useEffect(() => {
if (!playground) return;
(async () => {
setIsSameOriginAsPlayground(
new URL(await playground.absoluteUrl).origin ===
window.location.origin
);
})();
}, [playground]);
return isSameOriginAsPlayground;
}