Skip to content

Commit

Permalink
Consolidate loaders into content config to fix bug with types
Browse files Browse the repository at this point in the history
Allows me to remove the type casts
  • Loading branch information
tylermercer committed Sep 24, 2024
1 parent 9094bf4 commit 1f77254
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 78 deletions.
35 changes: 32 additions & 3 deletions src/content/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { z, defineCollection } from "astro:content";
import episodesLoader from "src/loaders/episodes";
import { z, defineCollection, reference } from "astro:content";

if (!import.meta.env.GOOGLE_SHEET_APP_URL.startsWith('https')) {
throw new Error("GOOGLE_SHEET_APP_URL not set at build time")
Expand All @@ -25,7 +24,37 @@ const seasons = defineCollection({

const episodes = defineCollection({
type: 'content_layer',
loader: episodesLoader,
loader: async () => {
const references = await fetch(`${import.meta.env.GOOGLE_SHEET_APP_URL}?s=References`)
.then(r => r.json())
.then(r => r.filter((e: any) => e.episode && e.season));

const episodes = await fetch(`${import.meta.env.GOOGLE_SHEET_APP_URL}?s=Episodes`)
.then(r => r.json())
.then(r => r
.filter((e: any) => e.season && e.episode)
.sort((e: any) => e.season + e.episode)
);

return episodes.map((e: any) => ({
id: `${e.season}:${e.episode}`,
...e,
references: references.filter((r: any) => r.season === e.season && r.episode === e.episode)
}));
},
schema: z.object({
title: z.string(),
description: z.string(),
season: reference('seasons'),
episode: z.string(),
watchUrl: z.string().url(),
references: z.array(z.object({
timeRange: z.string().optional(),
notes: z.string(),
passage: z.string(),
type: z.string(),
}))
}),
});

export const collections = {
Expand Down
44 changes: 0 additions & 44 deletions src/loaders/episodes.ts

This file was deleted.

20 changes: 6 additions & 14 deletions src/pages/[season]/[episode].astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export const getStaticPaths = (async () => {
params: {
episode: entryWithNextAndPrev.item.data.episode,
//cast is necessary because of a bug in Astro
season: (entryWithNextAndPrev.item.data.season as { id: string })
.id,
season: entryWithNextAndPrev.item.data.season.id,
},
props: { entryWithNextAndPrev },
}));
Expand All @@ -37,14 +36,12 @@ const { item: entry, next, prev } = entryWithNextAndPrev;
const prevData = prev && {
label: prev.data.title,
//cast is necessary because of a bug in Astro
url: `/${(prev.data.season as { id: string }).id}/${prev.data.episode}`,
url: `/${prev.data.season.id}/${prev.data.episode}`,
};
const nextData = next && {
label: next.data.title,
//cast is necessary because of a bug in Astro
url: `/${(next.data.season as { id: string }).id}/${next.data.episode}`,
url: `/${next.data.season.id}/${next.data.episode}`,
};
type Reference = CollectionEntry<"episodes">["data"]["references"][number];
Expand All @@ -56,14 +53,9 @@ type Reference = CollectionEntry<"episodes">["data"]["references"][number];
<nav aria-label="Breadcrumbs" class="breadcrumbs">
<ol>
<li>
<!-- cast is necessary because of a bug in Astro -->
<a href={`/${(entry.data.season as { id: string }).id}`}
>{
(
entry.data.season as { id: string }
).id.toUpperCase()
}</a
>
<a href={`/${entry.data.season.id}`}>
{entry.data.season.id.toUpperCase()}
</a>
<span aria-hidden="true">:</span>
</li>
<li>
Expand Down
3 changes: 1 addition & 2 deletions src/pages/[season]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const nextData = next && {
const episodes = await getCollection(
"episodes",
//cast is necessary because of a bug in Astro
(e: CollectionEntry<"episodes">) => (e.data.season as {id: string}).id === entry.id,
(e: CollectionEntry<"episodes">) => e.data.season.id === entry.id,
);
---

Expand Down
34 changes: 19 additions & 15 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ const {
const seasons = await getCollection("seasons");
const references = new Map(await Promise.all(
seasons.map(
async (s: CollectionEntry<"seasons">) => [
s.id,
(
await getCollection(
"episodes",
(e: CollectionEntry<"episodes">) =>
//cast is necessary because of a bug in Astro
(e.data.season as { id: string}).id === s.id,
)
).flatMap((e) => e.data.references),
] as const,
const references = new Map(
await Promise.all(
seasons.map(
async (s: CollectionEntry<"seasons">) =>
[
s.id,
(
await getCollection(
"episodes",
(e: CollectionEntry<"episodes">) =>
e.data.season.id === s.id,
)
).flatMap((e) => e.data.references),
] as const,
),
),
));
);
type Season = CollectionEntry<"seasons">;
---
Expand All @@ -48,7 +50,9 @@ type Season = CollectionEntry<"seasons">;
<a href={`/${s.id}`}>{s.data.title}</a>
</h3>
<p>
<ReferencesSummary references={references.get(s.id)!} />
<ReferencesSummary
references={references.get(s.id)!}
/>
</p>
</li>
))
Expand Down

0 comments on commit 1f77254

Please sign in to comment.