sentryDocumentTitle.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import * as React from 'react';
  2. import DocumentTitle from 'react-document-title';
  3. type Props = {
  4. /**
  5. * This string will be shown at the very front of the title
  6. */
  7. title?: string;
  8. /**
  9. * The organization slug to show in the title
  10. */
  11. orgSlug?: string;
  12. /**
  13. * The project slug to show in the title.
  14. */
  15. projectSlug?: string;
  16. /**
  17. * Should the ` - Sentry` suffix be excluded?
  18. */
  19. noSuffix?: boolean;
  20. children?: React.ReactChild;
  21. };
  22. /**
  23. * Assigns the document title. The deepest nested version of this title will be
  24. * the one which is assigned.
  25. */
  26. function SentryDocumentTitle({
  27. title = '',
  28. orgSlug,
  29. projectSlug,
  30. noSuffix,
  31. children,
  32. }: Props) {
  33. function getPageTitle() {
  34. if (orgSlug && projectSlug) {
  35. return `${title} - ${orgSlug} - ${projectSlug}`;
  36. }
  37. if (orgSlug) {
  38. return `${title} - ${orgSlug}`;
  39. }
  40. if (projectSlug) {
  41. return `${title} - ${projectSlug}`;
  42. }
  43. return title;
  44. }
  45. const pageTitle = getPageTitle();
  46. const documentTitle = noSuffix
  47. ? pageTitle
  48. : pageTitle !== ''
  49. ? `${pageTitle} - Sentry`
  50. : 'Sentry';
  51. return <DocumentTitle title={documentTitle}>{children}</DocumentTitle>;
  52. }
  53. export default SentryDocumentTitle;