Skip to content

Commit 065a810

Browse files
committed
Refactor to use shared function
1 parent 5124c5a commit 065a810

1 file changed

Lines changed: 43 additions & 25 deletions

File tree

src/routes/workspaces/[workspaceId]/+page.svelte

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -382,18 +382,13 @@
382382
}
383383
// Cancel navigation first, then show async modal and navigate if confirmed
384384
cancel();
385-
showUnsavedChangesModal(
385+
resolveUnsavedChanges(
386386
'There are unsaved changes. What would you like to do before leaving this page?',
387-
'Unsaved Changes',
388387
'Save and Leave',
389388
'Discard and Leave',
390389
'Stay on Page',
391-
).then(async ({ confirm, value }) => {
392-
if (!confirm || !to?.url) {
393-
return;
394-
}
395-
if (value?.shouldSave && !(await saveActiveDocument())) {
396-
// Save failed or was cancelled — stay on the page so edits aren't lost.
390+
).then(decision => {
391+
if (decision === 'stay' || !to?.url) {
397392
return;
398393
}
399394
// Reset content to allow navigation without re-triggering the modal
@@ -420,22 +415,19 @@
420415
if (!$activeDocumentIsDirty) {
421416
return true;
422417
}
423-
const { confirm, value } = await showUnsavedChangesModal(
418+
const decision = await resolveUnsavedChanges(
424419
'There are unsaved changes. What would you like to do before navigating away from the current file?',
425-
'Unsaved Changes',
426420
'Save and Navigate',
427421
'Discard and Navigate',
428422
'Keep Editing',
429423
);
430-
if (!confirm) {
424+
if (decision === 'stay') {
431425
return false;
432426
}
433-
if (value?.shouldSave) {
434-
// Only proceed if the save actually persisted; otherwise stay so edits aren't lost.
435-
return await saveActiveDocument();
427+
if (decision === 'proceed-discarded') {
428+
// Discard: mark clean so navigation proceeds without re-prompting.
429+
activeDocument.markClean();
436430
}
437-
// Discard: mark clean so navigation proceeds without re-prompting.
438-
activeDocument.markClean();
439431
return true;
440432
}
441433
@@ -805,6 +797,38 @@
805797
return false;
806798
}
807799
800+
/**
801+
* Prompts the user about unsaved changes and resolves the save/discard decision so callers
802+
* don't have to reimplement the modal + save-attempt dance. Returns:
803+
* - 'stay': user cancelled, or chose Save but the save failed/was cancelled — do not navigate.
804+
* - 'proceed-saved': document was saved (and marked clean); safe to navigate.
805+
* - 'proceed-discarded': user chose to discard; caller decides whether to revert content.
806+
* Callers own the post-decision action (goto / replaceState / content-mode switch) and any
807+
* discard-time content revert, since those differ per call site.
808+
*/
809+
async function resolveUnsavedChanges(
810+
message: string,
811+
confirmSaveLabel: string,
812+
confirmDiscardLabel: string,
813+
cancelLabel: string,
814+
): Promise<'stay' | 'proceed-saved' | 'proceed-discarded'> {
815+
const { confirm, value } = await showUnsavedChangesModal(
816+
message,
817+
'Unsaved Changes',
818+
confirmSaveLabel,
819+
confirmDiscardLabel,
820+
cancelLabel,
821+
);
822+
if (!confirm) {
823+
return 'stay';
824+
}
825+
if (value?.shouldSave) {
826+
// Save failed or was cancelled — stay so edits aren't lost.
827+
return (await saveActiveDocument()) ? 'proceed-saved' : 'stay';
828+
}
829+
return 'proceed-discarded';
830+
}
831+
808832
async function confirmAndNavigate(filePath: string | null) {
809833
if (!(await confirmNavigateAway())) {
810834
return false;
@@ -1280,22 +1304,16 @@
12801304
) {
12811305
// Guard against switching away from dirty file
12821306
if ($workspaceContentMode === WorkspaceContentMode.File && $activeDocumentIsDirty) {
1283-
const { confirm, value } = await showUnsavedChangesModal(
1307+
const decision = await resolveUnsavedChanges(
12841308
'There are unsaved changes. What would you like to do before navigating away from the current file?',
1285-
'Unsaved Changes',
12861309
'Save and Navigate',
12871310
'Discard and Navigate',
12881311
'Keep Editing',
12891312
);
1290-
if (!confirm) {
1313+
if (decision === 'stay') {
12911314
return;
12921315
}
1293-
if (value?.shouldSave) {
1294-
if (!(await saveActiveDocument())) {
1295-
// Save failed or was cancelled — stay on the current file so edits aren't lost.
1296-
return;
1297-
}
1298-
} else {
1316+
if (decision === 'proceed-discarded') {
12991317
// Discard: revert content to last-saved state and mark clean
13001318
activeDocument.updateContent($activeDocument.originalContent);
13011319
activeDocument.markClean();

0 commit comments

Comments
 (0)