Skip to content

Commit 732c607

Browse files
CORE-1304: add new titles for REX modals (#2582)
* add new titles for REX modals * resolve comments --------- Co-authored-by: staxly[bot] <35789409+staxly[bot]@users.noreply.github.com>
1 parent bda0a12 commit 732c607

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

src/app/content/hooks/receiveContent.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import queryString from 'query-string';
12
import { setHead } from '../../head/actions';
23
import { initialState as headInitialState } from '../../head/reducer';
34
import { Link } from '../../head/types';
45
import createIntl from '../../messages/createIntl';
56
import { locationChange } from '../../navigation/actions';
6-
import { pathname } from '../../navigation/selectors';
7+
import { pathname, query } from '../../navigation/selectors';
78
import theme from '../../theme';
89
import { ActionHookBody } from '../../types';
910
import { receivePage } from '../actions';
@@ -41,6 +42,8 @@ const hookBody: ActionHookBody<typeof receivePage | typeof locationChange> = (se
4142
const loadingBook = select.loadingBook(state);
4243
const loadingPage = select.loadingPage(state);
4344
const currentPath = pathname(state);
45+
const queryParams = queryString.stringify(query(state));
46+
const queryParamsWithPrefix = queryParams ? `?${queryParams}` : '';
4447

4548
if (!page || !book) {
4649
dispatch(
@@ -57,7 +60,7 @@ const hookBody: ActionHookBody<typeof receivePage | typeof locationChange> = (se
5760

5861
const locale = book.language;
5962
const intl = await createIntl(locale);
60-
const title = createTitle(page, book, intl);
63+
const title = createTitle(page, book, intl, queryParamsWithPrefix);
6164
const description = getPageDescription(services, intl, book, page);
6265
const canonical = await getCanonicalUrlParams(archiveLoader, osWebLoader, book, page.id);
6366
const canonicalUrl = canonical && contentRoute.getUrl(canonical);

src/app/content/utils/seoUtils.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,87 @@ describe('createTitle', () => {
144144
expect(title).toEqual('3 page1 - book | OpenStax');
145145
});
146146
});
147+
describe('createTitle (modal param)', () => {
148+
const intl = createIntl();
149+
150+
it('returns modal title for MH modal param', () => {
151+
const page = makeArchiveSection('page1');
152+
const book = {
153+
title: 'book',
154+
tree: makeArchiveTree('book', [page]),
155+
};
156+
// params string with modal=MH
157+
const params = 'modal=MH';
158+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
159+
expect(title).toEqual('My Highlights and Notes | OpenStax');
160+
});
161+
162+
it('returns modal title for KS modal param', () => {
163+
const page = makeArchiveSection('page1');
164+
const book = {
165+
title: 'book',
166+
tree: makeArchiveTree('book', [page]),
167+
};
168+
// params string with modal=KS
169+
const params = 'modal=KS';
170+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
171+
expect(title).toEqual('REX Keyboard Shortcuts | OpenStax');
172+
});
173+
174+
it('returns modal title for PQ modal param', () => {
175+
const page = makeArchiveSection('page1');
176+
const book = {
177+
title: 'book',
178+
tree: makeArchiveTree('book', [page]),
179+
};
180+
// params string with modal=PQ
181+
const params = 'modal=PQ';
182+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
183+
expect(title).toEqual('REX Practice Questions | OpenStax');
184+
});
185+
186+
it('returns modal title for SG modal param', () => {
187+
const page = makeArchiveSection('page1');
188+
const book = {
189+
title: 'book',
190+
tree: makeArchiveTree('book', [page]),
191+
};
192+
// params string with modal=SG
193+
const params = 'modal=SG';
194+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
195+
expect(title).toEqual('REX Study Guides | OpenStax');
196+
});
197+
198+
it('returns normal title if modal param is not present', () => {
199+
const page = makeArchiveSection('page1');
200+
const book = {
201+
title: 'book',
202+
tree: makeArchiveTree('book', [page]),
203+
};
204+
const params = '';
205+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
206+
expect(title).toEqual('page1 - book | OpenStax');
207+
});
208+
209+
it('returns normal title if modal param is unknown', () => {
210+
const page = makeArchiveSection('page1');
211+
const book = {
212+
title: 'book',
213+
tree: makeArchiveTree('book', [page]),
214+
};
215+
const params = 'modal=UNKNOWN';
216+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
217+
expect(title).toEqual('page1 - book | OpenStax');
218+
});
219+
220+
it('returns modal title if modal param is present among other params', () => {
221+
const page = makeArchiveSection('page1');
222+
const book = {
223+
title: 'book',
224+
tree: makeArchiveTree('book', [page]),
225+
};
226+
const params = 'foo=bar&modal=MH&baz=qux';
227+
const title = createTitle(page as any as Page, book as any as Book, intl, params);
228+
expect(title).toEqual('My Highlights and Notes | OpenStax');
229+
});
230+
});

src/app/content/utils/seoUtils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,28 @@ export const getPageDescription = (services: Pick<AppServices, 'archiveLoader'>,
111111
return pageDescription || intl.formatMessage({id: 'i18n:metadata:description'});
112112
};
113113

114-
export const createTitle = (page: Page, book: Book, intl: IntlShape): string => {
114+
const modalTitles = {
115+
'MH': 'My Highlights and Notes',
116+
'KS': 'REX Keyboard Shortcuts',
117+
'PQ': 'REX Practice Questions',
118+
'SG': 'REX Study Guides',
119+
};
120+
121+
export const createTitle = (page: Page, book: Book, intl: IntlShape, params?: string): string => {
115122
const node = assertDefined(
116123
findArchiveTreeNodeById(book.tree, page.id),
117124
`couldn't find node for a page id: ${page.id}`
118125
);
119126
const [nodeNumber, nodeTitle] = splitTitleParts(node.title);
120127
const title = `${nodeTitle} - ${book.title} | OpenStax`;
121-
128+
const searchParams = params ? new URLSearchParams(params) : undefined;
129+
const modalParam = searchParams?.get('modal');
130+
const modalTitle = modalParam && modalParam in modalTitles
131+
? modalTitles[modalParam as keyof typeof modalTitles]
132+
: null;
133+
if (modalTitle) {
134+
return `${modalTitle} | OpenStax`;
135+
}
122136
if (nodeNumber) {
123137
return `${nodeNumber} ${title}`;
124138
}

0 commit comments

Comments
 (0)