import { ObjectImage } from "@thorium/cards/Navigation/ObjectDetails";
import { q } from "@thorium/context/AppContext";
import { useStation } from "@thorium/routes/station/useStation";
import {
planetScanTypes,
scanTypes,
shipScanTypes,
starScanTypes,
} from "@thorium/utils/flags/scanTypes";
import { capitalCase } from "change-case";
import type { ReactNode } from "react";
import { Button } from "react-aria-components";
import type { z } from "zod";
export const ScanComponents = {
shields: ShieldsResults,
cargo: CargoResults,
lifeSupport: null,
targeting: TargetingResults,
identification: IdentificationResults,
crew: CrewResults,
weapons: WeaponsResults,
engines: null,
damage: DamageResults,
communications: CommunicationsResults,
life: LifeResults,
atmosphere: AtmosphereResults,
temperature: TemperatureResults,
};
export function ScanResults({ objectId, type }: { objectId: number; type: string }) {
const scans =
type === "ship"
? shipScanTypes
: type === "planet"
? planetScanTypes
: type === "star"
? starScanTypes
: scanTypes.options;
return (
{["planet", "star"].includes(type) ? : null}
{scans.map((value) => {
const ScanComponent = ScanComponents[value];
if (!ScanComponent) return;
return (
);
})}
);
}
function ResultsWrapper({
scanType,
children,
objectId,
}: {
scanType: z.infer;
objectId: number;
children: ReactNode;
}) {
const { shipId } = useStation();
const [scans] = q.sensors.scans.useNetRequest({ shipId });
const isScanning = scans.some(
(scan) => scan.target === objectId && scan.type === scanType && scan.progress < 1,
);
return (
{capitalCase(scanType)}
{isScanning ? (
) : (
)}
{children}
);
}
function IdentificationResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.identification) return null;
return (
{results.identification.image ? (
) : null}
{results.identification.name}
{results.identification.classification}
Faction: {results.identification.factionName}
);
}
function CargoResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.cargo) return null;
const cargoEntries = Object.entries(results.cargo.cargo);
return (
{cargoEntries.length === 0
? "No cargo detected."
: cargoEntries.map(([name, amount]) => (
{name}: {amount}
))}
);
}
function TargetingResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.targeting) return null;
return (
Target: {results.targeting.targetName}
Targeted System: {results.targeting.targetedSystem}
);
}
function CrewResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.crew) return null;
return (
Population: {results.crew.count}
);
}
function WeaponsResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.weapons) return null;
return (
{results.weapons.weapons.map((weapon, index) => (
{capitalCase(weapon.type)}
{weapon.type === "phasers" ? `${Math.round(weapon.charge * 100)}%` : `${weapon.loaded}`}
))}
);
}
function DamageResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.damage) return null;
const damageEntries = Object.entries(results.damage.damage);
return (
{damageEntries.length === 0
? "No damage detected"
: damageEntries.map(([system, efficiency]) => (
{system}: {Math.round((1 - efficiency) * 100)}% Damaged
))}
);
}
function ShieldsResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.shields) return null;
return (
Status: {results.shields.status === "up" ? "Raised" : "Lowered"}
Strength:{" "}
{typeof results.shields.strength === "number"
? `${Math.round(results.shields.strength * 100)}%`
: "0%"}
);
}
function CommunicationsResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.communications) return null;
return (
{results.communications.status} — {results.communications.frequency}
);
}
function LifeResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.life) return null;
return (
Habitable: {results.life.isHabitable ? "Yes" : "No"}
Population: {results.life.population}
Lifeforms:
{results.life.lifeforms.map((l) => (
- {l}
))}
);
}
function AtmosphereResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.atmosphere) return null;
return (
{results.atmosphere.atmosphere.map((a) => (
-
{a.component}: {a.concentration}%
))}
);
}
function TemperatureResults({ objectId }: { objectId: number }) {
const { shipId } = useStation();
const [results] = q.sensors.scanResult.useNetRequest({ shipId, objectId });
if (!results.temperature) return null;
return (
Temperature: {results.temperature.temperature.toLocaleString()} K
);
}