'use client'
import Image from "next/image";
import {useEffect, useState} from "react";
import {Breed, TheCatAPI} from "@thatapicompany/thecatapi";
export default function Page() {
const [html, setHtml] = useState(
Loading...
);
const [apiKey, setApiKey] = useState("");
const [score, setScore] = useState(0);
const [message, setMessage] = useState("");
const [error, setError] = useState("");
const [currentImage, setCurrentImage] = useState("/loading.gif");
const [currentBreed, setCurrentBreed] = useState(Breed.AEGEAN);
const [randomBreeds, setRandomBreeds]: [randomBreeds: Breed[], setRandomBreeds: any] = useState([]);
const [api, setApi] = useState(new TheCatAPI(""));
useEffect(() => {
if (window.localStorage.getItem("apiKey") !== null && window.localStorage.getItem("apiKey") !== undefined) {
setApiKey(window.localStorage.getItem("apiKey") as string)
} else {
setApiKeyUi()
}
}, []);
useEffect(() => {
if (apiKey === "") {
return
}
setMessage("")
setApi(new TheCatAPI(apiKey))
}, [apiKey])
useEffect(() => {
if (apiKey === "") {
return
}
async function check() {
try {
const img= await api.images.searchImages({
limit: 1,
hasBreeds: true,
mimeTypes: ["jpg", "png"],
})
if (img[0].breeds === undefined) {
window.localStorage.removeItem("apiKey")
setError("API key error!")
return false
}
return true
} catch (e) {
console.error(e)
window.localStorage.removeItem("apiKey")
setError("API key error!")
return false
}
}
check().then((canContinue) => {
if (!canContinue) {
return
}
setNewImage()
})
}, [api])
useEffect(() => {
if (apiKey === "") {
return
}
if (currentImage === "") return
getRandomBreeds()
}, [currentImage])
useEffect(() => {
if (apiKey === "") {
return
}
if (randomBreeds.length === 0) return
setGameUi()
}, [randomBreeds])
async function setNewImage() {
try {
const img= await api.images.searchImages({
limit: 1,
hasBreeds: true,
mimeTypes: ["jpg", "png"],
})
if (img[0].breeds === undefined) {
window.localStorage.removeItem("apiKey")
setMessage("Error!")
return
}
setCurrentBreed(img[0].breeds[0].id)
setCurrentImage(img[0].url)
} catch (e) {
console.error(e)
window.localStorage.removeItem("apiKey")
setMessage("API key error!")
}
}
useEffect(() => {
if (error == "API key error!") setApiKeyUi()
}, [error])
function setApiKeyUi() {
setHtml(
{error}
Please set your own TheCatAPI key.
You can get one completely for free here.
)
}
function setGameUi() {
setHtml(
{
randomBreeds.map((breed, i) => {
return
})
}
Score: {score}
{message}
)
}
function shuffle(array: T[]): T[] {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function getRandomBreeds() {
let keys= Object.values(Breed).filter((breed) => breed !== currentBreed)
keys = shuffle(keys)
let items = []
for (let i = 0; i < 3; i++) {
items.push(keys[i])
}
items.push(currentBreed)
setRandomBreeds(shuffle(items))
}
function checkAnswer(answer: Breed) {
if (answer === currentBreed) {
setScore(score + 1)
setMessage("Correct!")
} else {
setScore(0)
setMessage("Incorrect!")
}
setNewImage()
}
return html
}