123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import {createContext, useContext, useEffect, useMemo} from 'react';
- type Props = {
- children?: React.ReactNode;
-
- noSuffix?: boolean;
-
- orgSlug?: string;
-
- projectSlug?: string;
-
- title?: string;
- };
- const DEFAULT_PAGE_TITLE = 'Sentry';
- const DocumentTitleContext = createContext(DEFAULT_PAGE_TITLE);
- function SentryDocumentTitle({
- title = '',
- orgSlug,
- projectSlug,
- noSuffix,
- children,
- }: Props) {
- const parentTitle = useContext(DocumentTitleContext);
- const pageTitle = useMemo(() => {
- if (orgSlug && projectSlug) {
- return `${title} — ${orgSlug} — ${projectSlug}`;
- }
- if (orgSlug) {
- return `${title} — ${orgSlug}`;
- }
- if (projectSlug) {
- return `${title} — ${projectSlug}`;
- }
- return title;
- }, [orgSlug, projectSlug, title]);
- const documentTitle = useMemo(() => {
- if (noSuffix) {
- return pageTitle;
- }
- if (pageTitle !== '') {
- return `${pageTitle} — Sentry`;
- }
- return DEFAULT_PAGE_TITLE;
- }, [noSuffix, pageTitle]);
-
-
-
-
- if (document.title !== documentTitle) {
- document.title = documentTitle;
- }
- useEffect(() => {
- return () => {
- document.title = parentTitle;
- };
- }, [parentTitle]);
- return (
- <DocumentTitleContext.Provider value={documentTitle}>
- {children}
- </DocumentTitleContext.Provider>
- );
- }
- export default SentryDocumentTitle;
|