eventInputName.tsx 2.2 KB

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