Skip to content

Commit

Permalink
docs(useLanguage): add doc comments to hook functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 27, 2024
1 parent 416cef9 commit 38112eb
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/utils/hooks/useLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

Expand Down

0 comments on commit 38112eb

Please sign in to comment.