-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathuseProjectFileActions.ts
More file actions
382 lines (358 loc) · 12.9 KB
/
Copy pathuseProjectFileActions.ts
File metadata and controls
382 lines (358 loc) · 12.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import {
DEFAULT_PROJECT_NAME,
projectFromStore,
serializeProject,
useAppStore,
} from "@geolibre/core";
import { type FormEvent, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { getPluginManager } from "./usePlugins";
import { useDesktopSettingsStore } from "./useDesktopSettings";
import {
browserSaveFallsBackToDownload,
isHttpUrl,
isTauri,
openProjectFile,
openRecentProjectFile,
RecentProjectGoneError,
saveProjectFile,
saveProjectFileToPath,
} from "../lib/tauri-io";
import { mergeStringLists } from "../lib/string-lists";
import { normalizeProjectUrl } from "../lib/urls";
import { resolveProjectXyzLayers } from "../lib/xyz-url";
import type { MapControllerRef } from "../components/layout/toolbar/constants";
/** A pending "strip env vars before saving?" prompt. */
export interface EnvStripPrompt {
count: number;
resolve: (choice: "strip" | "keep" | "cancel") => void;
}
/**
* A pending "name this project file" prompt, shown when Save As (or a first
* Save) runs in a browser that can only download under a fixed name.
*/
export interface SaveNamePrompt {
resolve: (name: string | null) => void;
}
/**
* Ensure a user-entered project file name carries a recognized extension,
* defaulting to `.geolibre.json` when none is present so the downloaded file
* opens cleanly again later. Falls back to the default project name when blank.
*
* @param name - The raw file name the user typed.
* @returns A sanitized file name ending in a project extension.
*/
function ensureProjectFileName(name: string): string {
const trimmed = name.trim();
if (!trimmed) return `${DEFAULT_PROJECT_NAME}.geolibre.json`;
return /\.(geolibre\.json|geolibre|json)$/i.test(trimmed)
? trimmed
: `${trimmed}.geolibre.json`;
}
/**
* Bundles every project file action (open from file/URL/recent, save, save as)
* along with the related dialog state (Open-from-URL, env-var strip prompt, and
* the shared action-error dialog).
*
* @param mapControllerRef - Ref to the live MapController, read when serializing.
* @returns Handlers and state consumed by the toolbar menus and dialogs.
*/
export function useProjectFileActions(mapControllerRef: MapControllerRef) {
const { t } = useTranslation();
const loadProject = useAppStore((s) => s.loadProject);
const setProjectPath = useAppStore((s) => s.setProjectPath);
const rememberRecentProject = useAppStore((s) => s.rememberRecentProject);
const forgetRecentProject = useAppStore((s) => s.forgetRecentProject);
const markSaved = useAppStore((s) => s.markSaved);
const [actionError, setActionError] = useState<string | null>(null);
const [projectUrlDialogOpen, setProjectUrlDialogOpen] = useState(false);
const [projectUrl, setProjectUrl] = useState("");
const [projectUrlError, setProjectUrlError] = useState<string | null>(null);
const [projectUrlLoading, setProjectUrlLoading] = useState(false);
const [envStripPrompt, setEnvStripPrompt] = useState<EnvStripPrompt | null>(
null,
);
const [saveNamePrompt, setSaveNamePrompt] = useState<SaveNamePrompt | null>(
null,
);
const [saveNameInput, setSaveNameInput] = useState("");
const projectUrlAbortRef = useRef<AbortController | null>(null);
const recentAbortRef = useRef<AbortController | null>(null);
const handleOpenFromFile = async () => {
const result = await openProjectFile();
if (result) {
try {
loadProject(
await resolveProjectXyzLayers(result.project),
result.path,
{ rememberRecent: isTauri() },
);
} catch (error) {
console.error("Failed to open project", error);
setActionError(
error instanceof Error
? error.message
: t("toolbar.error.couldNotOpenProject"),
);
}
}
};
const handleOpenFromUrl = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const normalizedUrl = normalizeProjectUrl(projectUrl);
if (!normalizedUrl) {
setProjectUrlError(t("toolbar.error.invalidProjectUrl"));
return;
}
projectUrlAbortRef.current?.abort();
const controller = new AbortController();
projectUrlAbortRef.current = controller;
setProjectUrlLoading(true);
setProjectUrlError(null);
try {
const result = await openRecentProjectFile(
normalizedUrl,
controller.signal,
);
const project = await resolveProjectXyzLayers(
result.project,
controller.signal,
);
if (controller.signal.aborted) return;
loadProject(project, result.path);
setProjectUrl("");
setProjectUrlDialogOpen(false);
} catch (error) {
if (controller.signal.aborted) return;
console.error("Failed to open project URL", error);
setProjectUrlError(
error instanceof Error
? error.message
: t("toolbar.error.couldNotOpenProjectUrl"),
);
} finally {
if (projectUrlAbortRef.current === controller) {
projectUrlAbortRef.current = null;
}
setProjectUrlLoading(false);
}
};
const handleOpenRecent = async (path: string) => {
// Cancel any previous in-flight open so rapid clicks cannot race and let a
// stale fetch win by resolving last.
recentAbortRef.current?.abort();
const controller = new AbortController();
recentAbortRef.current = controller;
let result: Awaited<ReturnType<typeof openRecentProjectFile>>;
try {
result = await openRecentProjectFile(path, controller.signal);
} catch (error) {
if (controller.signal.aborted) return;
// Only drop the entry when the project is permanently gone; preserve it
// for transient failures (network timeout, 5xx, momentary IO error).
if (error instanceof RecentProjectGoneError) {
forgetRecentProject(path);
}
console.error("Failed to open recent project", error);
setActionError(
error instanceof Error
? error.message
: t("toolbar.error.couldNotOpenRecentProject"),
);
return;
}
try {
const project = await resolveProjectXyzLayers(
result.project,
controller.signal,
);
if (controller.signal.aborted) return;
loadProject(project, result.path);
} catch (error) {
if (controller.signal.aborted) return;
console.error("Failed to load recent project", error);
setActionError(
error instanceof Error
? error.message
: t("toolbar.error.couldNotLoadRecentProject"),
);
} finally {
if (recentAbortRef.current === controller) {
recentAbortRef.current = null;
}
}
};
// Build the current project from live store + map state and serialize it.
// Shared by Save/Save As and the Share action so they all capture identical
// project content (including the current map view and plugin state).
const buildCurrentProject = (nameOverride?: string) => {
const state = useAppStore.getState();
const defaultProjectName =
nameOverride?.trim() || state.projectName.trim() || DEFAULT_PROJECT_NAME;
const pluginManifestUrls = mergeStringLists(
state.projectPlugins?.manifestUrls ?? [],
useDesktopSettingsStore.getState().desktopSettings.pluginManifestUrls,
);
const project = projectFromStore({
projectName: defaultProjectName,
mapView: mapControllerRef.current?.readView() ?? state.mapView,
basemapStyleUrl: state.basemapStyleUrl,
basemapVisible: state.basemapVisible,
basemapOpacity: state.basemapOpacity,
layers: state.layers,
layerGroups: state.layerGroups,
preferences: state.preferences,
plugins: {
...getPluginManager().getProjectState(),
manifestUrls: pluginManifestUrls,
},
legend: state.legend,
storymap: state.storymap,
models: state.models,
widgets: state.widgets,
dashboardColumns: state.dashboardColumns,
metadata: state.metadata,
});
return {
project,
defaultProjectName,
content: serializeProject(project),
// Expose the path read from this same snapshot so callers don't take a
// second `getState()` read that could be misread as a separate instant.
projectPath: state.projectPath,
};
};
// Ask whether to strip environment variables before writing the file. The
// promise resolves when the user picks an option in the dialog.
const askStripEnvVars = (count: number) =>
new Promise<"strip" | "keep" | "cancel">((resolve) => {
setEnvStripPrompt({ count, resolve });
});
const resolveEnvStripPrompt = (choice: "strip" | "keep" | "cancel") => {
// Resolve outside the state updater (updaters must be side-effect free).
envStripPrompt?.resolve(choice);
setEnvStripPrompt(null);
};
// Ask the user to name the project file. Used only when saving falls back to
// a browser download (no File System Access picker), where the name is the
// only thing the user can control. Resolves with the name, or null if cancelled.
const askSaveName = (defaultName: string) =>
new Promise<string | null>((resolve) => {
setSaveNameInput(defaultName);
setSaveNamePrompt({ resolve });
});
const submitSaveNamePrompt = (event?: FormEvent<HTMLFormElement>) => {
event?.preventDefault();
saveNamePrompt?.resolve(saveNameInput);
setSaveNamePrompt(null);
};
const cancelSaveNamePrompt = () => {
saveNamePrompt?.resolve(null);
setSaveNamePrompt(null);
};
const saveProject = async (options?: {
saveAs?: boolean;
}): Promise<boolean> => {
const { project, defaultProjectName, content, projectPath } =
buildCurrentProject();
// Env vars (possibly API keys) are serialized in plain text. If any are set,
// offer to strip them from the saved file before writing.
let contentToSave = content;
const envVarCount = (project.preferences.environmentVariables ?? []).filter(
(variable) => variable.key.trim(),
).length;
if (envVarCount > 0) {
const choice = await askStripEnvVars(envVarCount);
if (choice === "cancel") return false;
if (choice === "strip") {
contentToSave = serializeProject({
...project,
preferences: { ...project.preferences, environmentVariables: [] },
});
}
}
// Projects opened from a URL have no writable path, so both Save and
// Save As fall back to the save dialog for them.
const existingLocalPath =
projectPath && !isHttpUrl(projectPath) ? projectPath : null;
// Browsers without the File System Access picker (Firefox, Safari) can only
// download under a fixed name, so Save As (and a first Save) would otherwise
// reuse a default name — exactly the bug users hit. Prompt for the name so
// they can choose it; later in-place Saves reuse the chosen name silently.
let saveName = `${defaultProjectName}.geolibre.json`;
const promptForName =
browserSaveFallsBackToDownload() &&
(options?.saveAs === true || !existingLocalPath);
if (promptForName) {
const chosen = await askSaveName(saveName);
if (chosen === null) return false;
saveName = ensureProjectFileName(chosen);
}
let path: string | null;
try {
path =
!options?.saveAs && existingLocalPath
? await saveProjectFileToPath(contentToSave, existingLocalPath)
: await saveProjectFile(
contentToSave,
promptForName ? saveName : (existingLocalPath ?? saveName),
);
} catch (error) {
console.error("Failed to save project", error);
setActionError(
error instanceof Error
? error.message
: t("toolbar.error.couldNotSaveProject"),
);
return false;
}
if (!path) return false;
setProjectPath(path);
rememberRecentProject({
path,
name: project.name,
openedAt: new Date().toISOString(),
});
markSaved();
return true;
};
const handleSave = () => saveProject();
const handleSaveAs = () => saveProject({ saveAs: true });
// Open-change handler for the Open-from-URL dialog; aborts an in-flight fetch
// and resets the form when the dialog closes.
const handleProjectUrlDialogOpenChange = (open: boolean) => {
setProjectUrlDialogOpen(open);
if (!open) {
projectUrlAbortRef.current?.abort();
projectUrlAbortRef.current = null;
setProjectUrl("");
setProjectUrlError(null);
setProjectUrlLoading(false);
}
};
return {
actionError,
setActionError,
projectUrlDialogOpen,
setProjectUrlDialogOpen,
handleProjectUrlDialogOpenChange,
projectUrl,
setProjectUrl,
projectUrlError,
setProjectUrlError,
projectUrlLoading,
envStripPrompt,
resolveEnvStripPrompt,
saveNamePrompt,
saveNameInput,
setSaveNameInput,
submitSaveNamePrompt,
cancelSaveNamePrompt,
handleOpenFromFile,
handleOpenFromUrl,
handleOpenRecent,
buildCurrentProject,
handleSave,
handleSaveAs,
};
}