wip routeContext

This commit is contained in:
ozakione 2024-05-16 09:41:34 +02:00
parent 7e417aa77d
commit 8ad074773a
6 changed files with 63 additions and 98 deletions

View file

@ -13,10 +13,12 @@ import {
useQueryStringList,
type ListUpdateFunction,
} from '@docusaurus/theme-common';
import useRouteContext from '@docusaurus/useRouteContext';
import type {
TagType,
Operator,
ShowcaseItem,
TagsOption,
} from '@docusaurus/plugin-content-showcase';
export function filterItems({
@ -130,3 +132,34 @@ export function sortItems(params: ShowcaseItem[]): ShowcaseItem[] {
result = sortBy(result, (user) => !user.tags.includes('favorite'));
return result;
}
export interface ShowcaseContextType {
items: ShowcaseItem[];
tags: TagsOption;
screenshotApi: string;
}
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',
);
}
return showcase as ShowcaseContextType;
}
export function useShowcaseItems(): ShowcaseItem[] {
return useShowcase().items;
}
export function useShowcaseApiScreenshot(): string {
return useShowcase().screenshotApi;
}
export function useShowcaseTags(): TagsOption {
return useShowcase().tags;
}