Skip to content

Commit

Permalink
fix(SLB-269): special handling for home page translations
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Mar 15, 2024
1 parent 006c5b1 commit 09242e2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/schema/src/operations/Frame.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
query Frame {
websiteSettings {
homePage {
translations {
locale
path
}
}
}
mainNavigation: mainNavigations {
...Navigation
}
Expand Down
36 changes: 30 additions & 6 deletions packages/ui/src/components/Molecules/LanguageSwitcher.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { Url } from '@custom/schema';
import { FrameQuery, registerExecutor, Url } from '@custom/schema';
import { Decorator, Meta, StoryObj } from '@storybook/react';
import React from 'react';

import { Translations, TranslationsProvider } from '../../utils/translations';
import { Default } from '../Routes/Frame.stories';
import { LanguageSwitcher } from './LanguageSwitcher';

const TranslationsDecorator = ((Story, ctx) => (
<TranslationsProvider defaultTranslations={ctx.args}>
<Story />
</TranslationsProvider>
)) as Decorator<Translations>;
const TranslationsDecorator = ((Story, ctx) => {
registerExecutor(FrameQuery, {
...Default.args,
websiteSettings: {
homePage: {
translations: [
{ locale: 'en', path: '/en/home' as Url },
{ locale: 'de', path: '/de/home' as Url },
],
},
},
});
return (
<TranslationsProvider defaultTranslations={ctx.args}>
<Story />
</TranslationsProvider>
);
}) as Decorator<Translations>;

export default {
component: LanguageSwitcher,
Expand All @@ -35,3 +49,13 @@ export const Full = {
en: '/en/english-version' as Url,
},
} satisfies Story;

export const Homepage = {
args: {
de: '/de/home' as Url,
en: '/en/home' as Url,
},
parameters: {
location: new URL('local:/de'),
},
} satisfies Story;
24 changes: 21 additions & 3 deletions packages/ui/src/utils/translations.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Locale, Url } from '@custom/schema';
import { FrameQuery, Locale, Url } from '@custom/schema';
import React, {
createContext,
PropsWithChildren,
Expand All @@ -7,6 +7,9 @@ import React, {
useState,
} from 'react';

import { isTruthy } from './isTruthy';
import { useOperation } from './operation';

/**
* A list of translations for the given page.
* A translations consists of the locale and the corresponding path.
Expand Down Expand Up @@ -36,15 +39,30 @@ export function TranslationsProvider({
}

function deepCompare(a: any, b: any) {
return JSON.stringify(a) === JSON.stringify(b);
return Object.keys(a).every((key) => a[key] === b[key]);
}

export function useTranslations(newTranslations?: Translations) {
const homeTranslations = Object.fromEntries(
useOperation(FrameQuery)
.data?.websiteSettings?.homePage?.translations?.filter(isTruthy)
.map(({ locale, path }) => [locale, path]) || [],
);
const { setTranslations, translations } = useContext(TranslationsContext);
useEffect(() => {
if (newTranslations && !deepCompare(translations, newTranslations)) {
setTranslations(newTranslations);
}
}, [setTranslations, newTranslations, translations]);
return translations;

const homePaths = Object.fromEntries(
Object.values(Locale).map((locale) => [locale, `/${locale}`]),
);

console.log(
{ translations, homeTranslations, homePaths },
deepCompare(homeTranslations, translations),
);

return deepCompare(homeTranslations, translations) ? homePaths : translations;
}

0 comments on commit 09242e2

Please sign in to comment.