- "frontend/src/hooks/useDocumentTitle.ts" - "frontend/src/pages/Home.tsx" - "frontend/src/pages/TopicsBrowse.tsx" - "frontend/src/pages/SubTopicPage.tsx" - "frontend/src/pages/CreatorsBrowse.tsx" - "frontend/src/pages/CreatorDetail.tsx" - "frontend/src/pages/TechniquePage.tsx" - "frontend/src/pages/SearchResults.tsx" GSD-Task: S04/T02
22 lines
558 B
TypeScript
22 lines
558 B
TypeScript
import { useEffect, useRef } from "react";
|
|
|
|
const DEFAULT_TITLE = "Chrysopedia";
|
|
|
|
/**
|
|
* Sets `document.title` to the given value. Resets to the default
|
|
* title on unmount so navigating away doesn't leave a stale tab name.
|
|
*/
|
|
export function useDocumentTitle(title: string): void {
|
|
const prevTitle = useRef(document.title);
|
|
|
|
useEffect(() => {
|
|
document.title = title || DEFAULT_TITLE;
|
|
}, [title]);
|
|
|
|
useEffect(() => {
|
|
const fallback = prevTitle.current;
|
|
return () => {
|
|
document.title = fallback || DEFAULT_TITLE;
|
|
};
|
|
}, []);
|
|
}
|