Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SLB-410): disable language switcher if no translations are available #282

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions packages/ui/src/components/Molecules/LanguageSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use client';
import { Link, Locale, useLocation } from '@custom/schema';
import { Menu, Transition } from '@headlessui/react';
import {
Menu,
MenuButton,
MenuItem,
MenuItems,
Transition,
} from '@headlessui/react';
import { ChevronDownIcon } from '@heroicons/react/20/solid';
import clsx from 'clsx';
import React, { Fragment } from 'react';
Expand All @@ -18,24 +24,30 @@ export function LanguageSwitcher() {
const translations = useTranslations();
const [location] = useLocation();

const currentLocale = Object.entries(translations).find(
([, path]) => path === location.pathname,
)?.[0];
const isMultiLingual = Object.keys(translations).length > 1;

return (
<div className="relative inline-block text-left">
<Menu as="div" className="relative inline-block text-left">
<div>
{Object.values(Locale).map((locale) => (
<React.Fragment key={locale}>
{translations[locale] &&
location.pathname !== translations[locale] ? null : (
<Menu.Button className="inline-flex justify-center w-full rounded-md bg-white text-sm hover:text-blue-600">
{getLanguageName(locale)}
<ChevronDownIcon
className="ml-1 h-5 w-5"
aria-hidden="true"
/>
</Menu.Button>
<React.Fragment key={currentLocale}>
<MenuButton
className={clsx(
'inline-flex justify-center w-full rounded-md bg-white text-sm',
{
'hover:text-blue-600': isMultiLingual,
'cursor-not-allowed opacity-70': !isMultiLingual,
},
)}
</React.Fragment>
))}
disabled={!isMultiLingual}
>
{getLanguageName(currentLocale ?? 'en')}
<ChevronDownIcon className="ml-1 h-5 w-5" aria-hidden="true" />
</MenuButton>
</React.Fragment>
</div>

<Transition
Expand All @@ -47,13 +59,13 @@ export function LanguageSwitcher() {
leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 translate-y-1"
>
<Menu.Items className="origin-top-right absolute z-50 right-0 mt-3 w-48 rounded bg-white shadow-md ring-1 ring-gray-100">
<MenuItems className="origin-top-right absolute z-50 right-0 mt-3 w-48 rounded bg-white shadow-md ring-1 ring-gray-100">
<div className="py-1">
{Object.values(Locale).map((locale) => (
<React.Fragment key={locale}>
{translations[locale] &&
location.pathname !== translations[locale] ? (
<Menu.Item>
<MenuItem>
{({ focus }) =>
translations[locale] ? (
<Link
Expand All @@ -78,12 +90,12 @@ export function LanguageSwitcher() {
</span>
)
}
</Menu.Item>
</MenuItem>
) : null}
</React.Fragment>
))}
</div>
</Menu.Items>
</MenuItems>
</Transition>
</Menu>
</div>
Expand Down
Loading