From 0d2265c57e18039bfc5fe991a652f2f6531b5632 Mon Sep 17 00:00:00 2001 From: Danny Koppenhagen Date: Mon, 19 Jun 2023 20:35:58 +0200 Subject: [PATCH] refactor: prevent duplicated title for index page by making prop optional --- dist_scripts/src/config.js | 8 +++++--- scripts/createStaticFiles.ts | 4 ++-- src/components/PageIndex/PageIndex.tsx | 4 ++-- src/components/SetTitle.tsx | 4 ++-- src/config.ts | 13 ++++++++----- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/dist_scripts/src/config.js b/dist_scripts/src/config.js index 2f83e0f..e245f46 100644 --- a/dist_scripts/src/config.js +++ b/dist_scripts/src/config.js @@ -5,9 +5,11 @@ exports.radarName = process.env.REACT_APP_RADAR_NAME || "AOE Technology Radar"; exports.radarNameShort = exports.radarName; exports.titleFormat = process.env.REACT_APP_RADAR_TITLE_FORMAT || "%TECHNOLOGY_NAME% | %APP_TITLE%"; function setTitle(document, title) { - document.title = exports.titleFormat - .replace("%TECHNOLOGY_NAME%", title) - .replace("%APP_TITLE%", exports.radarName); + document.title = title + ? exports.titleFormat + .replace("%TECHNOLOGY_NAME%", title) + .replace("%APP_TITLE%", exports.radarName) + : exports.radarName; } exports.setTitle = setTitle; var getItemPageNames = function (items) { diff --git a/scripts/createStaticFiles.ts b/scripts/createStaticFiles.ts index 2a24af8..92da220 100644 --- a/scripts/createStaticFiles.ts +++ b/scripts/createStaticFiles.ts @@ -56,9 +56,9 @@ const createStaticFiles = async () => { const document = dom.window.document; const rootEl = document.getElementById("root"); - document.title = 'test' + document.title = "test"; - setTitle(document, item.title) + setTitle(document, item.title); if (rootEl) { const textNode = document.createElement("div"); diff --git a/src/components/PageIndex/PageIndex.tsx b/src/components/PageIndex/PageIndex.tsx index eab116d..adba480 100644 --- a/src/components/PageIndex/PageIndex.tsx +++ b/src/components/PageIndex/PageIndex.tsx @@ -1,6 +1,6 @@ import { MomentInput } from "moment"; -import { ConfigData, radarName, radarNameShort } from "../../config"; +import { ConfigData, radarName } from "../../config"; import { useMessages } from "../../context/MessagesContext"; import { formatRelease } from "../../date"; import { HomepageOption, Item, featuredOnly } from "../../model"; @@ -38,7 +38,7 @@ export default function PageIndex({ config.homepageContent === HomepageOption.both; return ( - +
{radarName} diff --git a/src/components/SetTitle.tsx b/src/components/SetTitle.tsx index 9da3a27..171eb7d 100644 --- a/src/components/SetTitle.tsx +++ b/src/components/SetTitle.tsx @@ -3,12 +3,12 @@ import { useEffect } from "react"; import { setTitle } from "../config"; type SetTitleProps = { - title: string; + title?: string; }; export default function SetTitle({ title }: SetTitleProps) { useEffect(() => { - setTitle(document, title) + setTitle(document, title); }, [title]); return null; diff --git a/src/config.ts b/src/config.ts index 04dad0c..6e26b39 100644 --- a/src/config.ts +++ b/src/config.ts @@ -23,12 +23,15 @@ export interface ConfigData { export const radarName = process.env.REACT_APP_RADAR_NAME || "AOE Technology Radar"; export const radarNameShort = radarName; -export const titleFormat = process.env.REACT_APP_RADAR_TITLE_FORMAT || "%TECHNOLOGY_NAME% | %APP_TITLE%" +export const titleFormat = + process.env.REACT_APP_RADAR_TITLE_FORMAT || "%TECHNOLOGY_NAME% | %APP_TITLE%"; -export function setTitle(document: Document, title: string) { - document.title = titleFormat - .replace('%TECHNOLOGY_NAME%', title) - .replace('%APP_TITLE%', radarName) +export function setTitle(document: Document, title?: string) { + document.title = title + ? titleFormat + .replace("%TECHNOLOGY_NAME%", title) + .replace("%APP_TITLE%", radarName) + : radarName; } export const getItemPageNames = (items: Item[]) =>