Skip to content

Commit

Permalink
Use de-duplicated species data methods
Browse files Browse the repository at this point in the history
Re alveusgg/data#87

Signed-off-by: flakey5 <[email protected]>
  • Loading branch information
flakey5 committed Dec 25, 2024
1 parent fdca5f8 commit 72fb99a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
21 changes: 12 additions & 9 deletions src/components/AmbassadorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, type Ref } from "react";
import type { CreateTypes } from "canvas-confetti";
import Confetti from "react-canvas-confetti";

import { getSpecies } from "../../../data/src/ambassadors/species";
import { calculateAge, formatDate, isBirthday } from "../utils/dateManager";
import { useAmbassador } from "../hooks/useAmbassadors";
import { camelToKebab } from "../utils/helpers";
Expand Down Expand Up @@ -119,6 +120,8 @@ export default function AmbassadorCard(props: AmbassadorCardProps) {

if (!ambassador) return null;

const species = getSpecies(ambassador.species);

return (
<>
{birthday && <Confetti onInit={confettiInit} />}
Expand Down Expand Up @@ -185,9 +188,9 @@ export default function AmbassadorCard(props: AmbassadorCardProps) {

<div>
<h3 className={headingClass}>Species</h3>
<p>{ambassador.species}</p>
<p>{species.name}</p>
<p>
<i>{ambassador.scientific}</i>{" "}
<i>{species.scientificName}</i>{" "}
<span className="text-alveus-green-200">
({ambassador.class.title})
</span>
Expand Down Expand Up @@ -252,34 +255,34 @@ export default function AmbassadorCard(props: AmbassadorCardProps) {

<div>
<h3 className={headingClass}>Native To</h3>
<p>{ambassador.native.text}</p>
<p>{species.native.text}</p>
</div>

<div>
<h3 className={headingClass}>Species Lifespan</h3>
<p>
Wild:{" "}
{"wild" in ambassador.lifespan &&
ambassador.lifespan.wild !== undefined ? (
{"wild" in species.lifespan &&
species.lifespan.wild !== undefined ? (
<>
<span className="text-base leading-none" title="Approx.">
~
</span>
{stringifyLifespan(ambassador.lifespan.wild)} years
{stringifyLifespan(species.lifespan.wild)} years
</>
) : (
"Unknown"
)}
</p>
<p>
Captivity:{" "}
{"captivity" in ambassador.lifespan &&
ambassador.lifespan.captivity !== undefined ? (
{"captivity" in species.lifespan &&
species.lifespan.captivity !== undefined ? (
<>
<span className="text-base leading-none" title="Approx.">
~
</span>
{stringifyLifespan(ambassador.lifespan.captivity)} years
{stringifyLifespan(species.lifespan.captivity)} years
</>
) : (
"Unknown"
Expand Down
32 changes: 14 additions & 18 deletions src/hooks/useAmbassadors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import { z } from "zod";

import allAmbassadors, {
ambassadorSchema,
} from "@alveusgg/data/src/ambassadors/core";
import { isActiveAmbassadorEntry } from "@alveusgg/data/src/ambassadors/filters";
import { getClassification } from "@alveusgg/data/src/ambassadors/classification";
} from "../../../data/src/ambassadors/core";
import { isActiveAmbassadorEntry } from "../../../data/src/ambassadors/filters";
import { getClassification } from "../../../data/src/ambassadors/classification";
import {
getAmbassadorImages,
ambassadorImageSchema,
} from "@alveusgg/data/src/ambassadors/images";
import { getIUCNStatus } from "@alveusgg/data/src/iucn";
} from "../../../data/src/ambassadors/images";
import { getIUCNStatus } from "../../../data/src/iucn";
import {
getSpecies,
speciesSchema,
} from "../../../data/src/ambassadors/species";

import {
typeSafeObjectEntries,
Expand All @@ -29,7 +33,7 @@ import winstonImage from "../assets/winston.png";
// These schema should match the type exposed by the API
const apiAmbassadorSchema = ambassadorSchema.extend({
image: ambassadorImageSchema,
iucn: ambassadorSchema.shape.iucn.extend({
iucn: speciesSchema.shape.iucn.extend({
title: z.string(),
}),
class: z.object({
Expand Down Expand Up @@ -65,15 +69,16 @@ const fallbackAmbassadors: Record<string, Ambassador> =
.filter(isActiveAmbassadorEntry)
.map<[string, Ambassador]>(([key, val]) => {
const image = getAmbassadorImages(key)[0];
const species = getSpecies(val.species);

return [
key,
{
...val,
image,
iucn: {
...val.iucn,
title: getIUCNStatus(val.iucn.status),
...species.iucn,
title: getIUCNStatus(species.iucn.status),
},
class: {
name: val.class,
Expand Down Expand Up @@ -136,8 +141,7 @@ const winston = {
name: "mammalia",
title: getClassification("mammalia"),
},
species: "Polar Bear",
scientific: "Twitchus memeticus",
species: "winston",
sex: "Male",
birth: "2020-04-01",
arrival: "2022-12-01",
Expand All @@ -152,14 +156,6 @@ const winston = {
"Winston was rescued by the Ontario Zoo in Canada after it was noticed that he was watching streams too often and not touching grass. Originally on loan to Alveus for two years, he is now a permanent resident of Texas.",
mission:
"He is an ambassador for stream-life balance and encouraging all chatters to step away from their devices more often.",
native: {
text: "Twitch chat (including the Animals, Aquariums, & Zoos category), miscellaneous emote services",
source:
"https://clips.twitch.tv/TangibleFurryTortoiseBCWarrior-izyQ3nOgq1pYe1rc", // https://clips.twitch.tv/CleverSecretiveAntChocolateRain--zjm5eRw6zxG75Up
},
lifespan: {
source: "",
},
clips: [],
homepage: null,
plush: null,
Expand Down

0 comments on commit 72fb99a

Please sign in to comment.