Skip to content

Commit 732b206

Browse files
committed
ref(js): Remove usage of react-document-title
1 parent 7ba8c59 commit 732b206

File tree

4 files changed

+86
-39
lines changed

4 files changed

+86
-39
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"@types/prismjs": "^1.26.0",
7171
"@types/react": "~17.0.14",
7272
"@types/react-date-range": "^1.4.4",
73-
"@types/react-document-title": "^2.0.5",
7473
"@types/react-dom": "~17.0.9",
7574
"@types/react-grid-layout": "^1.3.2",
7675
"@types/react-mentions": "4.1.6",
@@ -140,7 +139,6 @@
140139
"react": "17.0.2",
141140
"react-autosize-textarea": "7.1.0",
142141
"react-date-range": "^1.4.0",
143-
"react-document-title": "2.0.3",
144142
"react-dom": "17.0.2",
145143
"react-grid-layout": "^1.3.4",
146144
"react-lazyload": "^2.3.0",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {render} from 'sentry-test/reactTestingLibrary';
2+
3+
import SentryDocumentTitle from './sentryDocumentTitle';
4+
5+
describe('SentryDocumentTitle', () => {
6+
it('sets the docuemnt title', () => {
7+
render(<SentryDocumentTitle title="This is a test" />);
8+
expect(document.title).toBe('This is a test - Sentry');
9+
});
10+
11+
it('adds a organization slug', () => {
12+
render(<SentryDocumentTitle orgSlug="org" title="This is a test" />);
13+
expect(document.title).toBe('This is a test - org - Sentry');
14+
});
15+
16+
it('adds a project slug', () => {
17+
render(<SentryDocumentTitle projectSlug="project" title="This is a test" />);
18+
expect(document.title).toBe('This is a test - project - Sentry');
19+
});
20+
21+
it('adds a organization and project slug', () => {
22+
render(
23+
<SentryDocumentTitle orgSlug="org" projectSlug="project" title="This is a test" />
24+
);
25+
expect(document.title).toBe('This is a test - org - project - Sentry');
26+
});
27+
28+
it('sets the title without suffix', () => {
29+
render(<SentryDocumentTitle title="This is a test" noSuffix />);
30+
expect(document.title).toBe('This is a test');
31+
});
32+
33+
it('reverts to the parent title', () => {
34+
const {rerender} = render(
35+
<SentryDocumentTitle title="This is a test">
36+
<SentryDocumentTitle title="child title">Content</SentryDocumentTitle>
37+
</SentryDocumentTitle>
38+
);
39+
40+
expect(document.title).toBe('child title - Sentry');
41+
42+
rerender(
43+
<SentryDocumentTitle title="This is a test">new Content</SentryDocumentTitle>
44+
);
45+
46+
expect(document.title).toBe('This is a test - Sentry');
47+
});
48+
});

static/app/components/sentryDocumentTitle.tsx

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DocumentTitle from 'react-document-title';
1+
import {createContext, useContext, useEffect, useMemo} from 'react';
22

