Bin
2025-12-17 2b99d77d73ba568beff0a549534017caaad8a6de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useEffect } from "react";
 
export const PAGE_TITLE_SEPARATOR = " | ";
 
export const getBasePageTitle = () => {
  return ((window.APP_SETTINGS as Record<string, unknown>)?.page_title as string)?.split(PAGE_TITLE_SEPARATOR).at(-1);
};
 
export const getPageTitle = (pageTitle?: string) => {
  const baseTitle = getBasePageTitle();
  return pageTitle && baseTitle ? `${pageTitle}${PAGE_TITLE_SEPARATOR}${baseTitle}` : pageTitle || baseTitle;
};
 
export const createTitleFromSegments = (titleSegments: (string | undefined)[]) => {
  return titleSegments.filter(Boolean).join(PAGE_TITLE_SEPARATOR);
};
 
export const setPageTitle = (pageTitle?: string, includeBaseTitle = true) => {
  const title = includeBaseTitle ? getPageTitle(pageTitle) : pageTitle;
  if (title && typeof document !== "undefined") {
    document.title = title;
  }
};
 
export const useUpdatePageTitle = (title?: string, includeBaseTitle = true) => {
  useEffect(() => {
    if (!title) return;
    // Debounce title update to avoid flickering when navigating between pages
    const requestId = requestAnimationFrame(() => {
      setPageTitle(title, includeBaseTitle);
    });
    return () => {
      if (requestId) cancelAnimationFrame(requestId);
    };
  }, [title, includeBaseTitle]);
};