From 08839420936edb15a1d3e6ea29547ef0b81400e3 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 25 Oct 2023 18:16:25 +0200 Subject: [PATCH] fix randomized titles --- src/hooks/useRandomTranslation.ts | 17 ++++++++++++----- src/pages/parts/home/HeroPart.tsx | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/hooks/useRandomTranslation.ts b/src/hooks/useRandomTranslation.ts index fcda150a..90c4cdc9 100644 --- a/src/hooks/useRandomTranslation.ts +++ b/src/hooks/useRandomTranslation.ts @@ -1,15 +1,22 @@ +import { useCallback, useMemo } from "react"; import { useTranslation } from "react-i18next"; export function useRandomTranslation() { const { t } = useTranslation(); + const seed = useMemo(() => Math.random(), []); - const getRandomTranslation = (key: string) => { - const res = t(key, { returnObjects: true }); + const getRandomTranslation = useCallback( + (key: string) => { + const res = t(key, { returnObjects: true }); - if (Array.isArray(res)) return res[Math.floor(Math.random() * res.length)]; + if (Array.isArray(res)) { + return res[Math.floor(seed * res.length)]; + } - return res; - }; + return res; + }, + [t, seed] + ); return { t: getRandomTranslation }; } diff --git a/src/pages/parts/home/HeroPart.tsx b/src/pages/parts/home/HeroPart.tsx index 1ab20fab..d45b3fe0 100644 --- a/src/pages/parts/home/HeroPart.tsx +++ b/src/pages/parts/home/HeroPart.tsx @@ -30,7 +30,8 @@ export function HeroPart({ setIsSticky, searchParams }: HeroPartProps) { let time = "night"; const hour = new Date().getHours(); if (hour < 12) time = "morning"; - if (hour < 19) time = "day"; + else if (hour < 19) time = "day"; + const title = t(`search.title.${time}`); return (