Skip to content

feat: created cached loader #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/pokedex/pokemon.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FunctionComponent } from 'react';
import { PokemonBase } from '@klifu/core/dist/types';
import { Box, Newline, Text } from 'ink';
import { PokemonTypeColor } from '../../utils/color';
import { PokemonTypeColor } from '../../utils';

export const PokemonView: FunctionComponent<{ pokemon?: PokemonBase }> = ({ pokemon }) => {

Expand Down
27 changes: 27 additions & 0 deletions src/hooks/klifu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Klifu from '@klifu/core';
import { PokemonBase } from '@klifu/core/dist/types';
import { useState, useEffect } from 'react';
import { Stash } from '../utils';


export const useKlifu = () => {
const stash = new Stash();
const [loading, setLoading] = useState<boolean>(true);
const [klifu, setKlifu] = useState<Klifu | undefined>();
const [error, setError] = useState(null);
const loadKlifu = async () => {
try {
let pokemons: Array<PokemonBase> = await stash.loadPokemonData();
setKlifu(await Klifu.load({ pokemonList: pokemons }));
setLoading(false);
} catch (error) {
setError(error);
setLoading(false);
}
}
useEffect(() => {
loadKlifu();
}, []);

return { loading, klifu, error };
}
57 changes: 57 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as path from 'path';
import * as fs from 'fs';
import { Pokemon, PokemonBase, PokemonType } from '@klifu/core/dist/types';
import {Cache} from '@klifu/core';

const DIR_PATH = path.resolve(__dirname);

export const filePaths = {
pokemons: path.resolve(DIR_PATH, 'pokemons.json')
}


type TypeColors = {
[pokemonType in PokemonType]: string;
};

export class PokemonTypeColor {
static readonly colors: TypeColors = {
Bug: 'green',
Dark: 'black',
Dragon: 'blueBright',
Electric: 'yellowBright',
Fairy: 'redBright',
Fighting: 'red',
Fire: 'yellow',
Flying: 'cyan',
Ghost: 'magenta',
Grass: 'green',
Ground: '#BA4A00',
Ice: 'blueBright',
Normal: 'white',
Poison: 'magentaBright',
Psychic: 'cyanBright',
Rock: '#F39C12',
Steel: '#D7BDE2',
Water: 'blue'
}

static get(pokemonType: PokemonType) {
return this.colors[pokemonType];
}
}

export class Stash {
private _filePaths = filePaths;
async loadPokemonData(): Promise<Array<PokemonBase>> {
let pokemons: Array<PokemonBase>
if(fs.existsSync(this._filePaths.pokemons)) {
pokemons = JSON.parse(fs.readFileSync(this._filePaths.pokemons, 'utf-8'));
return pokemons;
}else {
pokemons = await Cache.pokemons();
fs.writeFileSync(this._filePaths.pokemons, JSON.stringify(pokemons), {encoding: 'utf-8'});
}
return pokemons;
}
}
32 changes: 0 additions & 32 deletions src/utils/color.ts

This file was deleted.