diff --git a/src/lib/layout/progress.svelte b/src/lib/layout/progress.svelte index 8d34097f4..a5b445fcc 100644 --- a/src/lib/layout/progress.svelte +++ b/src/lib/layout/progress.svelte @@ -1,4 +1,5 @@ diff --git a/src/lib/stores/navigation.ts b/src/lib/stores/navigation.ts new file mode 100644 index 000000000..f35211058 --- /dev/null +++ b/src/lib/stores/navigation.ts @@ -0,0 +1,3 @@ +import { writable } from 'svelte/store'; + +export const navigationCancelled = writable(false); diff --git a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/(suggestions)/empty.svelte b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/(suggestions)/empty.svelte index e8f94c43d..cb76163a0 100644 --- a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/(suggestions)/empty.svelte +++ b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/(suggestions)/empty.svelte @@ -24,12 +24,16 @@ type SuggestedColumnSchema, tableColumnSuggestions, basicColumnOptions, - mockSuggestions + mockSuggestions, + createTableRequest } from './store'; import { addNotification } from '$lib/stores/notifications'; import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; import { sleep } from '$lib/helpers/promises'; - import { invalidate } from '$app/navigation'; + import { invalidate, beforeNavigate, goto } from '$app/navigation'; + import { showCreateTable } from '../store'; + import { showSubNavigation } from '$lib/stores/layout'; + import { navigationCancelled } from '$lib/stores/navigation'; import { Dependencies } from '$lib/constants'; import { isWithinSafeRange } from '$lib/helpers/numbers'; import type { Columns } from '../table-[table]/store'; @@ -52,6 +56,9 @@ let scrollAnimationFrame: number | null = null; let confirmDismiss = $state(false); + let confirmNavigation = $state(false); + let pendingNavigationUrl: string | null = null; + let creatingColumns = $state(false); const baseColProps = { draggable: false, resizable: false }; @@ -252,6 +259,25 @@ }); }; + // Handle create table requests from subNavigation + const unsubscribeCreateTable = createTableRequest.subscribe((requested) => { + if (requested) { + if (customColumns.length > 0 && !creatingColumns) { + confirmNavigation = true; + pendingNavigationUrl = 'create-table'; + } else { + executeCreateTable(); + } + + createTableRequest.set(false); + } + }); + + function executeCreateTable() { + $showCreateTable = true; + $showSubNavigation = false; + } + const customSuggestedColumns = $derived.by(() => { return customColumns.map((col: SuggestedColumnSchema) => { const columnOption = getColumnOption(col.type, col.format); @@ -314,6 +340,16 @@ } ]; + // Handle browser back/forward navigation + const handleBeforeUnload = (event: BeforeUnloadEvent) => { + if (customColumns.length > 0 && !creatingColumns) { + event.preventDefault(); + event.returnValue = + 'You have unsaved column suggestions. Are you sure you want to leave?'; + return event.returnValue; + } + }; + const spreadsheetColumns = $derived(getRowColumns()); const emptyCells = $derived(($isSmallViewport ? 14 : 17) + (!$expandTabs ? 2 : 0)); @@ -327,7 +363,20 @@ await suggestColumns(); }); + beforeNavigate(({ cancel, to }) => { + if (customColumns.length > 0 && !creatingColumns) { + cancel(); + confirmNavigation = true; + $navigationCancelled = true; + pendingNavigationUrl = to?.url?.pathname || null; + } + }); + function resetSuggestionsStore(fullReset: boolean = true) { + if ($tableColumnSuggestions.table?.id !== page.params.table) { + return; + } + if (fullReset) { // these are referenced in // `table-[table]/+page.svelte` @@ -434,6 +483,10 @@ ); } + function isCustomColumn(id: string) { + return !['$id', '$createdAt', '$updatedAt', 'actions'].includes(id); + } + async function createColumns() { creatingColumns = true; const client = sdk.forProject(page.params.region, page.params.project); @@ -563,14 +616,11 @@ customColumns = []; resetSuggestionsStore(); + unsubscribeCreateTable(); }); - - function isCustomColumn(id: string) { - return !['$id', '$createdAt', '$updatedAt', 'actions'].includes(id); - } - +
+ { + customColumns = []; + resetSuggestionsStore(); + confirmNavigation = false; + + if (pendingNavigationUrl) { + if (pendingNavigationUrl === 'create-table') { + executeCreateTable(); + } else { + goto(pendingNavigationUrl); + } + + pendingNavigationUrl = null; + } + }}> + You have unsaved column suggestions. If you leave this page, you'll lose these suggestions. Are + you sure you want to continue? + +