mirror of
https://github.com/dergigi/boris.git
synced 2026-02-17 21:15:02 +01:00
- Add useDocumentTitle hook to manage document title dynamically - Update useArticleLoader to set title when articles load - Update useExternalUrlLoader to set title for external URLs/videos - Update useEventLoader to set title for events - Reset title to default when navigating away from content - Browser title now shows article/video title instead of always 'Boris'
36 lines
956 B
TypeScript
36 lines
956 B
TypeScript
import { useEffect, useRef } from 'react'
|
|
|
|
const DEFAULT_TITLE = 'Boris - Read, Highlight, Explore'
|
|
|
|
interface UseDocumentTitleProps {
|
|
title?: string
|
|
fallback?: string
|
|
}
|
|
|
|
export function useDocumentTitle({ title, fallback }: UseDocumentTitleProps) {
|
|
const originalTitleRef = useRef<string>(document.title)
|
|
|
|
useEffect(() => {
|
|
// Store the original title on first mount
|
|
if (originalTitleRef.current === DEFAULT_TITLE) {
|
|
originalTitleRef.current = document.title
|
|
}
|
|
|
|
// Set the new title if provided, otherwise use fallback or default
|
|
const newTitle = title || fallback || DEFAULT_TITLE
|
|
document.title = newTitle
|
|
|
|
// Cleanup: restore original title when component unmounts
|
|
return () => {
|
|
document.title = originalTitleRef.current
|
|
}
|
|
}, [title, fallback])
|
|
|
|
// Return a function to manually reset to default
|
|
const resetTitle = () => {
|
|
document.title = DEFAULT_TITLE
|
|
}
|
|
|
|
return { resetTitle }
|
|
}
|