From c8b7e6d5dffd6d75fe4ab9a6a9e70c3110c7ee37 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Sat, 20 Apr 2024 19:25:11 +0200 Subject: [PATCH] screenshot api --- .../src/index.ts | 11 +++- .../src/lifecycle/contentLoaded.ts | 3 + .../src/theme-classic.d.ts | 1 + .../src/theme/Showcase/ShowcaseCard/index.tsx | 19 +++---- .../src/theme/Showcase/index.tsx | 35 ++++++------ .../src/contexts/showcase.tsx | 55 ++++++++++++++++++- .../docusaurus-theme-common/src/internal.ts | 4 +- website/showcase/tags.yml | 12 ++++ 8 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 website/showcase/tags.yml diff --git a/packages/docusaurus-plugin-content-showcase/src/index.ts b/packages/docusaurus-plugin-content-showcase/src/index.ts index a5e4603db3..27a48522ce 100644 --- a/packages/docusaurus-plugin-content-showcase/src/index.ts +++ b/packages/docusaurus-plugin-content-showcase/src/index.ts @@ -31,7 +31,15 @@ export default async function pluginContentShowcase( ): Promise> { const {siteDir, localizationDir} = context; // todo check for better naming of path: sitePath - const {include, exclude, tags, routeBasePath, path: sitePath, id} = options; + const { + include, + exclude, + tags, + routeBasePath, + path: sitePath, + id, + screenshotApi, + } = options; const contentPaths: ShowcaseContentPaths = { contentPath: path.resolve(siteDir, sitePath), @@ -81,6 +89,7 @@ export default async function pluginContentShowcase( await processContentLoaded({ content, tags: validatedTags, + screenshotApi, routeBasePath, addRoute, }); diff --git a/packages/docusaurus-plugin-content-showcase/src/lifecycle/contentLoaded.ts b/packages/docusaurus-plugin-content-showcase/src/lifecycle/contentLoaded.ts index b44877242b..c6be241a79 100644 --- a/packages/docusaurus-plugin-content-showcase/src/lifecycle/contentLoaded.ts +++ b/packages/docusaurus-plugin-content-showcase/src/lifecycle/contentLoaded.ts @@ -15,11 +15,13 @@ export async function processContentLoaded({ content, tags, routeBasePath, + screenshotApi, addRoute, }: { content: ShowcaseItems; routeBasePath: string; tags: TagsOption; + screenshotApi: string; addRoute: PluginContentLoadedActions['addRoute']; }): Promise { addRoute({ @@ -28,6 +30,7 @@ export async function processContentLoaded({ props: { items: content.items, tags, + screenshotApi, }, exact: true, }); diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index e1dee49472..c8dc4605f1 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -256,6 +256,7 @@ declare module '@theme/Showcase' { export type Props = { items: ShowcaseItem[]; tags: TagsOption; + screenshotApi: string; }; export default function Showcase(props: Props): JSX.Element; diff --git a/packages/docusaurus-theme-classic/src/theme/Showcase/ShowcaseCard/index.tsx b/packages/docusaurus-theme-classic/src/theme/Showcase/ShowcaseCard/index.tsx index d47da0859c..c8d04aec2e 100644 --- a/packages/docusaurus-theme-classic/src/theme/Showcase/ShowcaseCard/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Showcase/ShowcaseCard/index.tsx @@ -10,7 +10,10 @@ import clsx from 'clsx'; import Link from '@docusaurus/Link'; import Translate from '@docusaurus/Translate'; import {sortBy} from '@docusaurus/plugin-content-showcase/client'; -import {useShowcaseTags} from '@docusaurus/theme-common/internal'; +import { + useShowcaseTags, + useShowcaseApiScreenshot, +} from '@docusaurus/theme-common/internal'; import Heading from '@theme/Heading'; import FavoriteIcon from '@theme/Showcase/FavoriteIcon'; import type {ShowcaseItem, TagType} from '@docusaurus/plugin-content-showcase'; @@ -36,6 +39,7 @@ function TagItem({ ); } +// TODO move tag reorder logic into hook function ShowcaseCardTag({tags}: {tags: TagType[]}) { const Tags = useShowcaseTags(); const TagList = Object.keys(Tags) as TagType[]; @@ -56,18 +60,13 @@ function ShowcaseCardTag({tags}: {tags: TagType[]}) { ); } -function getCardImage(item: ShowcaseItem): string { - return ( - item.preview ?? - // TODO make it configurable - `https://slorber-api-screenshot.netlify.app/${encodeURIComponent( - item.website, - )}/showcase` - ); +function getCardImage(item: ShowcaseItem, api: string): string { + return item.preview ?? `${api}/${encodeURIComponent(item.website)}/showcase`; } function ShowcaseCard({item}: {item: ShowcaseItem}) { - const image = getCardImage(item); + const api = useShowcaseApiScreenshot(); + const image = getCardImage(item, api); return (
  • diff --git a/packages/docusaurus-theme-classic/src/theme/Showcase/index.tsx b/packages/docusaurus-theme-classic/src/theme/Showcase/index.tsx index 0847018ef1..bd920d4ca5 100644 --- a/packages/docusaurus-theme-classic/src/theme/Showcase/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Showcase/index.tsx @@ -7,7 +7,7 @@ import Translate, {translate} from '@docusaurus/Translate'; import Link from '@docusaurus/Link'; -import {TagsProvider, ItemsProvider} from '@docusaurus/theme-common/internal'; +import {ShowcaseProvider} from '@docusaurus/theme-common/internal'; import Layout from '@theme/Layout'; import Heading from '@theme/Heading'; import ShowcaseSearchBar from '@theme/Showcase/ShowcaseSearchBar'; @@ -37,21 +37,22 @@ function ShowcaseHeader() { export default function Showcase(props: Props): JSX.Element { return ( - - - -
    - - -
    - -
    - -
    -
    -
    -
    + + +
    + + +
    + +
    + +
    +
    +
    ); } diff --git a/packages/docusaurus-theme-common/src/contexts/showcase.tsx b/packages/docusaurus-theme-common/src/contexts/showcase.tsx index c8b1ebc667..617f5fae8e 100644 --- a/packages/docusaurus-theme-common/src/contexts/showcase.tsx +++ b/packages/docusaurus-theme-common/src/contexts/showcase.tsx @@ -12,18 +12,31 @@ import type { TagsOption, } from '@docusaurus/plugin-content-showcase'; +// duplicated from theme classic showcase +type Props = { + items: ShowcaseItem[]; + tags: TagsOption; + screenshotApi: string; + children: ReactNode; +}; + const ItemsContext = React.createContext(null); +const ApiContext = React.createContext(null); const TagsContext = React.createContext(null); function useItemsContextValue(content: ShowcaseItem[]): ShowcaseItem[] { return useMemo(() => content, [content]); } +function useApiScreenshotContextValue(content: string): string { + return useMemo(() => content, [content]); +} + function useTagsContextValue(tags: TagsOption): TagsOption { return useMemo(() => tags, [tags]); } -export function ItemsProvider({ +function ItemsProvider({ children, items, }: { @@ -38,7 +51,20 @@ export function ItemsProvider({ ); } -export function TagsProvider({ +function ApiScreenshotProvider({ + children, + api, +}: { + children: ReactNode; + api: string; +}): JSX.Element { + const contextValue = useApiScreenshotContextValue(api); + return ( + {children} + ); +} + +function TagsProvider({ children, tags, }: { @@ -51,6 +77,23 @@ export function TagsProvider({ ); } +export function ShowcaseProvider({ + items, + tags, + screenshotApi, + children, +}: Props): JSX.Element { + return ( + + + + {children} + + + + ); +} + export function useShowcaseItems(): ShowcaseItem[] { const showcaseItems = useContext(ItemsContext); if (showcaseItems === null) { @@ -59,6 +102,14 @@ export function useShowcaseItems(): ShowcaseItem[] { return showcaseItems; } +export function useShowcaseApiScreenshot(): string { + const showcaseItems = useContext(ApiContext); + if (showcaseItems === null) { + throw new ReactContextError('ItemsProvider'); + } + return showcaseItems; +} + export function useShowcaseTags(): TagsOption { const tags = useContext(TagsContext); if (tags === null) { diff --git a/packages/docusaurus-theme-common/src/internal.ts b/packages/docusaurus-theme-common/src/internal.ts index 56c3b0d4d6..8602b04e04 100644 --- a/packages/docusaurus-theme-common/src/internal.ts +++ b/packages/docusaurus-theme-common/src/internal.ts @@ -27,10 +27,10 @@ export {DocsSidebarProvider, useDocsSidebar} from './contexts/docsSidebar'; export {DocProvider, useDoc, type DocContextValue} from './contexts/doc'; export { - ItemsProvider, - TagsProvider, useShowcaseItems, useShowcaseTags, + useShowcaseApiScreenshot, + ShowcaseProvider, } from './contexts/showcase'; export { BlogPostProvider, diff --git a/website/showcase/tags.yml b/website/showcase/tags.yml new file mode 100644 index 0000000000..8c94a0e1d0 --- /dev/null +++ b/website/showcase/tags.yml @@ -0,0 +1,12 @@ +favorite: + label: 'Favorite' + description: + message: 'Our favorite Docusaurus sites that you must absolutely check out!' + id: 'showcase.tag.favorite.description' + color: '#e9669e' +opensource: + label: 'Open Source' + description: + message: 'These sites are open source, so you can learn from them!' + id: 'showcase.tag.opensource.description' + color: '#f6993f'