1
1
import { useMemo } from 'react' ;
2
2
import styled from '@emotion/styled' ;
3
+ import * as Sentry from '@sentry/react' ;
3
4
5
+ import {
6
+ addErrorMessage ,
7
+ addLoadingMessage ,
8
+ addSuccessMessage ,
9
+ } from 'sentry/actionCreators/indicator' ;
4
10
import { openSaveQueryModal } from 'sentry/actionCreators/modal' ;
5
11
import Feature from 'sentry/components/acl/feature' ;
6
12
import { t } from 'sentry/locale' ;
@@ -27,7 +33,9 @@ import {
27
33
import { handleAddQueryToDashboard } from 'sentry/views/discover/utils' ;
28
34
import { Mode } from 'sentry/views/explore/contexts/pageParamsContext/mode' ;
29
35
import { formatSort } from 'sentry/views/explore/contexts/pageParamsContext/sortBys' ;
36
+ import { useGetSavedQuery } from 'sentry/views/explore/hooks/useGetSavedQueries' ;
30
37
import { useLogsSaveQuery } from 'sentry/views/explore/hooks/useSaveQuery' ;
38
+ import { useQueryParamsId } from 'sentry/views/explore/queryParams/context' ;
31
39
import type { Visualize } from 'sentry/views/explore/queryParams/visualize' ;
32
40
import { TraceItemDataset } from 'sentry/views/explore/types' ;
33
41
import { getAlertsUrl } from 'sentry/views/insights/common/utils/getAlertsUrl' ;
@@ -56,7 +64,9 @@ export function useSaveAsItems({
56
64
const organization = useOrganization ( ) ;
57
65
const { projects} = useProjects ( ) ;
58
66
const pageFilters = usePageFilters ( ) ;
59
- const { saveQuery} = useLogsSaveQuery ( ) ;
67
+ const { saveQuery, updateQuery} = useLogsSaveQuery ( ) ;
68
+ const id = useQueryParamsId ( ) ;
69
+ const { data : savedQuery } = useGetSavedQuery ( id ) ;
60
70
61
71
const project =
62
72
projects . length === 1
@@ -69,6 +79,30 @@ export function useSaveAsItems({
69
79
) ;
70
80
71
81
const saveAsQuery = useMemo ( ( ) => {
82
+ // Show "Existing Query" if we have a non-prebuilt saved query, otherwise "A New Query"
83
+ if ( defined ( id ) && savedQuery ?. isPrebuilt === false ) {
84
+ return {
85
+ key : 'update-query' ,
86
+ textValue : t ( 'Existing Query' ) ,
87
+ label : < span > { t ( 'Existing Query' ) } </ span > ,
88
+ onAction : async ( ) => {
89
+ try {
90
+ addLoadingMessage ( t ( 'Updating query...' ) ) ;
91
+ await updateQuery ( ) ;
92
+ addSuccessMessage ( t ( 'Query updated successfully' ) ) ;
93
+ trackAnalytics ( 'logs.save_as' , {
94
+ save_type : 'update_query' ,
95
+ ui_source : 'searchbar' ,
96
+ organization,
97
+ } ) ;
98
+ } catch ( error ) {
99
+ addErrorMessage ( t ( 'Failed to update query' ) ) ;
100
+ Sentry . captureException ( error ) ;
101
+ }
102
+ } ,
103
+ } ;
104
+ }
105
+
72
106
return {
73
107
key : 'save-query' ,
74
108
label : < span > { t ( 'A New Query' ) } </ span > ,
@@ -88,7 +122,7 @@ export function useSaveAsItems({
88
122
} ) ;
89
123
} ,
90
124
} ;
91
- } , [ organization , saveQuery ] ) ;
125
+ } , [ id , savedQuery ?. isPrebuilt , updateQuery , saveQuery , organization ] ) ;
92
126
93
127
const saveAsAlert = useMemo ( ( ) => {
94
128
const alertsUrls = aggregates . map ( ( yAxis : string , index : number ) => {
0 commit comments