eventInputName.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {browserHistory} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import EditableText from 'sentry/components/editableText';
  4. import {Title} from 'sentry/components/layouts/thirds';
  5. import {t} from 'sentry/locale';
  6. import {Organization, SavedQuery} from 'sentry/types';
  7. import EventView from 'sentry/utils/discover/eventView';
  8. import useApi from 'sentry/utils/useApi';
  9. import {handleUpdateQueryName} from './savedQuery/utils';
  10. type Props = {
  11. eventView: EventView;
  12. organization: Organization;
  13. savedQuery?: SavedQuery;
  14. };
  15. const NAME_DEFAULT = t('Untitled query');
  16. /**
  17. * Allows user to edit the name of the query.
  18. * By pressing Enter or clicking outside the component, the changes will be saved, if valid.
  19. */
  20. function EventInputName({organization, eventView, savedQuery}: Props) {
  21. const api = useApi();
  22. function handleChange(nextQueryName: string) {
  23. // Do not update automatically if
  24. // 1) It is a new query
  25. // 2) The new name is same as the old name
  26. if (!savedQuery || savedQuery.name === nextQueryName) {
  27. return;
  28. }
  29. // This ensures that we are updating SavedQuery.name only.
  30. // Changes on QueryBuilder table will not be saved.
  31. const nextEventView = EventView.fromSavedQuery({
  32. ...savedQuery,
  33. name: nextQueryName,
  34. });
  35. handleUpdateQueryName(api, organization, nextEventView).then(
  36. (_updatedQuery: SavedQuery) => {
  37. // The current eventview may have changes that are not explicitly saved.
  38. // So, we just preserve them and change its name
  39. const renamedEventView = eventView.clone();
  40. renamedEventView.name = nextQueryName;
  41. browserHistory.push(renamedEventView.getResultsViewUrlTarget(organization.slug));
  42. }
  43. );
  44. }
  45. const value = eventView.name || NAME_DEFAULT;
  46. return (
  47. <StyledTitle data-test-id={`discover2-query-name-${value}`}>
  48. <EditableText
  49. value={value}
  50. onChange={handleChange}
  51. isDisabled={!eventView.id}
  52. errorMessage={t('Please set a name for this query')}
  53. />
  54. </StyledTitle>
  55. );
  56. }
  57. const StyledTitle = styled(Title)`
  58. overflow: unset;
  59. `;
  60. export default EventInputName;