Skip to content

Commit

Permalink
refactor: one context to rule them all
Browse files Browse the repository at this point in the history
  • Loading branch information
cemms1 committed Aug 31, 2023
1 parent 8342952 commit 3f494e9
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 32 deletions.
11 changes: 0 additions & 11 deletions dotcom-rendering/.storybook/decorators/RenderingTargetDecorator.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { RenderingContext } from '../../src/components/RenderingContext';

const defaultContext = { target: 'Web' };

export const RenderingContextDecorator = (
Story,
{ args: { renderingContext } },
) => {
const context = { ...defaultContext, ...renderingContext };

// For easy debugging
console.log('Storybook rendering context: \n', context);

return (
<RenderingContext.Provider value={context}>
<Story />
</RenderingContext.Provider>
);
};
16 changes: 12 additions & 4 deletions dotcom-rendering/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Lazy } from '../src/components/Lazy';
import { Picture } from '../src/components/Picture';
import { mockRESTCalls } from '../src/lib/mockRESTCalls';
import { setABTests } from '../src/lib/useAB';
import { RenderingContextDecorator } from './decorators/renderingContextDecorator';

// Prevent components being lazy rendered when we're taking Chromatic snapshots
Lazy.disabled = isChromatic();
Expand Down Expand Up @@ -139,10 +140,17 @@ const guardianViewports = {
/** @type {import('@storybook/react').Preview} */
export default {
decorators: [
(Story) => {
storage.local.clear();
return Story();
},
RenderingContextDecorator,
// (Story) => {
// <>
// <h1>HELLO!</h1>
// <Story />
// </>;
// },
// (Story) => {
// storage.local.clear();
// return Story();
// },
],
};

Expand Down
26 changes: 26 additions & 0 deletions dotcom-rendering/src/components/ArticleMeta.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ export const ArticleStory = () => {
);
};

export const ArticleAppsStory = () => {
return (
<Wrapper>
<ArticleMeta
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
pageId=""
webTitle=""
byline="Lanre Bakare"
tags={tagsWithLargeBylineImage}
primaryDateline="Sun 12 Jan 2020 18.00 GMT"
secondaryDateline="Last modified on Sun 12 Jan 2020 21.00 GMT"
isCommentable={false}
discussionApiUrl=""
shortUrlId=""
ajaxUrl=""
showShareCount={true}
/>
</Wrapper>
);
};
ArticleAppsStory.args = { renderingContext: { target: 'Apps' } };

