mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 05:13:48 +00:00
bcc453e8cf
## Description Adds a modal URL router so modals can be opened, deep-linked, and bookmarked via the hash. URLs of the form `#modal=<name>&tab=<key>&...` open the named modal and pass remaining keys as args to `onOpen`. The reverse direction also syncs: opening a modal via the UI updates the URL, closing it clears the hash, and switching tabs updates `&tab=`. Builds on the BaseModal refactor from #3923. ### What's new **`ModalRouter.ts`** — small registry + two-way sync helper. - `register(name, { tag, pageId? })` declares a modal as router-managed - `routeFromHash()` parses `#modal=...` and dispatches to `modal.open(args)` - `syncOpened/syncClosed/syncTab` push state back into the URL via `history.replaceState` (no history entries) - A `routingFromUrl` flag prevents URL→modal→URL feedback loops - Unknown modal names silently strip the hash **`BaseModal`** — opt-in URL sync via a `routerName` property. - When set, BaseModal calls into `modalRouter.syncOpened/syncClosed/syncTab` from `open` / `close` / `setActiveTab` - Modals that own their own URL state (lobby modals) just leave `routerName` undefined **`Main.ts`** — registers all routable modals and wires the router. - `handleUrl()`: adds a `modalRouter.routeFromHash()` branch after the path-based lobby join - `onHashUpdate`: when the hash is router-managed, routes via the router instead of tearing down lobby state ### Routable modals 13 inline modals: store, settings, leaderboard, clan, account, help, news, language, single-player, ranked, troubleshooting, territory-patterns, flag-input. Excluded by design: join-lobby, host-lobby (own URL state via `/game/<id>`), matchmaking (no URL state). ### Example uses - Deep link to store flags tab: `/#modal=store&tab=flags` - Settings keybinds tab: `/#modal=settings&tab=keybinds` - Cosmetics.ts now redirects to `#modal=store&tab=packs` when a hard-currency purchase fails for insufficient plutonium (after the alert), so users can top up directly ### URL behavior - `replaceState` everywhere — no history entries added when modals open / close / switch tabs - Browser back/forward still works for the existing path-based game flow - `hashchange` events are router-aware so external hash changes (back button, manual edit) correctly switch between routed modals without tearing down lobby state ## Please complete the following: - [x] I have added screenshots for all UI updates _(no visual changes; smoke-tested in dev)_ - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file _(no new user-visible strings)_ - [ ] I have added relevant tests to the test directory _(no test coverage; manually tested URL load, UI open, tab switch, close, hashchange, insufficient-plutonium redirect)_ - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: DISCORD_USERNAME
206 lines
5.9 KiB
TypeScript
206 lines
5.9 KiB
TypeScript
import { html } from "lit";
|
|
import { customElement, state } from "lit/decorators.js";
|
|
import Countries from "resources/countries.json" with { type: "json" };
|
|
import { UserMeResponse } from "src/core/ApiSchemas";
|
|
import { assetUrl } from "src/core/AssetUrls";
|
|
import { Cosmetics, Flag } from "src/core/CosmeticSchemas";
|
|
import { UserSettings } from "src/core/game/UserSettings";
|
|
import { getUserMe } from "./Api";
|
|
import {
|
|
fetchCosmetics,
|
|
flagRelationship,
|
|
ResolvedCosmetic,
|
|
} from "./Cosmetics";
|
|
import { translateText } from "./Utils";
|
|
import { BaseModal } from "./components/BaseModal";
|
|
import "./components/CosmeticButton";
|
|
import "./components/NotLoggedInWarning";
|
|
import { modalHeader } from "./components/ui/ModalHeader";
|
|
|
|
function countryFlag(name: string, code: string): Flag {
|
|
return {
|
|
name,
|
|
url: assetUrl(`/flags/${code}.svg`),
|
|
product: null,
|
|
rarity: "common",
|
|
affiliateCode: null,
|
|
};
|
|
}
|
|
|
|
@customElement("flag-input-modal")
|
|
export class FlagInputModal extends BaseModal {
|
|
protected routerName = "flag-input";
|
|
|
|
@state() private search = "";
|
|
@state() private cosmetics: Cosmetics | null = null;
|
|
@state() private userMe: UserMeResponse | false = false;
|
|
public returnTo = "";
|
|
|
|
updated(changedProperties: Map<string | number | symbol, unknown>) {
|
|
super.updated(changedProperties);
|
|
}
|
|
|
|
private renderFlags() {
|
|
const userSettings = new UserSettings();
|
|
const selectedFlag = userSettings.getFlag() ?? "";
|
|
|
|
const cosmeticFlags = Object.entries(this.cosmetics?.flags ?? {})
|
|
.filter(([, flag]) => {
|
|
if (!this.includedInSearch({ name: flag.name, code: flag.name }))
|
|
return false;
|
|
return flagRelationship(flag, this.userMe, null) === "owned";
|
|
})
|
|
.map(([key, flag]) => {
|
|
const r: ResolvedCosmetic = {
|
|
type: "flag",
|
|
cosmetic: flag,
|
|
colorPalette: null,
|
|
relationship: "owned",
|
|
key: `flag:${key}`,
|
|
};
|
|
return html`
|
|
<cosmetic-button
|
|
.resolved=${r}
|
|
.selected=${selectedFlag === `flag:${key}`}
|
|
.onSelect=${() => {
|
|
this.setFlag(`flag:${key}`);
|
|
this.close();
|
|
}}
|
|
></cosmetic-button>
|
|
`;
|
|
});
|
|
|
|
const noFlagResolved: ResolvedCosmetic = {
|
|
type: "flag",
|
|
cosmetic: countryFlag("None", "xx"),
|
|
colorPalette: null,
|
|
relationship: "owned",
|
|
key: "country:xx",
|
|
};
|
|
const noFlag = this.search
|
|
? null
|
|
: html`
|
|
<cosmetic-button
|
|
.resolved=${noFlagResolved}
|
|
.selected=${selectedFlag === "" || selectedFlag === "country:xx"}
|
|
.onSelect=${() => {
|
|
this.setFlag("country:xx");
|
|
this.close();
|
|
}}
|
|
></cosmetic-button>
|
|
`;
|
|
|
|
const countryFlags = Countries.filter(
|
|
(country) =>
|
|
country.code !== "xx" &&
|
|
!country.restricted &&
|
|
this.includedInSearch(country),
|
|
).map((country) => {
|
|
const r: ResolvedCosmetic = {
|
|
type: "flag",
|
|
cosmetic: countryFlag(country.name, country.code),
|
|
colorPalette: null,
|
|
relationship: "owned",
|
|
key: `country:${country.code}`,
|
|
};
|
|
return html`
|
|
<cosmetic-button
|
|
.resolved=${r}
|
|
.selected=${selectedFlag === `country:${country.code}`}
|
|
.onSelect=${() => {
|
|
this.setFlag(`country:${country.code}`);
|
|
this.close();
|
|
}}
|
|
></cosmetic-button>
|
|
`;
|
|
});
|
|
|
|
return html`
|
|
<div
|
|
class="flex flex-wrap gap-4 p-8 justify-center items-stretch content-start"
|
|
>
|
|
${noFlag} ${cosmeticFlags} ${countryFlags}
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
protected renderHeaderSlot() {
|
|
return html`
|
|
<div
|
|
class="relative flex flex-col border-b border-white/10 pb-4 shrink-0"
|
|
>
|
|
${modalHeader({
|
|
title: translateText("flag_input.title"),
|
|
onBack: () => this.close(),
|
|
ariaLabel: translateText("common.back"),
|
|
rightContent: html`<not-logged-in-warning></not-logged-in-warning>`,
|
|
})}
|
|
|
|
<div class="md:flex items-center gap-2 justify-center mt-4">
|
|
<input
|
|
class="h-12 w-full max-w-md border border-white/10 bg-black/60
|
|
rounded-xl shadow-inner text-xl text-center focus:outline-none
|
|
focus:ring-2 focus:ring-blue-500/50 focus:border-blue-500 text-white placeholder-white/30 transition-all"
|
|
type="text"
|
|
placeholder=${translateText("flag_input.search_flag")}
|
|
.value=${this.search}
|
|
@change=${this.handleSearch}
|
|
@keyup=${this.handleSearch}
|
|
/>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
protected renderBody() {
|
|
return html`
|
|
<div class="flex justify-center py-3 shrink-0">
|
|
<o-button
|
|
class="no-crazygames"
|
|
variant="primary"
|
|
size="sm"
|
|
translationKey="main.store"
|
|
@click=${() => {
|
|
this.close();
|
|
window.showPage?.("page-item-store");
|
|
}}
|
|
></o-button>
|
|
</div>
|
|
<div class="px-3 pb-3">${this.renderFlags()}</div>
|
|
`;
|
|
}
|
|
|
|
private includedInSearch(country: { name: string; code: string }): boolean {
|
|
return (
|
|
country.name.toLowerCase().includes(this.search.toLowerCase()) ||
|
|
country.code.toLowerCase().includes(this.search.toLowerCase())
|
|
);
|
|
}
|
|
|
|
private handleSearch(event: Event) {
|
|
this.search = (event.target as HTMLInputElement).value;
|
|
}
|
|
|
|
private setFlag(flag: string) {
|
|
new UserSettings().setFlag(flag);
|
|
}
|
|
|
|
protected async onOpen(): Promise<void> {
|
|
[this.cosmetics, this.userMe] = await Promise.all([
|
|
fetchCosmetics(),
|
|
getUserMe().then((r) => r || (false as const)),
|
|
]);
|
|
}
|
|
|
|
protected onClose(): void {
|
|
this.search = "";
|
|
if (this.returnTo) {
|
|
const returnEl = document.querySelector(this.returnTo) as any;
|
|
if (returnEl?.open) {
|
|
returnEl.open();
|
|
}
|
|
this.returnTo = "";
|
|
}
|
|
}
|
|
}
|