eventInputName.tsx 2.2 KB

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