From 698828699ef5e8f87e2df6480d2358b7c6a2c69a Mon Sep 17 00:00:00 2001 From: Marin <51886896+mar1@users.noreply.github.com> Date: Sat, 28 Dec 2024 14:51:52 +0100 Subject: [PATCH] Fixing rendering for null values --- src/components/CharacterDetail.astro | 164 ++++++++++++++++----------- src/pages/characters/[id].astro | 4 + src/pages/index.astro | 2 +- src/utils/characters.ts | 24 +++- 4 files changed, 119 insertions(+), 75 deletions(-) diff --git a/src/components/CharacterDetail.astro b/src/components/CharacterDetail.astro index 28d189e..2fcced8 100644 --- a/src/components/CharacterDetail.astro +++ b/src/components/CharacterDetail.astro @@ -1,5 +1,5 @@ --- -import type { Character } from '../types/character'; +import type { Character } from "../types/character"; interface Props { character: Character; @@ -12,82 +12,110 @@ const { character } = Astro.props;

{character.name}

-
-

Bio

- -
+ { + character.bio && character.bio.length > 0 && ( +
+

Bio

+ +
+ ) + } -
-

Lore

- -
+ { + character.lore && character.lore.length > 0 && ( +
+

Lore

+ +
+ ) + } -
-

Knowledge

-
- {character.knowledge.map((item) => ( - - {item} - - ))} -
-
+ { + character.knowledge && character.knowledge.length > 0 && ( +
+

Knowledge

+
+ {character.knowledge.map((item) => ( + + {item} + + ))} +
+
+ ) + } -
-

Style

-
-
-

General

-
    - {character.style.all.map((style) => ( -
  • {style}
  • - ))} -
-
-
-

Chat

-
    - {character.style.chat.map((style) => ( -
  • {style}
  • - ))} -
-
-
-

Post

-
    - {character.style.post.map((style) => ( -
  • {style}
  • - ))} -
-
-
-
+ { + character.style && ( +
+

Style

+
+ {character.style.all && character.style.all.length > 0 && ( +
+

General

+
    + {character.style.all.map((style) => ( +
  • {style}
  • + ))} +
+
+ )} + {character.style.chat && character.style.chat.length > 0 && ( +
+

Chat

+
    + {character.style.chat.map((style) => ( +
  • {style}
  • + ))} +
+
+ )} + {character.style.post && character.style.post.length > 0 && ( +
+

Post

+
    + {character.style.post.map((style) => ( +
  • {style}
  • + ))} +
+
+ )} +
+
+ ) + } -
-

Message Examples

-
- {character.messageExamples.map((conversation) => ( -
- {conversation.map((message) => ( -
-
{message.user}
-
{message.content.text}
+ { + character.messageExamples && character.messageExamples.length > 0 && ( +
+

Message Examples

+
+ {character.messageExamples.map((conversation) => ( +
+ {conversation.map((message) => ( +
+
{message.user}
+
{message.content.text}
+
+ ))}
))}
- ))} -
-
+ + ) + } diff --git a/src/pages/characters/[id].astro b/src/pages/characters/[id].astro index d1c2375..2a9b84b 100644 --- a/src/pages/characters/[id].astro +++ b/src/pages/characters/[id].astro @@ -7,6 +7,10 @@ import type { Character } from "../../types/character"; export async function getStaticPaths() { const characters = await getAllCharacters(); + if (!characters || !Array.isArray(characters)) { + throw new Error("Failed to fetch characters data"); + } + return characters.map((character) => ({ params: { id: character.id }, props: { character }, diff --git a/src/pages/index.astro b/src/pages/index.astro index 39f851f..8772571 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -3,7 +3,7 @@ import Layout from "../layouts/Layout.astro"; import CharacterList from "../components/CharacterList.astro"; import { getAllCharacters } from "../utils/characters"; -const characters = await getAllCharacters(); +const characters = (await getAllCharacters()).filter((char) => char != null); --- diff --git a/src/utils/characters.ts b/src/utils/characters.ts index 002fa26..1ad8f45 100644 --- a/src/utils/characters.ts +++ b/src/utils/characters.ts @@ -1,11 +1,23 @@ import type { Character } from '../types/character'; export async function getAllCharacters(): Promise { - // Use Astro's glob imports to load all JSON files at build time - const characterModules = await import.meta.glob('../data/characters/*.json', { - eager: true, - import: 'default', - }); + try { + // Use Astro's glob imports to load all JSON files at build time + const characterModules = await import.meta.glob('/src/data/characters/*.json', { + eager: true, + import: 'default', + }); - return Object.values(characterModules) as Character[]; + if (!characterModules || Object.keys(characterModules).length === 0) { + console.warn('No character modules found'); + return []; + } + + const characters = Object.values(characterModules) as Character[]; + console.log(`Found ${characters.length} characters`); + return characters; + } catch (error) { + console.error('Error loading characters:', error); + return []; + } }