Skip to content

Commit 5996ea2

Browse files
committed
reduce code duplication
1 parent d993008 commit 5996ea2

4 files changed

Lines changed: 21 additions & 34 deletions

File tree

apps/showcase/app/layouts/sidebar.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const props = defineProps<{
66
* Whether to hide the page hero.
77
*/
88
hideHero?: boolean;
9+
collectionOptions?: UseCollectionOptions;
910
}>();
1011
1112
defineSlots<{
@@ -15,11 +16,11 @@ defineSlots<{
1516
default(): unknown;
1617
}>();
1718
18-
const { data } = await useCollection();
19+
const { data } = await useCollection(computed(() => props.collectionOptions));
1920
</script>
2021

2122
<template>
22-
<SidebarLayout>
23+
<SidebarLayout :collection-options="props.collectionOptions">
2324
<template v-if="!props.hideHero" #hero>
2425
<PageContentHero
2526
:headline="data?.title"

apps/showcase/app/pages/components/[category]/[name].vue

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,17 @@ import SidebarLayout from "~/layouts/sidebar.vue";
44
55
definePageMeta({ layout: false });
66
7-
const { locale } = useI18n();
87
const route = useRoute();
9-
108
const slug = computed(() => `/components/${route.params.category}/${route.params.name}`);
119
12-
const collection = await useAsyncData(
13-
() => `page-${slug.value}-${locale.value}`,
14-
async () => {
15-
const collection = `content_${locale.value}` as const;
16-
return queryCollection(collection).path(slug.value).first();
17-
},
18-
);
19-
20-
watch(
21-
() => collection.data.value,
22-
(data) => {
23-
// if data is "null", the page content was not found. "undefined" means it is not loaded yet
24-
if (data !== null) return;
25-
throw showError({
26-
message: "Page not found",
27-
statusCode: 404,
28-
});
29-
},
30-
{ immediate: true },
31-
);
32-
33-
const data = computed(() => collection.data.value);
34-
35-
useSeoMeta({
36-
title: computed(() => collection.data.value?.seo.title),
37-
description: computed(() => collection.data.value?.seo.description),
38-
});
10+
const collectionOptions: UseCollectionOptions = { slug };
11+
const { data } = await useCollection(collectionOptions);
3912
4013
const activeTab = useRouteQuery("tab", "overview");
4114
</script>
4215

4316
<template>
44-
<SidebarLayout hide-hero>
17+
<SidebarLayout hide-hero :collection-options>
4518
<ProseH1>{{ data?.title }}</ProseH1>
4619

4720
<OnyxTabs v-model="activeTab" :label="$t('components.details')">

packages/nuxt-docs/app/composables/useCollection.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import type { Collections } from "@nuxt/content";
22

3+
export type UseCollectionOptions = {
4+
/**
5+
* Slug / route path to use for querying the collection.
6+
*
7+
* @default `slug` route parameter
8+
*/
9+
slug?: MaybeRef<string>;
10+
};
11+
312
/**
413
* Composable for loading the collection content for the current route and locale.
514
*/
6-
export const useCollection = () => {
15+
export const useCollection = (options?: MaybeRef<UseCollectionOptions | undefined>) => {
716
const route = useRoute();
817
const { locale } = useI18n();
918

1019
const slug = computed(() => {
20+
const _slug = toValue(toValue(options)?.slug);
21+
if (_slug) return _slug;
22+
1123
const path = Array.isArray(route.params.slug)
1224
? route.params.slug.join("/")
1325
: (route.params.slug ?? "");

packages/nuxt-docs/app/layouts/sidebar.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { SidebarNavigationItem } from "../composables/useSidebarNavigation.
66
const props = defineProps<
77
Omit<OnyxPageLayoutProps, "noPadding"> & {
88
sidebar?: OnyxSidebarProps;
9+
collectionOptions?: UseCollectionOptions;
910
}
1011
>();
1112
@@ -43,7 +44,7 @@ const slots = defineSlots<{
4344
4445
const { navigation, previousRootItem } = await useSidebarNavigation();
4546
46-
const collection = await useCollection();
47+
const collection = await useCollection(computed(() => props.collectionOptions));
4748
const toc = computed(() => collection.data.value?.body.toc?.links ?? []);
4849
</script>
4950

0 commit comments

Comments
 (0)