Skip to content

Commit

Permalink
Use de-duplicated species data methods (#214)
Browse files Browse the repository at this point in the history
* Use de-duplicated species data methods

Re alveusgg/data#87

Signed-off-by: flakey5 <[email protected]>

* update

Signed-off-by: flakey5 <[email protected]>

* bump data version

Signed-off-by: flakey5 <[email protected]>

---------

Signed-off-by: flakey5 <[email protected]>
Co-authored-by: Matt Cowley <[email protected]>
  • Loading branch information
flakey5 and MattIPv4 authored Dec 31, 2024
1 parent ac3a395 commit 95e48d5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 50 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"format": "run-p format:*"
},
"dependencies": {
"@alveusgg/data": "github:alveusgg/data#0.49.0",
"@alveusgg/data": "github:alveusgg/data#0.50.0",
"@headlessui/react": "^2.2.0",
"react": "^19.0.0",
"react-canvas-confetti": "^2.0.7",
Expand Down
4 changes: 3 additions & 1 deletion src/components/AmbassadorButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export default function AmbassadorButton(props: AmbassadorButtonProps) {

<div className="my-auto px-1 pb-2 pt-2">
<h2 className="text-sm">{ambassador.name}</h2>
<h3 className="text-alveus-green-200 text-xs">{ambassador.species}</h3>
<h3 className="text-alveus-green-200 text-xs">
{ambassador.species.name}
</h3>
</div>
</button>
);
Expand Down
23 changes: 12 additions & 11 deletions src/components/AmbassadorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ export default function AmbassadorCard(props: AmbassadorCardProps) {

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

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

<div>
<h3 className={headingClass}>Species Lifespan</h3>
<p>
Wild:{" "}
{"wild" in ambassador.lifespan &&
ambassador.lifespan.wild !== undefined ? (
{"wild" in ambassador.species.lifespan &&
ambassador.species.lifespan.wild !== undefined ? (
<>
<span className="text-base leading-none" title="Approx.">
~
</span>
{stringifyLifespan(ambassador.lifespan.wild)} years
{stringifyLifespan(ambassador.species.lifespan.wild)} years
</>
) : (
"Unknown"
)}
</p>
<p>
Captivity:{" "}
{"captivity" in ambassador.lifespan &&
ambassador.lifespan.captivity !== undefined ? (
{"captivity" in ambassador.species.lifespan &&
ambassador.species.lifespan.captivity !== undefined ? (
<>
<span className="text-base leading-none" title="Approx.">
~
</span>
{stringifyLifespan(ambassador.lifespan.captivity)} years
{stringifyLifespan(ambassador.species.lifespan.captivity)}{" "}
years
</>
) : (
"Unknown"
Expand Down
78 changes: 45 additions & 33 deletions src/hooks/useAmbassadors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
ambassadorImageSchema,
} from "@alveusgg/data/src/ambassadors/images";
import { getIUCNStatus } from "@alveusgg/data/src/iucn";
import {
getSpecies,
speciesSchema,
} from "@alveusgg/data/src/ambassadors/species";

import {
typeSafeObjectEntries,
Expand All @@ -29,17 +33,19 @@ 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({
title: z.string(),
}),
class: z.object({
name: ambassadorSchema.shape.class,
title: z.string(),
species: speciesSchema.extend({
iucn: speciesSchema.shape.iucn.extend({
title: z.string(),
}),
class: z.object({
name: speciesSchema.shape.class,
title: z.string(),
}),
}),
});

const apiSchema = z.object({
ambassadors: z.record(apiAmbassadorSchema),
v2: z.record(apiAmbassadorSchema),
});

type Ambassador = z.infer<typeof apiAmbassadorSchema>;
Expand All @@ -56,7 +62,7 @@ const fetchAmbassadors = async (): Promise<Record<string, Ambassador>> => {
);

const data = await response.json();
return apiSchema.parse(data).ambassadors;
return apiSchema.parse(data).v2;
};

const fallbackAmbassadors: Record<string, Ambassador> =
Expand All @@ -65,19 +71,23 @@ 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),
},
class: {
name: val.class,
title: getClassification(val.class),
species: {
...species,
iucn: {
...species.iucn,
title: getIUCNStatus(species.iucn.status),
},
class: {
name: species.class,
title: getClassification(species.class),
},
},
},
];
Expand Down Expand Up @@ -132,34 +142,36 @@ const winston = {
name: "Winston",
alternate: [],
commands: ["winston"],
class: {
name: "mammalia",
title: getClassification("mammalia"),
species: {
name: "Polar Bear",
scientificName: "Twitchus memeticus",
iucn: {
id: null,
title: getIUCNStatus("NE"),
status: "NE",
},
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: "",
},
class: {
name: "mammalia",
title: getClassification("mammalia"),
},
},
species: "Polar Bear",
scientific: "Twitchus memeticus",
sex: "Male",
birth: "2020-04-01",
arrival: "2022-12-01",
retired: null,
iucn: {
id: null,
status: "NE",
title: getIUCNStatus("NE"),
},
enclosure: "wolves",
story:
"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 95e48d5

Please sign in to comment.