-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- reactivated cache validation - fixed fetching logic - audit fix
- Loading branch information
Showing
9 changed files
with
115 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,65 @@ | ||
import { columns } from "./components/columns"; | ||
"use client"; | ||
|
||
import { useState, useEffect } from "react"; | ||
import { DataTable } from "./components/data-table"; | ||
import { statusAPI } from "@/components/widgets/util/getApiData"; | ||
import { columns } from "./components/columns"; | ||
import { fetchPlanetsData } from "../util/getWarMap"; | ||
|
||
interface Planet { | ||
name: string; | ||
playerCount: number; | ||
health: number; | ||
name: string; | ||
maxHealth: number; | ||
initialOwner: string; | ||
event: any; | ||
position: { | ||
x: number; | ||
y: number; | ||
}; | ||
event: any; | ||
liberationPercentage: number; | ||
} | ||
|
||
export default async function TargetsTable() { | ||
const targets = await statusAPI(); | ||
|
||
const flattenedPlanets = targets.map( | ||
({ | ||
name, | ||
health, | ||
initialOwner, | ||
statistics, | ||
}: { | ||
statistics: { playerCount: string }; | ||
name: string; | ||
players: number; | ||
health: number; | ||
initialOwner: string; | ||
}) => ({ | ||
name, | ||
health: health / 10000, | ||
initialOwner, | ||
playerCount: statistics.playerCount, | ||
}), | ||
); | ||
// Transform Planet type into the format expected by DataTable | ||
const transformPlanetData = (planets: Planet[]) => { | ||
return planets.map((planet) => ({ | ||
players: planet.playerCount, | ||
liberation: planet.liberationPercentage, | ||
name: planet.name, | ||
initial_owner: planet.initialOwner, | ||
})); | ||
}; | ||
|
||
export default function TargetsTable() { | ||
const [planets, setPlanets] = useState<Planet[]>([]); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const planetsData = await fetchPlanetsData(); | ||
setPlanets(planetsData); | ||
} catch (err) { | ||
setError("Failed to fetch data"); | ||
} | ||
}; | ||
fetchData(); | ||
}, []); | ||
|
||
const eventPlanets = flattenedPlanets.filter((planet: Planet) => { | ||
return planet.event === null; | ||
}); | ||
if (error) { | ||
return <div>Error: {error}</div>; | ||
} | ||
|
||
// Filter planets that have players, an active event, or liberation between 0% and 99% | ||
const eventPlanets = planets.filter((planet) => planet.event !== null); | ||
|
||
const activePlanets = planets.filter( | ||
(planet) => planet.health < planet.maxHealth, | ||
); | ||
|
||
const activePlanets = flattenedPlanets.filter((planet: Planet) => { | ||
return planet.health !== 100; | ||
}); | ||
const active = [...eventPlanets, ...activePlanets]; | ||
|
||
const filtered = [...eventPlanets, ...activePlanets]; | ||
// Transform data to fit DataTable requirements | ||
const transformedData = transformPlanetData(active); | ||
|
||
return <DataTable data={filtered} columns={columns} />; | ||
return <DataTable data={transformedData} columns={columns} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { z } from "zod" | ||
import { z } from "zod"; | ||
|
||
// We're keeping a simple non-relational schema here. | ||
// IRL, you will have a schema for your data models. | ||
export const targetsSchema = z.object({ | ||
players: z.number(), | ||
liberation: z.number(), | ||
|
||
planet: z.object({ | ||
name: z.string(), | ||
initial_owner: z.string(), | ||
}), | ||
|
||
name: z.string(), | ||
initial_owner: z.string(), | ||
}); | ||
|
||
export type Target = z.infer<typeof targetsSchema> | ||
export type Target = z.infer<typeof targetsSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.