Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Aug 5, 2024
1 parent 91b04fd commit dc98328
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 116 deletions.
119 changes: 28 additions & 91 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"jest-environment-jsdom": "^29.0.3",
"lint-staged": "^11.1.2",
"prettier": "3.2.5",
"turbo": "^2.0.3"
"turbo": "^2.0.11"
},
"nextBundleAnalysis": {
"buildOutputDirectory": "./projects/wp-nextjs/.next",
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/rsc/actions/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use server';

import { cookies, draftMode, headers } from 'next/headers';
import { redirect } from 'next/navigation';
import { COOKIE_NAME } from '../handlers/previewRouteHandler';

export async function disableDraftMode() {
const currentUrl = headers().get('x-headstartwp-current-url') ?? '/';
draftMode().disable();
cookies().delete(COOKIE_NAME);
redirect(currentUrl);
}
2 changes: 1 addition & 1 deletion packages/next/src/rsc/blocks/LinkBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface LinkBlockProps extends BlockProps<IDataWPBlock> {
* import { BlocksRenderer } from "@headstartwp/core/react";
* import { LinkBlock } from "@headstartwp/next/app";
*
* <BlocksRenderer html={html}>
* <BlocksRenderer html={html settings={settings} }>
* <LinkBlock />
* </BlocksRenderer>
* ```
Expand Down
25 changes: 25 additions & 0 deletions packages/next/src/rsc/blocks/__tests__/LinkBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { render } from '@testing-library/react';
import React from 'react';
import { BlocksRenderer } from '@headstartwp/core/react';
import { getHeadstartWPConfig, setHeadstartWPConfig } from '@headstartwp/core';
import { LinkBlock } from '../LinkBlock';

describe('LinkBlock', () => {
it('shound replace internal links with next/link', () => {
setHeadstartWPConfig({
sourceUrl: 'http://wpadmin.com',
hostUrl: 'http://domain.com',
});

const { container } = render(
<BlocksRenderer
html="<div class='content'><a href='http://wpadmin.com/post-name'>This is an internal link</a>></div>"
settings={getHeadstartWPConfig()}
>
<LinkBlock />
</BlocksRenderer>,
);

expect(container).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`LinkBlock shound replace internal links with next/link 1`] = `
<div>
<div
class="content"
>
<a
class=""
href="/post-name"
>
This is an internal link
</a>
&gt;
</div>
</div>
`;
33 changes: 33 additions & 0 deletions packages/next/src/rsc/components/HeadstartWPApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
SettingsContextProps,
SettingsProvider,
ThemeSettingsProvider,
} from '@headstartwp/core/react';
import React, { FC, ReactNode } from 'react';
import { AppEntity } from '@headstartwp/core';

type HeadstartWPAppProps = {
/**
* Supported settings by the framework. Such as custom image component, custom link component etc.
*
* @see {@link SettingsContextProps}
*/
settings: SettingsContextProps;

/**
* Theme settings from the `theme.json`.
*
* Passing this will expose theme json through `useThemeSettings` hook.
*/
themeJSON?: AppEntity['theme.json'];

children?: ReactNode;
};

export const HeadstartWPApp: FC<HeadstartWPAppProps> = ({ settings, children, themeJSON = {} }) => {
return (
<SettingsProvider settings={settings}>
<ThemeSettingsProvider data={themeJSON}>{children}</ThemeSettingsProvider>
</SettingsProvider>
);
};
15 changes: 2 additions & 13 deletions packages/next/src/rsc/components/PreviewIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { cookies, draftMode, headers } from 'next/headers';
import { draftMode } from 'next/headers';
import React from 'react';
import { redirect } from 'next/navigation';

import { COOKIE_NAME } from '../handlers/previewRouteHandler';
import { disableDraftMode } from '../actions/preview';

export type PreviewIndicatorProps = {
className?: string;
Expand All @@ -15,15 +13,6 @@ export const PreviewIndicator: React.FC<PreviewIndicatorProps> = ({ className })
return null;
}

async function disableDraftMode() {
'use server';

const currentUrl = headers().get('x-headstartwp-current-url') ?? '/';
draftMode().disable();
cookies().delete(COOKIE_NAME);
redirect(currentUrl);
}

return (
<div className={className}>
<form action={disableDraftMode}>
Expand Down
21 changes: 21 additions & 0 deletions packages/next/src/rsc/components/__tests__/Link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { render } from '@testing-library/react';
import React from 'react';
import { SettingsProvider } from '@headstartwp/core/react';
import { Link } from '../Link';

describe('Link', () => {
it('shound replace internal links with next/link', () => {
const { container } = render(
<SettingsProvider
settings={{
sourceUrl: 'http://wpadmin.com',
hostUrl: 'http://domain.com',
}}
>
<Link href="http://wpadmin.com/post-name" />
</SettingsProvider>,
);

expect(container).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Link shound replace internal links with next/link 1`] = `
<div>
<a
href="/post-name"
/>
</div>
`;
1 change: 1 addition & 0 deletions packages/next/src/rsc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './handlers/revalidateRouterHandler';
export * from './components/PreviewIndicator';
export * from './components/JSONLD';
export * from './components/Link';
export * from './components/HeadstartWPApp';

// blocks
export * from './blocks/LinkBlock';
Expand Down
23 changes: 13 additions & 10 deletions projects/wp-nextjs-app/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inter } from 'next/font/google';
import './globals.css';
import { Link, PreviewIndicator, queryAppSettings } from '@headstartwp/next/app';
import { Menu, SettingsProvider, ThemeSettingsProvider } from '@headstartwp/core/react';
import { Link, PreviewIndicator, queryAppSettings, HeadstartWPApp } from '@headstartwp/next/app';
import { Menu, SettingsContextProps } from '@headstartwp/core/react';

const inter = Inter({ subsets: ['latin'] });

Expand All @@ -12,17 +12,20 @@ const RootLayout = async ({
}>) => {
const { menu, data, config } = await queryAppSettings({ menu: 'primary' });

const settings: SettingsContextProps = {
...config,
// @ts-expect-error
linkComponent: Link,
};

return (
<html lang="en">
<body className={inter.className}>
<ThemeSettingsProvider data={data['theme.json']}>
<SettingsProvider settings={config}>
{/* @ts-expect-error */}
{menu ? <Menu items={menu} linkWrapper={Link} /> : null}
{children}
<PreviewIndicator className="form-container" />
</SettingsProvider>
</ThemeSettingsProvider>
<HeadstartWPApp settings={settings} themeJSON={data['theme.json']}>
{menu ? <Menu items={menu} /> : null}
{children}
<PreviewIndicator className="form-container" />
</HeadstartWPApp>
</body>
</html>
);
Expand Down
Loading

0 comments on commit dc98328

Please sign in to comment.