Skip to content

Commit aa5f1c6

Browse files
committed
refactor(#7646): update the select component to have a default placeholder of the window title in site
1 parent 927d6b3 commit aa5f1c6

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

apps/site/components/withSidebar.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { RichTranslationValues } from 'next-intl';
77
import type { FC } from 'react';
88

99
import Link from '#site/components/Link';
10+
import { useDocumentTitle } from '#site/hooks';
1011
import { useSiteNavigation } from '#site/hooks/server';
1112
import { useRouter } from '#site/navigation.mjs';
1213
import type { NavigationKeys } from '#site/types';
@@ -23,6 +24,7 @@ const WithSidebar: FC<WithSidebarProps> = ({ navKeys, context, ...props }) => {
2324
const locale = useLocale();
2425
const t = useTranslations();
2526
const { push } = useRouter();
27+
const pageTitle = useDocumentTitle();
2628
const sideNavigation = getSideNavigation(navKeys, context);
2729

2830
const mappedSidebarItems =
@@ -40,6 +42,7 @@ const WithSidebar: FC<WithSidebarProps> = ({ navKeys, context, ...props }) => {
4042
groups={mappedSidebarItems}
4143
pathname={pathname.replace(`/${locale}`, '')}
4244
title={t('components.common.sidebar.title')}
45+
placeholder={pageTitle}
4346
onSelect={push}
4447
as={Link}
4548
{...props}

apps/site/hooks/react-client/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { default as useMediaQuery } from './useMediaQuery';
44
export { default as useNotification } from './useNotification';
55
export { default as useClientContext } from './useClientContext';
66
export { default as useNavigationState } from './useNavigationState';
7+
export { default as useDocumentTitle } from './useDocumentTitle';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { usePathname } from 'next/navigation';
2+
import { useEffect, useState } from 'react';
3+
4+
const useDocumentTitle = (): string => {
5+
const pathname = usePathname();
6+
const [title, setTitle] = useState<string>(window.document.title);
7+
8+
useEffect(() => {
9+
setTitle(window.document.title);
10+
}, [pathname]);
11+
12+
return title;
13+
};
14+
15+
export default useDocumentTitle;

packages/ui-components/Containers/Sidebar/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type SidebarProps = {
1515
onSelect: (value: string) => void;
1616
as?: LinkLike;
1717
showProgressionIcons?: boolean;
18+
placeholder?: string;
1819
};
1920

2021
const SideBar: FC<PropsWithChildren<SidebarProps>> = ({
@@ -25,16 +26,16 @@ const SideBar: FC<PropsWithChildren<SidebarProps>> = ({
2526
as,
2627
showProgressionIcons = false,
2728
children,
29+
placeholder,
2830
}) => {
2931
const selectItems = groups.map(({ items, groupName }) => ({
3032
label: groupName,
3133
items: items.map(({ label, link }) => ({ value: link, label })),
3234
}));
3335

34-
const currentItem =
35-
selectItems
36-
.flatMap(item => item.items)
37-
.find(item => pathname === item.value) || selectItems[0]?.items[0];
36+
const currentItem = selectItems
37+
.flatMap(item => item.items)
38+
.find(item => pathname === item.value);
3839

3940
return (
4041
<aside className={styles.wrapper}>
@@ -45,6 +46,7 @@ const SideBar: FC<PropsWithChildren<SidebarProps>> = ({
4546
label={title}
4647
values={selectItems}
4748
defaultValue={currentItem?.value}
49+
placeholder={placeholder}
4850
onChange={onSelect}
4951
className={styles.mobileSelect}
5052
/>

0 commit comments

Comments
 (0)