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() {
const routeContext = useRouteContext();
console.log('routeContext:', routeContext);
const showcase = routeContext?.data?.showcase;
console.log('showcase:', showcase);
if (!showcase) {
throw new Error(
'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) {
return;
}
@ -92,6 +92,7 @@ export default async function pluginContentShowcase(
screenshotApi,
routeBasePath,
addRoute,
createData,
});
},
};

View file

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

View file

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

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 {DocProvider, useDoc, type DocContextValue} from './contexts/doc';
export {ShowcaseProvider} from './contexts/showcase';
export {
BlogPostProvider,
useBlogPost,