Skip to content

Commit

Permalink
24.09.10
Browse files Browse the repository at this point in the history
    - reactivated cache validation
    - fixed fetching logic
    - audit fix
  • Loading branch information
Mixhi1845 committed Sep 10, 2024
1 parent 67d1009 commit 2c3bbb0
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 86 deletions.
9 changes: 5 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 src/components/widgets/latestOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default async function majorOrder() {
{order.major_order[0].setting.overrideBrief}
</span>
</p>
<p className="mt-4">{order.major_order[0].setting.taskDescription}</p>
{/*<p className="mt-4">{order.major_order[0].setting.taskDescription}</p>*/}
</div>
<div
className={`grid ${
Expand Down
15 changes: 9 additions & 6 deletions src/components/widgets/targets/components/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,40 @@ export const columns: ColumnDef<Target>[] = [
enableHiding: false,
},
{
accessorKey: "health",
accessorKey: "liberation",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Liberation" />
),
cell: ({ row }) => (
<div className="flex w-[40px] items-center md:w-[80px]">
<Badge className="mr-1">{Math.round(row.getValue("health"))}%</Badge>
<Badge className="mr-1">
{Math.round(row.getValue("liberation"))}%
</Badge>
</div>
),
},
{
accessorKey: "playerCount",
accessorKey: "players",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Player Count" />
),
cell: ({ row }) => (
<div className="w-[40px] md:w-[80px]">
{formatNumber(row.getValue("playerCount"))}
{formatNumber(row.getValue("players"))}
</div>
),
enableHiding: false,
enableSorting: false,
},
{
accessorKey: "initialOwner",
accessorKey: "initial_owner",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Owner" />
),
cell: ({ row }) => {
const initial_owner = species.find(
(initial_owner) => initial_owner.value === row.getValue("initialOwner"),
(initial_owner) =>
initial_owner.value === row.getValue("initial_owner"),
);

if (!initial_owner) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/widgets/targets/components/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export function DataTable<TData, TValue>({
initialState: {
sorting: [
{
id: "health",
desc: true, // sort by name in descending order by default
id: "liberation",
desc: false, // sort by name in descending order by default
},
],
},
Expand Down
82 changes: 48 additions & 34 deletions src/components/widgets/targets/currentTargets.tsx
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} />;
}
12 changes: 5 additions & 7 deletions src/components/widgets/targets/data/schema.ts
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>;
5 changes: 2 additions & 3 deletions src/components/widgets/util/getApiData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export const revalidate = 3600;

export async function API() {
const res = await fetch("https://api.diveharder.com/v1/all", {
headers: {
"User-Agent": "Helldivers 2 Companion - helldiverscompanion.app",
"Accept-Language": "en-US",
"X-Super-Client": "Helldivers 2 Companion - helldiverscompanion.app",
"X-Super-Contact": "@.mixhi",
cache: "no-store",
},
});
return res.json();
Expand All @@ -21,7 +22,6 @@ export async function statusAPI() {
"Accept-Language": "en-US",
"X-Super-Client": "Helldivers 2 Companion - helldiverscompanion.app",
"X-Super-Contact": "@.mixhi",
cache: "no-store",
},
});
return res.json();
Expand All @@ -34,7 +34,6 @@ export async function stratagemsAPI() {
headers: {
"User-Agent": "Helldivers 2 Companion - helldiverscompanion.app",
"Accept-Language": "en-US",
cache: "no-store",
},
},
);
Expand Down
13 changes: 11 additions & 2 deletions src/components/widgets/util/getWarMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import axios from "axios";
interface Planet {
playerCount: number;
health: number;
maxHealth: number;
name: string;
initialOwner: string;
position: {
x: number;
y: number;
};
event: any;
liberationPercentage: number;
}

export async function fetchPlanetsData(): Promise<Planet[]> {
Expand All @@ -23,22 +25,29 @@ export async function fetchPlanetsData(): Promise<Planet[]> {
name,
players,
health,
maxHealth,
initialOwner,
event,
statistics,
position,
}: {
statistics: { playerCount: string };
name: string;
players: number;
health: number;
maxHealth: number;
initialOwner: string;
event: any;
position: { x: number; y: number };
statistics: { playerCount: number };
}) => ({
name,
players,
health: health / 10000,
health,
maxHealth,
initialOwner,
playerCount: statistics.playerCount,
event,
liberationPercentage: (health / maxHealth) * 100, // Calculate liberation percentage
position: { x: position.x, y: position.y },
}),
);
Expand Down
Loading

0 comments on commit 2c3bbb0

Please sign in to comment.