@@ -3,12 +3,14 @@ import dynamic from 'next/dynamic';
33import Head from 'next/head' ;
44import Link from 'next/link' ;
55import { useRouter } from 'next/router' ;
6+ import { useQueryStates } from 'nuqs' ;
67import {
78 type FieldPath ,
89 useForm ,
910 useFormState ,
1011 useWatch ,
1112} from 'react-hook-form' ;
13+ import type { Filter } from '@hyperdx/common-utils/dist/types' ;
1214import {
1315 Anchor ,
1416 Box ,
@@ -30,6 +32,7 @@ import { withAppNavForSurface } from '@/layout';
3032import { useSaveSlo , useSlo , useSlosEnabledGuard } from '@/slo' ;
3133import { useSource } from '@/source' ;
3234import { getApiErrorMessage } from '@/utils/apiErrors' ;
35+ import { parseAsJsonEncoded , parseAsStringEncoded } from '@/utils/queryParsers' ;
3336
3437import {
3538 SloDetailsFields ,
@@ -40,8 +43,10 @@ import {
4043 buildSloPreviewRequest ,
4144 DEFAULT_SLO_FORM_VALUES ,
4245 getSloPreviewKey ,
46+ sloCopyFormValues ,
4347 sloFormResolver ,
4448 type SloFormValues ,
49+ sloFormValuesFromSearch ,
4550 type SloPreviewRequest ,
4651 sloToFormValues ,
4752 toSloInput ,
@@ -57,12 +62,28 @@ const PREVIEW_FIELDS: FieldPath<SloFormValues>[] = [
5762 'window' ,
5863] ;
5964
65+ // Carried over from Search by "Create SLO from this search".
66+ const prefillParsers = {
67+ source : parseAsStringEncoded ,
68+ where : parseAsStringEncoded ,
69+ whereLanguage : parseAsStringEncoded ,
70+ filters : parseAsJsonEncoded < Filter [ ] > ( ) ,
71+ name : parseAsStringEncoded ,
72+ } ;
73+
6074function SloEditorPage ( ) {
6175 const enabled = useSlosEnabledGuard ( ) ;
6276 const router = useRouter ( ) ;
6377 const editId =
6478 typeof router . query . sloId === 'string' ? router . query . sloId : undefined ;
79+ // "Duplicate" on an SLO links here with ?duplicate=<id>.
80+ const duplicateId =
81+ ! editId && typeof router . query . duplicate === 'string'
82+ ? router . query . duplicate
83+ : undefined ;
84+ const [ prefill ] = useQueryStates ( prefillParsers ) ;
6585 const { data : existing } = useSlo ( editId ) ;
86+ const { data : original } = useSlo ( duplicateId ) ;
6687 const title = usePageTitle ( editId ? 'Edit SLO' : 'New SLO' ) ;
6788
6889 const { control, handleSubmit, reset, setValue, getValues, trigger } =
@@ -77,16 +98,21 @@ function SloEditorPage() {
7798 } ) ;
7899 const { data : source } = useSource ( { id : sourceId || undefined } ) ;
79100
80- // Load the SLO being edited, once.
101+ // Load the SLO being edited or copied, or the search it starts from , once.
81102 const initializedRef = useRef ( false ) ;
82103 useEffect ( ( ) => {
83104 if ( initializedRef . current || ! router . isReady ) return ;
84105 if ( editId ) {
85106 if ( ! existing ) return ;
86107 reset ( sloToFormValues ( existing ) ) ;
108+ } else if ( duplicateId ) {
109+ if ( ! original ) return ;
110+ reset ( sloCopyFormValues ( original ) ) ;
111+ } else if ( prefill . source ) {
112+ reset ( sloFormValuesFromSearch ( { ...prefill , source : prefill . source } ) ) ;
87113 }
88114 initializedRef . current = true ;
89- } , [ router . isReady , editId , existing , reset ] ) ;
115+ } , [ router . isReady , editId , existing , duplicateId , original , prefill , reset ] ) ;
90116
91117 const [ preview , setPreview ] = useState < SloPreviewRequest | null > ( null ) ;
92118 const runPreview = async ( ) => {
@@ -100,6 +126,21 @@ function SloEditorPage() {
100126 source ,
101127 ) !== preview . key ;
102128
129+ // A definition carried over from Search previews itself once its source
130+ // has loaded.
131+ const autoPreviewedRef = useRef ( false ) ;
132+ useEffect ( ( ) => {
133+ if ( autoPreviewedRef . current || ! initializedRef . current || ! source ) return ;
134+ if ( editId || ! prefill . source ) return ;
135+ autoPreviewedRef . current = true ;
136+ void trigger ( PREVIEW_FIELDS ) . then ( valid => {
137+ if ( ! valid ) return ;
138+ setPreview (
139+ buildSloPreviewRequest ( getValues ( ) , source , Date . now ( ) ) ?? null ,
140+ ) ;
141+ } ) ;
142+ } ) ;
143+
103144 const save = useSaveSlo ( ) ;
104145 const onSave = handleSubmit ( async values => {
105146 try {
0 commit comments