33
type Props = {
44
children?: React.ReactChild;
@@ -21,6 +21,10 @@ type Props = {
2121
title?: string;
2222
};
2323

24+
const DEFAULT_PAGE_TITLE = 'Sentry';
25+
26+
const DocumentTitleContext = createContext(DEFAULT_PAGE_TITLE);
27+
2428
/**
2529
* Assigns the document title. The deepest nested version of this title will be
2630
* the one which is assigned.
@@ -32,7 +36,9 @@ function SentryDocumentTitle({
3236
noSuffix,
3337
children,
3438
}: Props) {
35-
function getPageTitle() {
39+
const parentTitle = useContext(DocumentTitleContext);
40+
41+
const pageTitle = useMemo(() => {
3642
if (orgSlug && projectSlug) {
3743
return `${title} - ${orgSlug} - ${projectSlug}`;
3844
}
@@ -46,17 +52,39 @@ function SentryDocumentTitle({
4652
}
4753

4854
return title;
49-
}
55+
}, [orgSlug, projectSlug, title]);
56+
57+
const documentTitle = useMemo(() => {
58+
if (noSuffix) {
59+
return pageTitle;
60+
}
61+
62+
if (pageTitle !== '') {
63+
return `${pageTitle} - Sentry`;
64+
}
5065

51-
const pageTitle = getPageTitle();
66+
return DEFAULT_PAGE_TITLE;
67+
}, [noSuffix, pageTitle]);
68+
69+
// NOTE: We do this OUTSIDE of a use effect so that the update order is
70+
// correct, otherwsie the inner most SentryDocumentTitle will have it's
71+
// useEffect called first followed by the parents, which will cause the wrong
72+
// title be set.
73+
if (document.title !== documentTitle) {
74+
document.title = documentTitle;
75+
}
5276

53-
const documentTitle = noSuffix
54-
? pageTitle
55-
: pageTitle !== ''
56-
? `${pageTitle} - Sentry`
57-
: 'Sentry';
77+
useEffect(() => {
78+
return () => {
79+
document.title = parentTitle;
80+
};
81+
}, [parentTitle]);
5882

59-
return <DocumentTitle title={documentTitle}>{children}</DocumentTitle>;
83+
return (
84+
<DocumentTitleContext.Provider value={documentTitle}>
85+
{children}
86+
</DocumentTitleContext.Provider>
87+
);
6088
}
6189

6290
export default SentryDocumentTitle;

yarn.lock

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,13 +2840,6 @@
28402840
"@types/react" "*"
28412841
date-fns "^2.16.1"
28422842

2843-
"@types/react-document-title@^2.0.5":
2844-
version "2.0.5"
2845-
resolved "https://registry.yarnpkg.com/@types/react-document-title/-/react-document-title-2.0.5.tgz#5093e25420c918066bb94efff1cb08a894289cce"
2846-
integrity sha512-EZ8uj09zBkqkKms2i0OPVYBb4iOLg+GWG1GegDOIDurQqJz+dc2ZMtJU+bgxmm01BjqHYq38XRj0/eC1kChLIw==
2847-
dependencies:
2848-
"@types/react" "*"
2849-
28502843
"@types/react-dom@*", "@types/react-dom@>=16", "@types/react-dom@~17.0.9":
28512844
version "17.0.9"
28522845
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.9.tgz#441a981da9d7be117042e1a6fd3dac4b30f55add"
@@ -9163,14 +9156,6 @@ react-date-range@^1.4.0:
91639156
react-list "^0.8.13"
91649157
shallow-equal "^1.2.1"
91659158

9166-
9167-
version "2.0.3"
9168-
resolved "https://registry.yarnpkg.com/react-document-title/-/react-document-title-2.0.3.tgz#bbf922a0d71412fc948245e4283b2412df70f2b9"
9169-
integrity sha1-u/kioNcUEvyUgkXkKDskEt9w8rk=
9170-
dependencies:
9171-
prop-types "^15.5.6"
9172-
react-side-effect "^1.0.2"
9173-
91749159
91759160
version "17.0.2"
91769161
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.2.tgz#ecffb6845e3ad8dbfcdc498f0d0a939736502c23"
@@ -9322,13 +9307,6 @@ react-shallow-renderer@^16.13.1:
93229307
object-assign "^4.1.1"
93239308
react-is "^16.12.0 || ^17.0.0"
93249309

9325-
react-side-effect@^1.0.2:
9326-
version "1.2.0"
9327-
resolved "https://registry.yarnpkg.com/react-side-effect/-/react-side-effect-1.2.0.tgz#0e940c78faba0c73b9b0eba9cd3dda8dfb7e7dae"
9328-
integrity sha512-v1ht1aHg5k/thv56DRcjw+WtojuuDHFUgGfc+bFHOWsF4ZK6C2V57DO0Or0GPsg6+LSTE0M6Ry/gfzhzSwbc5w==
9329-
dependencies:
9330-
shallowequal "^1.0.1"
9331-
93329310
93339311
version "1.7.0"
93349312
resolved "https://registry.yarnpkg.com/react-sparklines/-/react-sparklines-1.7.0.tgz#9b1d97e8c8610095eeb2ad658d2e1fcf91f91a60"
@@ -9820,11 +9798,6 @@ shallow-equal@^1.2.1:
98209798
resolved "https://registry.yarnpkg.com/shallow-equal/-/shallow-equal-1.2.1.tgz#4c16abfa56043aa20d050324efa68940b0da79da"
98219799
integrity sha512-S4vJDjHHMBaiZuT9NPb616CSmLf618jawtv3sufLl6ivK8WocjAo58cXwbRV1cgqxH0Qbv+iUt6m05eqEa2IRA==
98229800

9823-
shallowequal@^1.0.1:
9824-
version "1.1.0"
9825-
resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"
9826-
integrity sha512-y0m1JoUZSlPAjXVtPPW70aZWfIL/dSP7AFkRnniLCrK/8MDKog3TySTBmckD+RObVxH0v4Tox67+F14PdED2oQ==
9827-
98289801
shebang-command@^2.0.0:
98299802
version "2.0.0"
98309803
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"

0 commit comments

Comments
 (0)