feat route context

This commit is contained in:
ozakione 2024-05-16 13:07:28 +02:00
parent 27a57d04e4
commit dee675c82b
6 changed files with 23 additions and 66 deletions

View file

@ -136,9 +136,7 @@ export function sortItems(params: ShowcaseItem[]): ShowcaseItem[] {
function useShowcase() { function useShowcase() {
const routeContext = useRouteContext(); const routeContext = useRouteContext();
console.log('routeContext:', routeContext);
const showcase = routeContext?.data?.showcase; const showcase = routeContext?.data?.showcase;
console.log('showcase:', showcase);
if (!showcase) { if (!showcase) {
throw new Error( throw new Error(
'showcase-related hooks can only be called on the showcase page', 'showcase-related hooks can only be called on the showcase page',

View file

@ -81,7 +81,7 @@ export default async function pluginContentShowcase(
}); });
}, },
async contentLoaded({content, actions: {addRoute}}) { async contentLoaded({content, actions: {addRoute, createData}}) {
if (!content) { if (!content) {
return; return;
} }
@ -92,6 +92,7 @@ export default async function pluginContentShowcase(
screenshotApi, screenshotApi,
routeBasePath, routeBasePath,
addRoute, addRoute,
createData,
}); });
}, },
}; };

View file

@ -17,20 +17,24 @@ export async function processContentLoaded({
routeBasePath, routeBasePath,
screenshotApi, screenshotApi,
addRoute, addRoute,
createData,
}: { }: {
content: ShowcaseItems; content: ShowcaseItems;
routeBasePath: string; routeBasePath: string;
tags: TagsOption; tags: TagsOption;
screenshotApi: string; screenshotApi: string;
addRoute: PluginContentLoadedActions['addRoute']; addRoute: PluginContentLoadedActions['addRoute'];
createData: PluginContentLoadedActions['createData'];
}): Promise<void> { }): Promise<void> {
addRoute({ addRoute({
path: routeBasePath, path: routeBasePath,
component: '@theme/Showcase', component: '@theme/Showcase',
props: { context: {
items: content.items, showcase: await createData('showcase.json', {
tags, items: content.items,
screenshotApi, tags,
screenshotApi,
}),
}, },
exact: true, exact: true,
}); });

View file

@ -7,13 +7,11 @@
import Translate, {translate} from '@docusaurus/Translate'; import Translate, {translate} from '@docusaurus/Translate';
import Link from '@docusaurus/Link'; import Link from '@docusaurus/Link';
import {ShowcaseProvider} from '@docusaurus/theme-common/internal';
import Layout from '@theme/Layout'; import Layout from '@theme/Layout';
import Heading from '@theme/Heading'; import Heading from '@theme/Heading';
import ShowcaseSearchBar from '@theme/Showcase/ShowcaseSearchBar'; import ShowcaseSearchBar from '@theme/Showcase/ShowcaseSearchBar';
import ShowcaseCards from '@theme/Showcase/ShowcaseCards'; import ShowcaseCards from '@theme/Showcase/ShowcaseCards';
import ShowcaseFilters from '@theme/Showcase/ShowcaseFilters'; import ShowcaseFilters from '@theme/Showcase/ShowcaseFilters';
import type {Props} from '@theme/Showcase';
const TITLE = translate({message: 'Docusaurus Site Showcase'}); const TITLE = translate({message: 'Docusaurus Site Showcase'});
const DESCRIPTION = translate({ const DESCRIPTION = translate({
@ -35,24 +33,19 @@ function ShowcaseHeader() {
); );
} }
export default function Showcase(props: Props): JSX.Element { export default function Showcase(): JSX.Element {
return ( return (
<ShowcaseProvider <Layout title={TITLE} description={DESCRIPTION}>
items={props.items} <main className="margin-vert--lg">
tags={props.tags} <ShowcaseHeader />
screenshotApi={props.screenshotApi}> <ShowcaseFilters />
<Layout title={TITLE} description={DESCRIPTION}> <div
<main className="margin-vert--lg"> style={{display: 'flex', marginLeft: 'auto'}}
<ShowcaseHeader /> className="container">
<ShowcaseFilters /> <ShowcaseSearchBar />
<div </div>
style={{display: 'flex', marginLeft: 'auto'}} <ShowcaseCards />
className="container"> </main>
<ShowcaseSearchBar /> </Layout>
</div>
<ShowcaseCards />
</main>
</Layout>
</ShowcaseProvider>
); );
} }

View file

@ -1,38 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, {createContext, useMemo, type ReactNode} from 'react';
import type {
ShowcaseItem,
TagsOption,
ShowcaseContextType,
} from '@docusaurus/plugin-content-showcase';
const ShowcaseContext = createContext<ShowcaseContextType | null>(null);
export function ShowcaseProvider({
items,
tags,
screenshotApi,
children,
}: {
items: ShowcaseItem[];
tags: TagsOption;
screenshotApi: string;
children: ReactNode;
}): JSX.Element {
const contextValue = useMemo(
() => ({items, tags, screenshotApi}),
[items, tags, screenshotApi],
);
return (
<ShowcaseContext.Provider value={contextValue}>
{children}
</ShowcaseContext.Provider>
);
}

View file

@ -26,7 +26,6 @@ export {DocsVersionProvider, useDocsVersion} from './contexts/docsVersion';
export {DocsSidebarProvider, useDocsSidebar} from './contexts/docsSidebar'; export {DocsSidebarProvider, useDocsSidebar} from './contexts/docsSidebar';
export {DocProvider, useDoc, type DocContextValue} from './contexts/doc'; export {DocProvider, useDoc, type DocContextValue} from './contexts/doc';
export {ShowcaseProvider} from './contexts/showcase';
export { export {
BlogPostProvider, BlogPostProvider,
useBlogPost, useBlogPost,