|
| 1 | +import {Fragment, useCallback, useState} from 'react'; |
| 2 | +import {css} from '@emotion/react'; |
| 3 | +import styled from '@emotion/styled'; |
| 4 | +import * as Sentry from '@sentry/react'; |
| 5 | + |
| 6 | +import { |
| 7 | + addErrorMessage, |
| 8 | + addLoadingMessage, |
| 9 | + addSuccessMessage, |
| 10 | +} from 'sentry/actionCreators/indicator'; |
| 11 | +import type {ModalRenderProps} from 'sentry/actionCreators/modal'; |
| 12 | +import ButtonBar from 'sentry/components/buttonBar'; |
| 13 | +import {Button} from 'sentry/components/core/button'; |
| 14 | +import {Input} from 'sentry/components/core/input'; |
| 15 | +import {FormattedQuery} from 'sentry/components/searchQueryBuilder/formattedQuery'; |
| 16 | +import {t} from 'sentry/locale'; |
| 17 | +import {space} from 'sentry/styles/space'; |
| 18 | +import type {Organization, SavedQuery} from 'sentry/types/organization'; |
| 19 | +import {trackAnalytics} from 'sentry/utils/analytics'; |
| 20 | +import useOrganization from 'sentry/utils/useOrganization'; |
| 21 | +import {useSetExplorePageParams} from 'sentry/views/explore/contexts/pageParamsContext'; |
| 22 | +import type {Visualize} from 'sentry/views/explore/contexts/pageParamsContext/visualizes'; |
| 23 | + |
| 24 | +export type SaveQueryModalProps = { |
| 25 | + organization: Organization; |
| 26 | + query: string; |
| 27 | + saveQuery: (name: string) => Promise<SavedQuery>; |
| 28 | + visualizes: Visualize[]; |
| 29 | + groupBys?: string[]; // This needs to be passed in because saveQuery relies on being within the Explore PageParamsContext to fetch params |
| 30 | +}; |
| 31 | + |
| 32 | +type Props = ModalRenderProps & SaveQueryModalProps; |
| 33 | + |
| 34 | +function SaveQueryModal({ |
| 35 | + Header, |
| 36 | + Body, |
| 37 | + Footer, |
| 38 | + closeModal, |
| 39 | + groupBys, |
| 40 | + query, |
| 41 | + visualizes, |
| 42 | + saveQuery, |
| 43 | +}: Props) { |
| 44 | + const yAxes = visualizes.flatMap(visualize => visualize.yAxes); |
| 45 | + |
| 46 | + const organization = useOrganization(); |
| 47 | + |
| 48 | + const [name, setName] = useState(''); |
| 49 | + const [isSaving, setIsSaving] = useState(false); |
| 50 | + |
| 51 | + const setExplorePageParams = useSetExplorePageParams(); |
| 52 | + |
| 53 | + const updatePageIdAndTitle = useCallback( |
| 54 | + (id: string, title: string) => { |
| 55 | + setExplorePageParams({id, title}); |
| 56 | + }, |
| 57 | + [setExplorePageParams] |
| 58 | + ); |
| 59 | + |
| 60 | + const onSave = useCallback(async () => { |
| 61 | + try { |
| 62 | + setIsSaving(true); |
| 63 | + addLoadingMessage(t('Saving query...')); |
| 64 | + const {id} = await saveQuery(name); |
| 65 | + updatePageIdAndTitle(id, name); |
| 66 | + addSuccessMessage(t('Query saved successfully')); |
| 67 | + trackAnalytics('trace_explorer.save_as', { |
| 68 | + save_type: 'saved_query', |
| 69 | + ui_source: 'toolbar', |
| 70 | + organization, |
| 71 | + }); |
| 72 | + closeModal(); |
| 73 | + } catch (error) { |
| 74 | + addErrorMessage(t('Failed to save query')); |
| 75 | + Sentry.captureException(error); |
| 76 | + } finally { |
| 77 | + setIsSaving(false); |
| 78 | + } |
| 79 | + }, [saveQuery, name, updatePageIdAndTitle, closeModal, organization]); |
| 80 | + |
| 81 | + return ( |
| 82 | + <Fragment> |
| 83 | + <Header closeButton> |
| 84 | + <h4>{t('New Query')}</h4> |
| 85 | + </Header> |
| 86 | + <Body> |
| 87 | + <Wrapper> |
| 88 | + <SectionHeader>{t('Name')}</SectionHeader> |
| 89 | + <Input |
| 90 | + placeholder={t('Enter a name for your saved query')} |
| 91 | + onChange={e => setName(e.target.value)} |
| 92 | + value={name} |
| 93 | + title={t('Enter a name for your saved query')} |
| 94 | + /> |
| 95 | + </Wrapper> |
| 96 | + <Wrapper> |
| 97 | + <SectionHeader>{t('Query')}</SectionHeader> |
| 98 | + <ExploreParamsContainer> |
| 99 | + <ExploreParamSection> |
| 100 | + <ExploreParamTitle>{t('Visualize')}</ExploreParamTitle> |
| 101 | + <ExploreVisualizes>{yAxes.join(', ')}</ExploreVisualizes> |
| 102 | + </ExploreParamSection> |
| 103 | + {query && ( |
| 104 | + <ExploreParamSection> |
| 105 | + <ExploreParamTitle>{t('Filter')}</ExploreParamTitle> |
| 106 | + <FormattedQueryWrapper> |
| 107 | + <FormattedQuery query={query} /> |
| 108 | + </FormattedQueryWrapper> |
| 109 | + </ExploreParamSection> |
| 110 | + )} |
| 111 | + {groupBys && groupBys.length > 0 && ( |
| 112 | + <ExploreParamSection> |
| 113 | + <ExploreParamTitle>{t('Group By')}</ExploreParamTitle> |
| 114 | + <ExploreGroupBys>{groupBys.join(', ')}</ExploreGroupBys> |
| 115 | + </ExploreParamSection> |
| 116 | + )} |
| 117 | + <ExploreParamSection>...</ExploreParamSection> |
| 118 | + </ExploreParamsContainer> |
| 119 | + </Wrapper> |
| 120 | + </Body> |
| 121 | + |
| 122 | + <Footer> |
| 123 | + <StyledButtonBar gap={1.5}> |
| 124 | + <Button onClick={closeModal} disabled={isSaving}> |
| 125 | + {t('Cancel')} |
| 126 | + </Button> |
| 127 | + <Button onClick={onSave} disabled={!name || isSaving} priority="primary"> |
| 128 | + {t('Create a New Query')} |
| 129 | + </Button> |
| 130 | + </StyledButtonBar> |
| 131 | + </Footer> |
| 132 | + </Fragment> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +export default SaveQueryModal; |
| 137 | + |
| 138 | +const Wrapper = styled('div')` |
| 139 | + margin-bottom: ${space(2)}; |
| 140 | +`; |
| 141 | + |
| 142 | +const StyledButtonBar = styled(ButtonBar)` |
| 143 | + @media (max-width: ${props => props.theme.breakpoints.small}) { |
| 144 | + grid-template-rows: repeat(2, 1fr); |
| 145 | + gap: ${space(1.5)}; |
| 146 | + width: 100%; |
| 147 | +
|
| 148 | + > button { |
| 149 | + width: 100%; |
| 150 | + } |
| 151 | + } |
| 152 | +`; |
| 153 | + |
| 154 | +export const modalCss = css` |
| 155 | + max-width: 700px; |
| 156 | + margin: 70px auto; |
| 157 | +`; |
| 158 | + |
| 159 | +const SectionHeader = styled('h6')` |
| 160 | + font-size: ${p => p.theme.form.md.fontSize}; |
| 161 | + margin-bottom: ${space(0.5)}; |
| 162 | +`; |
| 163 | + |
| 164 | +const ExploreParamsContainer = styled('span')` |
| 165 | + display: flex; |
| 166 | + flex-direction: row; |
| 167 | + gap: ${space(1)}; |
| 168 | + flex-wrap: wrap; |
| 169 | +`; |
| 170 | + |
| 171 | +const ExploreParamSection = styled('span')` |
| 172 | + display: flex; |
| 173 | + flex-direction: row; |
| 174 | + gap: ${space(0.5)}; |
| 175 | + align-items: center; |
| 176 | +`; |
| 177 | + |
| 178 | +const ExploreParamTitle = styled('span')` |
| 179 | + font-size: ${p => p.theme.form.sm.fontSize}; |
| 180 | + color: ${p => p.theme.gray300}; |
| 181 | +`; |
| 182 | + |
| 183 | +const ExploreVisualizes = styled('span')` |
| 184 | + font-size: ${p => p.theme.form.sm.fontSize}; |
| 185 | + display: flex; |
| 186 | + align-items: center; |
| 187 | + gap: ${space(0.5)}; |
| 188 | + background: ${p => p.theme.background}; |
| 189 | + padding: ${space(0.25)} ${space(0.5)}; |
| 190 | + border: 1px solid ${p => p.theme.innerBorder}; |
| 191 | + border-radius: ${p => p.theme.borderRadius}; |
| 192 | + height: 24px; |
| 193 | + white-space: nowrap; |
| 194 | + overflow: hidden; |
| 195 | +`; |
| 196 | + |
| 197 | +const ExploreGroupBys = ExploreVisualizes; |
| 198 | + |
| 199 | +const FormattedQueryWrapper = styled('span')` |
| 200 | + display: inline-block; |
| 201 | +`; |
0 commit comments