export const BrandingStory = () => {
return (
<Wrapper>
Expand Down
7 changes: 3 additions & 4 deletions dotcom-rendering/src/components/BylineLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import type { TagType } from '../types/tag';
import { FollowWrapper } from './FollowWrapper.importable';
import { Island } from './Island';
import { RenderingTargetContext } from './RenderingTarget';
import { RenderingContext } from './RenderingContext';

type Props = {
byline: string;
Expand Down Expand Up @@ -132,13 +132,12 @@ export const BylineLink = ({ byline, tags, format }: Props) => {
);
});

const renderingTargetContext = useContext(RenderingTargetContext);
const { target } = useContext(RenderingContext);

return (
<>
{renderedTokens}
{renderingTargetContext === 'Apps' &&
soleContributor !== undefined ? (
{target === 'Apps' && soleContributor !== undefined ? (
<Island>
<FollowWrapper
displayName={soleContributor.title}
Expand Down
15 changes: 15 additions & 0 deletions dotcom-rendering/src/components/RenderingContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createContext } from 'react';
import type { RenderingContextType } from '../types/renderingContext';

/** Represents the initial or default value for the app context */
const defaultContext: RenderingContextType = {
target: 'Web',
};

/**
* Context for global, static values (generic)
*
* This should not contain anything which will change between re-renders
*/
export const RenderingContext =
createContext<RenderingContextType>(defaultContext);
4 changes: 0 additions & 4 deletions dotcom-rendering/src/components/RenderingTarget.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions dotcom-rendering/src/lib/ArticleRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ArticleFormat } from '@guardian/libs';
import { ArticleDesign } from '@guardian/libs';
import { useContext } from 'react';
import { adContainerStyles } from '../components/AdSlot';
import { RenderingTargetContext } from '../components/RenderingTarget';
import { RenderingContext } from '../components/RenderingContext';
import { interactiveLegacyClasses } from '../layouts/lib/interactiveLegacyStyling';
import type { ServerSideTests, Switches } from '../types/config';
import type { FEElement } from '../types/content';
Expand Down Expand Up @@ -85,7 +85,7 @@ export const ArticleRenderer = ({
);
});

const renderingTargetContext = useContext(RenderingTargetContext);
const { target } = useContext(RenderingContext);

// const cleanedElements = elements.map(element =>
// 'html' in element ? { ...element, html: clean(element.html) } : element,
Expand All @@ -106,7 +106,7 @@ export const ArticleRenderer = ({
].join(' ')}
css={[adStylesDynamic, commercialPosition]}
>
{renderingTargetContext === 'Apps'
{target === 'Apps'
? renderedElements
: /* Insert the placeholder for the sign in gate on the 2nd article element */
withSignInGateSlot({
Expand Down
9 changes: 6 additions & 3 deletions dotcom-rendering/src/server/render.article.apps.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { isString } from '@guardian/libs';
import { ArticlePage } from '../components/ArticlePage';
import { RenderingTargetContext } from '../components/RenderingTarget';
import { RenderingContext } from '../components/RenderingContext';
import { generateScriptTags, getPathFromManifest } from '../lib/assets';
import { decideFormat } from '../lib/decideFormat';
import { renderToStringWithEmotion } from '../lib/emotion';
import { createGuardian } from '../model/guardian';
import type { FEArticleType } from '../types/frontend';
import type { RenderingContextType } from '../types/renderingContext';
import { htmlPageTemplate } from './htmlPageTemplate';

export const renderArticle = (
Expand All @@ -16,14 +17,16 @@ export const renderArticle = (
} => {
const format: ArticleFormat = decideFormat(article.format);

const renderingContext: RenderingContextType = { target: 'Apps' };

const { html, extractedCss } = renderToStringWithEmotion(
<RenderingTargetContext.Provider value="Apps">
<RenderingContext.Provider value={renderingContext}>
<ArticlePage
format={format}
article={article}
renderingTarget="Apps"
/>
</RenderingTargetContext.Provider>,
</RenderingContext.Provider>,
);

const clientScripts = [getPathFromManifest('apps', 'index.js')];
Expand Down
9 changes: 6 additions & 3 deletions dotcom-rendering/src/server/render.article.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ArticleDesign, isString, Pillar } from '@guardian/libs';
import { ArticlePage } from '../components/ArticlePage';
import { isAmpSupported } from '../components/Elements.amp';
import { KeyEventsContainer } from '../components/KeyEventsContainer';
import { RenderingTargetContext } from '../components/RenderingTarget';
import { RenderingContext } from '../components/RenderingContext';
import {
ASSET_ORIGIN,
generateScriptTags,
Expand All @@ -21,6 +21,7 @@ import { extractNAV } from '../model/extract-nav';
import { createGuardian as createWindowGuardian } from '../model/guardian';
import type { FEElement } from '../types/content';
import type { FEArticleType, FEBlocksRequest } from '../types/frontend';
import type { RenderingContextType } from '../types/renderingContext';
import type { TagType } from '../types/tag';
import { htmlPageTemplate } from './htmlPageTemplate';

Expand Down Expand Up @@ -48,15 +49,17 @@ export const renderHtml = ({

const format: ArticleFormat = decideFormat(article.format);

const renderingContext: RenderingContextType = { target: 'Web' };

const { html, extractedCss } = renderToStringWithEmotion(
<RenderingTargetContext.Provider value="Web">
<RenderingContext.Provider value={renderingContext}>
<ArticlePage
format={format}
article={article}
NAV={NAV}
renderingTarget="Web"
/>
</RenderingTargetContext.Provider>,
</RenderingContext.Provider>,
);

// We want to only insert script tags for the elements or main media elements on this page view
Expand Down
10 changes: 10 additions & 0 deletions dotcom-rendering/src/types/renderingContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { RenderingTarget } from './renderingTarget';

/**
* Context for global values (generic)
*
* This should not contain any properties which are likely to change between re-renders
*/
export interface RenderingContextType {
target: RenderingTarget;
}

0 comments on commit 3f494e9

Please sign in to comment.