8e552f556d
Polish pass after the first F8-10 walkthrough:
- table-planets: moved the `foreign` chip to the end of the row and
hid the race dropdown until `foreign` is on (it never made sense
to pick a race while the bucket itself was off).
- persistent per-table filter / sort state — extracted to
`table-{planets,ship-groups,fleets}-state.svelte.ts` singletons so
a row click → map → back to the table restores the prior chip /
dropdown / sort state. Held in memory only; an F5 still resets.
- table-ship-groups: the planet and class dropdowns now narrow to
the slice surviving the owner checkboxes, so toggling `foreign`
off removes planets / classes touched only by foreign rows.
- map.svelte: camera (centre + zoom) is captured on every dispose
path into a new `GameStateStore.lastCamera` and consumed on the
next mount, so leaving the map for any other active view and
coming back restores the prior pan / zoom. A pending focus from
the tables still wins for the centre point.
- table-ship-classes: `:disabled` now reads as disabled (muted
colour, no hover ring, not-allowed cursor) — the click was already
a no-op, only the visual was lying.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
371 lines
9.4 KiB
Svelte
371 lines
9.4 KiB
Svelte
<!--
|
|
Phase 17 ship-classes table. Renders the player's designed ship
|
|
classes with sort, filter, double-tap-to-open-designer, and a
|
|
per-row Delete action. Source data is the optimistic overlay
|
|
(`RenderedReportSource.report.localShipClass`) so a freshly
|
|
queued `createShipClass` shows up immediately and a freshly
|
|
queued `removeShipClass` disappears immediately — both before the
|
|
auto-sync round-trip lands.
|
|
|
|
The component sits inside the active-view slot owned by
|
|
`routes/games/[id]/+layout.svelte`, so it inherits the per-game
|
|
`OrderDraftStore` and `RenderedReportSource` through context. No
|
|
data fetching is performed here — the layout is responsible.
|
|
-->
|
|
<script lang="ts">
|
|
import { getContext } from "svelte";
|
|
|
|
import type { ShipClassSummary } from "../../api/game-state";
|
|
import { i18n, type TranslationKey } from "$lib/i18n/index.svelte";
|
|
import {
|
|
RENDERED_REPORT_CONTEXT_KEY,
|
|
type RenderedReportSource,
|
|
} from "$lib/rendered-report.svelte";
|
|
import {
|
|
ORDER_DRAFT_CONTEXT_KEY,
|
|
OrderDraftStore,
|
|
} from "../../sync/order-draft.svelte";
|
|
import { calculatorLoadRequest } from "$lib/calculator/load-request.svelte";
|
|
import ViewState from "$lib/ui/view-state.svelte";
|
|
import { formatFloat } from "$lib/util/number-format";
|
|
|
|
type SortColumn =
|
|
| "name"
|
|
| "drive"
|
|
| "armament"
|
|
| "weapons"
|
|
| "shields"
|
|
| "cargo";
|
|
type SortDirection = "asc" | "desc";
|
|
|
|
const COLUMN_LABELS: Record<SortColumn, TranslationKey> = {
|
|
name: "game.table.ship_classes.column.name",
|
|
drive: "game.table.ship_classes.column.drive",
|
|
armament: "game.table.ship_classes.column.armament",
|
|
weapons: "game.table.ship_classes.column.weapons",
|
|
shields: "game.table.ship_classes.column.shields",
|
|
cargo: "game.table.ship_classes.column.cargo",
|
|
};
|
|
|
|
const COLUMNS: readonly SortColumn[] = [
|
|
"name",
|
|
"drive",
|
|
"armament",
|
|
"weapons",
|
|
"shields",
|
|
"cargo",
|
|
];
|
|
|
|
const rendered = getContext<RenderedReportSource | undefined>(
|
|
RENDERED_REPORT_CONTEXT_KEY,
|
|
);
|
|
const draft = getContext<OrderDraftStore | undefined>(
|
|
ORDER_DRAFT_CONTEXT_KEY,
|
|
);
|
|
|
|
|
|
let sortColumn: SortColumn = $state("name");
|
|
let sortDirection: SortDirection = $state("asc");
|
|
let filter: string = $state("");
|
|
|
|
const localShipClass = $derived<ShipClassSummary[]>(
|
|
rendered?.report?.localShipClass ?? [],
|
|
);
|
|
const reportLoaded = $derived(rendered?.report !== null && rendered?.report !== undefined);
|
|
|
|
// inUseCounts is a derived index from class name → number of player
|
|
// ship groups currently referencing it. The engine refuses
|
|
// `removeShipClass` while any such group exists, so the table
|
|
// pre-emptively disables the per-row Delete affordance instead of
|
|
// surfacing a server-side rejection. The map is rebuilt whenever
|
|
// the rendered report changes; an empty report yields an empty map
|
|
// and every Delete stays enabled.
|
|
const inUseCounts = $derived.by(() => {
|
|
const map = new Map<string, number>();
|
|
for (const group of rendered?.report?.localShipGroups ?? []) {
|
|
map.set(group.class, (map.get(group.class) ?? 0) + 1);
|
|
}
|
|
return map;
|
|
});
|
|
|
|
const filtered = $derived.by(() => {
|
|
const needle = filter.trim().toLowerCase();
|
|
if (needle === "") return localShipClass;
|
|
return localShipClass.filter((cls) =>
|
|
cls.name.toLowerCase().includes(needle),
|
|
);
|
|
});
|
|
|
|
const sorted = $derived.by(() => {
|
|
const list = [...filtered];
|
|
const dir = sortDirection === "asc" ? 1 : -1;
|
|
list.sort((a, b) => {
|
|
if (sortColumn === "name") {
|
|
return a.name.localeCompare(b.name) * dir;
|
|
}
|
|
return (a[sortColumn] - b[sortColumn]) * dir;
|
|
});
|
|
return list;
|
|
});
|
|
|
|
function toggleSort(column: SortColumn): void {
|
|
if (sortColumn === column) {
|
|
sortDirection = sortDirection === "asc" ? "desc" : "asc";
|
|
return;
|
|
}
|
|
sortColumn = column;
|
|
sortDirection = "asc";
|
|
}
|
|
|
|
function ariaSort(column: SortColumn): "ascending" | "descending" | "none" {
|
|
if (sortColumn !== column) return "none";
|
|
return sortDirection === "asc" ? "ascending" : "descending";
|
|
}
|
|
|
|
function openInCalculator(name: string): void {
|
|
calculatorLoadRequest.request(name);
|
|
}
|
|
|
|
function newShipClass(): void {
|
|
calculatorLoadRequest.request(null);
|
|
}
|
|
|
|
async function deleteShipClass(name: string): Promise<void> {
|
|
if (draft === undefined) return;
|
|
await draft.add({
|
|
kind: "removeShipClass",
|
|
id: crypto.randomUUID(),
|
|
name,
|
|
});
|
|
}
|
|
|
|
function deleteTooltip(count: number): string {
|
|
if (count === 0) return "";
|
|
return i18n.t("game.table.ship_classes.action.delete.in_use", {
|
|
count: String(count),
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<section
|
|
class="active-view"
|
|
data-testid="active-view-table"
|
|
data-entity="ship-classes"
|
|
>
|
|
<header>
|
|
<h2>{i18n.t("game.table.ship_classes.title")}</h2>
|
|
<div class="controls">
|
|
<input
|
|
type="search"
|
|
class="filter"
|
|
data-testid="ship-classes-filter"
|
|
placeholder={i18n.t("game.table.ship_classes.filter.placeholder")}
|
|
bind:value={filter}
|
|
/>
|
|
<button
|
|
type="button"
|
|
class="new"
|
|
data-testid="ship-classes-new"
|
|
onclick={newShipClass}
|
|
>
|
|
{i18n.t("game.table.ship_classes.action.new")}
|
|
</button>
|
|
</div>
|
|
</header>
|
|
|
|
{#if !reportLoaded}
|
|
<ViewState
|
|
kind="loading"
|
|
testid="ship-classes-loading"
|
|
message={i18n.t("game.table.ship_classes.loading")}
|
|
/>
|
|
{:else if localShipClass.length === 0}
|
|
<ViewState
|
|
kind="empty"
|
|
testid="ship-classes-empty"
|
|
message={i18n.t("game.table.ship_classes.empty")}
|
|
/>
|
|
{:else}
|
|
<table class="grid" data-testid="ship-classes-table">
|
|
<thead>
|
|
<tr>
|
|
{#each COLUMNS as column (column)}
|
|
<th aria-sort={ariaSort(column)} class:numeric={column !== "name"}>
|
|
<button
|
|
type="button"
|
|
class="sort"
|
|
data-testid="ship-classes-column-{column}"
|
|
onclick={() => toggleSort(column)}
|
|
>
|
|
{i18n.t(COLUMN_LABELS[column])}
|
|
{#if sortColumn === column}
|
|
<span class="sort-indicator" aria-hidden="true">
|
|
{sortDirection === "asc" ? "▲" : "▼"}
|
|
</span>
|
|
{/if}
|
|
</button>
|
|
</th>
|
|
{/each}
|
|
<th>{i18n.t("game.table.ship_classes.column.actions")}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{#each sorted as cls (cls.name)}
|
|
{@const inUse = inUseCounts.get(cls.name) ?? 0}
|
|
<tr
|
|
data-testid="ship-classes-row"
|
|
data-name={cls.name}
|
|
ondblclick={() => openInCalculator(cls.name)}
|
|
>
|
|
<td data-testid="ship-classes-cell-name">{cls.name}</td>
|
|
<td class="numeric" data-testid="ship-classes-cell-drive">
|
|
{formatFloat(cls.drive)}
|
|
</td>
|
|
<td class="numeric" data-testid="ship-classes-cell-armament">
|
|
{cls.armament}
|
|
</td>
|
|
<td class="numeric" data-testid="ship-classes-cell-weapons">
|
|
{formatFloat(cls.weapons)}
|
|
</td>
|
|
<td class="numeric" data-testid="ship-classes-cell-shields">
|
|
{formatFloat(cls.shields)}
|
|
</td>
|
|
<td class="numeric" data-testid="ship-classes-cell-cargo">
|
|
{formatFloat(cls.cargo)}
|
|
</td>
|
|
<td>
|
|
<button
|
|
type="button"
|
|
class="delete"
|
|
data-testid="ship-classes-delete"
|
|
data-in-use={inUse}
|
|
disabled={inUse > 0 || draft === undefined}
|
|
title={deleteTooltip(inUse)}
|
|
onclick={() => void deleteShipClass(cls.name)}
|
|
>
|
|
{i18n.t("game.table.ship_classes.action.delete")}
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
{/each}
|
|
</tbody>
|
|
</table>
|
|
{/if}
|
|
</section>
|
|
|
|
<style>
|
|
.active-view {
|
|
padding: 1.5rem;
|
|
font-family: system-ui, sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 1rem;
|
|
}
|
|
header {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.5rem;
|
|
}
|
|
header h2 {
|
|
margin: 0;
|
|
font-size: 1.1rem;
|
|
}
|
|
.controls {
|
|
display: flex;
|
|
gap: 0.5rem;
|
|
align-items: center;
|
|
flex-wrap: wrap;
|
|
}
|
|
.filter {
|
|
font: inherit;
|
|
padding: 0.3rem 0.5rem;
|
|
background: var(--color-bg);
|
|
color: var(--color-text);
|
|
border: 1px solid var(--color-border);
|
|
border-radius: 3px;
|
|
flex: 1 1 12rem;
|
|
min-width: 8rem;
|
|
}
|
|
.new {
|
|
font: inherit;
|
|
font-size: 0.9rem;
|
|
padding: 0.3rem 0.7rem;
|
|
background: transparent;
|
|
color: var(--color-text-muted);
|
|
border: 1px solid var(--color-border);
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
}
|
|
.new:hover {
|
|
color: var(--color-text);
|
|
border-color: var(--color-accent);
|
|
}
|
|
.grid {
|
|
border-collapse: collapse;
|
|
width: 100%;
|
|
font-variant-numeric: tabular-nums;
|
|
}
|
|
.grid th,
|
|
.grid td {
|
|
padding: 0.4rem 0.6rem;
|
|
text-align: left;
|
|
border-bottom: 1px solid var(--color-border-subtle);
|
|
font-size: 0.85rem;
|
|
}
|
|
.grid th {
|
|
color: var(--color-text-muted);
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.04em;
|
|
font-size: 0.75rem;
|
|
}
|
|
.grid th.numeric,
|
|
.grid td.numeric {
|
|
font-family: var(--font-mono);
|
|
text-align: right;
|
|
}
|
|
.grid th.numeric .sort {
|
|
justify-content: flex-end;
|
|
}
|
|
.grid tbody tr {
|
|
cursor: pointer;
|
|
}
|
|
.grid tbody tr:hover {
|
|
background: var(--color-surface);
|
|
}
|
|
.sort {
|
|
font: inherit;
|
|
font-size: inherit;
|
|
text-transform: inherit;
|
|
letter-spacing: inherit;
|
|
color: inherit;
|
|
background: transparent;
|
|
border: none;
|
|
padding: 0;
|
|
cursor: pointer;
|
|
display: inline-flex;
|
|
gap: 0.3rem;
|
|
align-items: baseline;
|
|
}
|
|
.sort-indicator {
|
|
font-size: 0.7em;
|
|
}
|
|
.delete {
|
|
font: inherit;
|
|
font-size: 0.85rem;
|
|
padding: 0.15rem 0.5rem;
|
|
background: transparent;
|
|
color: var(--color-danger);
|
|
border: 1px solid var(--color-border);
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
}
|
|
.delete:not(:disabled):hover {
|
|
border-color: var(--color-danger);
|
|
}
|
|
.delete:disabled {
|
|
cursor: not-allowed;
|
|
color: var(--color-text-muted);
|
|
opacity: 0.55;
|
|
}
|
|
</style>
|