From 38112eb49d1aab3bf7e37713e1bba1c0282a2180 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Fri, 27 Sep 2024 13:36:29 +0200 Subject: [PATCH] docs(useLanguage): add doc comments to hook functions --- src/utils/hooks/useLanguage.ts | 48 ++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/utils/hooks/useLanguage.ts b/src/utils/hooks/useLanguage.ts index bdbdfbd8b..289553385 100644 --- a/src/utils/hooks/useLanguage.ts +++ b/src/utils/hooks/useLanguage.ts @@ -7,6 +7,13 @@ import { LanguageObject } from "studio/lib/interfaces/supportedLanguages"; import { LANGUAGES_QUERY } from "studio/lib/queries/languages"; import { SLUG_TRANSLATIONS_FROM_LANGUAGE_QUERY } from "studio/lib/queries/slugTranslations"; +/** + * Client hook providing access to the available Sanity translations for the given slug + * + * @param currentLanguage the language of the given slug + * @param slug slug to translate + * @param availableLanguages languages with possible slug translation + */ function useSlugTranslations( currentLanguage: LanguageObject | undefined, slug: string, @@ -28,29 +35,38 @@ function useSlugTranslations( ).then(setSlugTranslationsData); }, [currentLanguage, slug]); - return ( + const allSlugTranslations = slug === "" ? availableLanguages?.map((lang) => ({ slug: "", language: lang.id, })) - : slugTranslationsData?._translations - ) - ?.filter( - (translation) => - translation && translation.language !== currentLanguage?.id, - ) - ?.map( - (translation) => - translation && { - slug: `/${translation.language}/${translation.slug}`, - language: availableLanguages?.find( - (lang) => lang.id === translation.language, - ), - }, - ); + : slugTranslationsData?._translations; + + const otherSlugTranslations = allSlugTranslations?.filter( + (translation) => + translation && translation.language !== currentLanguage?.id, + ); + + // include full language object, not just id, in slug translation + return otherSlugTranslations?.map( + (translation) => + translation && { + slug: `/${translation.language}/${translation.slug}`, + language: availableLanguages?.find( + (lang) => lang.id === translation.language, + ), + }, + ); } +/** + * Client hook providing access to: + * - the currently selected language from URL + * - all enabled languages in Sanity + * - default language selected in Sanity + * - available translations of the current page slug + */ export default function useLanguage() { const pathname = usePathname();