Files
Danny White e7b1f68956 feat(studio): create pipelines from wizard review (#51900)
## Problem

The pipeline wizard can validate and review settings, but cannot create
a pipeline. This is slice 13 of #49243, following #51823.

## Solution

- Enable **Start pipeline** on Review using the sheet’s existing
validation, creation and start requests.
- Recheck publication settings, block critical failures and show
warnings inline before **Start pipeline anyway**.
- Preserve values after validation or create failures. Once creation
succeeds, finish the start attempt and return to Pipelines, including
when starting fails.

| After |
| --- |
| <img width="1234" height="2470" alt="CleanShot 2026-10-19 at 11 22
44@2x"
src="https://github.com/user-attachments/assets/23882092-26e2-4d23-b2a1-00d1d2dcba90"
/> |

**Add pipeline** still opens the sheet. Wizard entry remains behind
**Pipeline creation wizard** at `/database/pipelines/new`; routing is
the next slice.

## Review instructions

1. Open the [Pipelines
preview](https://studio-staging-git-dnywh-featpipeline-review-creation-supabase.vercel.app/dashboard/project/_/database/pipelines),
choose a test project and enable **Pipeline creation wizard** in Feature
Previews. Open the [wizard
preview](https://studio-staging-git-dnywh-featpipeline-review-creation-supabase.vercel.app/dashboard/project/_/database/pipelines/new)
for that project.
2. Choose a destination, enter valid test credentials and select a
publication. Continue to Review and check the settings, masked secrets
and costs. Back and Continue should preserve your values.
3. Click **Start pipeline**. Controls should lock while submitting, then
return to Pipelines with one new pipeline and no discard prompt. If
warnings appear, review them before clicking **Start pipeline anyway**;
critical failures must block creation.
4. Turn the preview off and open the wizard URL again: creation should
use the existing sheet. Editing existing pipelines should also keep the
sheet.

Automated tests cover final validation, changed warnings, create
retries, duplicate-click protection, removed publications and start
failure recovery.
2026-10-20 11:04:05 +11:00

83 lines
2.5 KiB
TypeScript

import { useRouter as useTanStackRouter } from '@tanstack/react-router'
import { useEffect, useRef } from 'react'
import { useLatest } from '@/hooks/misc/useLatest'
import { useConfirmOnClose } from '@/hooks/ui/useConfirmOnClose'
export const usePipelineNavigationGuard = (
hasChanges: boolean,
isNavigationLocked: () => boolean = () => false
) => {
const router = useTanStackRouter({ warn: false })
const canLeave = useRef(false)
const pending = useRef<(() => void) | undefined>(undefined)
const cancelPending = useRef<(() => void) | undefined>(undefined)
const { confirmOnClose, modalProps } = useConfirmOnClose({
checkIsDirty: () => hasChanges,
onClose: () => {
canLeave.current = true
pending.current?.()
},
})
const requestNavigation = (proceed: () => void, cancel?: () => void) => {
// Keep the first requested destination while its confirmation is open.
if (isNavigationLocked() || pending.current) {
cancel?.()
return
}
pending.current = proceed
cancelPending.current = cancel
confirmOnClose()
}
const latest = useLatest({ hasChanges, requestNavigation, isNavigationLocked })
useEffect(() => {
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
if (!latest.current.hasChanges || canLeave.current) return
event.preventDefault()
event.returnValue = ''
}
window.addEventListener('beforeunload', handleBeforeUnload)
return () => window.removeEventListener('beforeunload', handleBeforeUnload)
}, [latest])
useEffect(() => {
if (!router) return
return router.history.block({
enableBeforeUnload: false,
blockerFn: ({ currentLocation, nextLocation }) => {
if (latest.current.isNavigationLocked() && !canLeave.current) return true
if (
!latest.current.hasChanges ||
canLeave.current ||
currentLocation.pathname === nextLocation.pathname
)
return false
return new Promise<boolean>((resolve) => {
latest.current.requestNavigation(
() => resolve(false),
() => resolve(true)
)
})
},
})
}, [router, latest])
return {
requestNavigation,
completeNavigation: (proceed: () => void) => {
canLeave.current = true
proceed()
},
modalProps: {
...modalProps,
onCancel: () => {
cancelPending.current?.()
pending.current = undefined
cancelPending.current = undefined
modalProps.onCancel()
},
},
}
}