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 23cd471
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 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;
10 changes: 9 additions & 1 deletion packages/ui/src/components/Routes/Page.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Locale, registerExecutor, Url, ViewPageQuery } from '@custom/schema';
import {
FrameQuery,
Locale,
registerExecutor,
Url,
ViewPageQuery,
} from '@custom/schema';
import Landscape from '@stories/landscape.jpg?as=metadata';
import { Meta, StoryObj } from '@storybook/react';
import React from 'react';

import { image } from '../../helpers/image';
import { Mixed, Paragraph } from '../Organisms/PageContent/BlockMarkup.stories';
import { WithCaption } from '../Organisms/PageContent/BlockMedia.stories';
import { Default as FrameStory } from './Frame.stories';
import { Page } from './Page';

export default {
Expand All @@ -15,6 +22,7 @@ export default {
export const Default = {
render: (args) => {
registerExecutor(ViewPageQuery, () => args);
registerExecutor(FrameQuery, FrameStory.args);
return <Page />;
},
args: {
Expand Down
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 (
a &&
b &&
Object.keys(a).length === Object.keys(b).length &&
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}` as Url]),
);

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

0 comments on commit 23cd471

Please sign in to comment.