Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@
"clan_disbanded": "Clan disbanded.",
"clan_name": "Clan Name",
"clan_settings": "Clan Settings",
"clan_shop": "Clan Shop",
"confirm_ban": "Are you sure you want to ban this player? They will be removed from the clan and unable to rejoin.",
"confirm_disband": "Are you sure you want to disband [{tag}] {name}? This cannot be undone.",
"confirm_kick": "Are you sure you want to kick this member?",
Expand Down
1 change: 1 addition & 0 deletions src/client/ClanModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ export class ClanModal extends BaseModal {
},
];
}}
@close-clan-modal=${() => this.close()}
></clan-detail-view>`;
}

Expand Down
62 changes: 60 additions & 2 deletions src/client/components/clan/ClanDetailView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { invalidateUserMe } from "../../Api";
import { getUserMe, invalidateUserMe } from "../../Api";
import {
type ClanDiscord,
type ClanInfo,
Expand All @@ -13,7 +13,11 @@ import {
joinClan,
leaveClan,
} from "../../ClanApi";
import { broadcastFreshUserMe } from "../../Cosmetics";
import {
broadcastFreshUserMe,
fetchCosmetics,
refreshStaleCosmetics,
} from "../../Cosmetics";
import { translateText } from "../../Utils";
import "../ConfirmDialog";
import "../CopyButton";
Expand All @@ -23,6 +27,7 @@ import {
type ClanRole,
defaultOrderForSort,
filterMembersBySearch,
findClanAffiliateCode,
formatBoostRemaining,
membersHaveLevels,
renderLoadingSpinner,
Expand Down Expand Up @@ -61,6 +66,7 @@ export class ClanDetailView extends LitElement {
@property({ type: Object }) cachedClan: ClanInfo | null = null;
@state() private selectedClan: ClanInfo | null = null;
@state() private discordMeta: ClanDiscord | null = null;
@state() private affiliateCode: string | null = null;
@state() private myRole: ClanRole | null = null;
@state() private members: ClanMember[] = [];
@state() private membersTotal = 0;
Expand All @@ -78,13 +84,18 @@ export class ClanDetailView extends LitElement {
@state() private boostOpen = false;
private memberSearchDebounce: ReturnType<typeof setTimeout> | null = null;
private memberLoadSeq = 0;
private affiliateLoadSeq = 0;
private asyncGeneration = 0;

// Re-renders once a minute so the boost's "time left" counts down.
private boostTicker: ReturnType<typeof setInterval> | null = null;
private onUserMeEvent = () => {
void this.loadAffiliateCode(this.clanTag, this.asyncGeneration);
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

connectedCallback() {
super.connectedCallback();
document.addEventListener("userMeResponse", this.onUserMeEvent);
this.boostTicker = setInterval(() => {
if (this.selectedClan?.boostEndsAt) this.requestUpdate();
}, 60_000);
Expand All @@ -111,6 +122,8 @@ export class ClanDetailView extends LitElement {
this.asyncGeneration,
);
}
this.affiliateCode = null;
void this.loadAffiliateCode(this.clanTag, this.asyncGeneration);
}

// Fetches live Discord invite metadata (server name, icon, counts) for the
Expand All @@ -122,11 +135,41 @@ export class ClanDetailView extends LitElement {
this.discordMeta = meta;
}

// Resolves the catalog's affiliateCode matching this clan's tag (if any) so
// the Overview actions only offer the Clan Shop button when items exist and
// are purchasable by the viewer. Floating; guarded by affiliateLoadSeq,
// asyncGeneration + tag so stale in-flight lookups cannot overwrite the current view.
private async loadAffiliateCode(tag: string, gen: number) {
const seq = ++this.affiliateLoadSeq;
refreshStaleCosmetics();
const [cosmetics, userMe] = await Promise.all([
fetchCosmetics(),
getUserMe().catch(() => false as const),
]);
if (
seq !== this.affiliateLoadSeq ||
gen !== this.asyncGeneration ||
this.clanTag !== tag
) {
return;
}
this.affiliateCode = findClanAffiliateCode(cosmetics, tag, userMe);
}

private openClanShop(affiliateCode: string) {
this.dispatchEvent(
new CustomEvent("close-clan-modal", { bubbles: true, composed: true }),
);
window.location.hash = `modal=store&affiliateCode=${encodeURIComponent(affiliateCode)}`;
Comment thread
LoSt543215543 marked this conversation as resolved.
Resolved
}

disconnectedCallback() {
document.removeEventListener("userMeResponse", this.onUserMeEvent);
if (this.boostTicker) clearInterval(this.boostTicker);
this.boostTicker = null;
if (this.memberSearchDebounce) clearTimeout(this.memberSearchDebounce);
this.memberLoadSeq++;
this.affiliateLoadSeq++;
super.disconnectedCallback();
}

Expand All @@ -141,6 +184,7 @@ export class ClanDetailView extends LitElement {
private async loadDetail() {
const gen = ++this.asyncGeneration;
this.memberLoadSeq++;
this.affiliateLoadSeq++;
this.loading = true;
this.myRole = null;
this.pendingRequestCount = 0;
Expand Down Expand Up @@ -177,6 +221,8 @@ export class ClanDetailView extends LitElement {
if (detail.discordUrl) {
void this.loadDiscordMeta(detail.discordUrl, this.clanTag, gen);
}
this.affiliateCode = null;
void this.loadAffiliateCode(this.clanTag, gen);
this.memberPage = 1;
if (!goingToMembers) {
// Members tab will populate these via loadInitialMembers; the
Expand Down Expand Up @@ -950,6 +996,18 @@ export class ClanDetailView extends LitElement {
</button>
`);
}
if (this.affiliateCode) {
const affiliateCode = this.affiliateCode;
buttons.push(html`
<button
data-action="clan-shop"
@click=${() => this.openClanShop(affiliateCode)}
class="no-game-portal w-full px-4 py-3 text-sm font-bold text-white uppercase tracking-wider bg-white/10 hover:bg-white/15 rounded-xl transition-all border border-white/10"
>
${translateText("clan_modal.clan_shop")}
</button>
`);
}
if (isMember && !isLeader) {
buttons.push(html`
<button
Expand Down
50 changes: 50 additions & 0 deletions src/client/components/clan/ClanShared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { UserMeResponse } from "@openfront/shared/ApiSchemas";
import type { Cosmetics } from "@openfront/shared/CosmeticSchemas";
import { html, type TemplateResult } from "lit";
import type {
ClanJoinRequest,
Expand All @@ -6,6 +8,7 @@ import type {
ClanMemberSort,
ClanMemberStats,
} from "../../ClanApi";
import { resolveCosmetics } from "../../Cosmetics";
import { showToast, translateText } from "../../Utils";
import { hasLevel, levelBadgeSlot } from "../LevelBadgeSlot";
import { playerNameLink } from "../ui/PlayerNameLink";
Expand Down Expand Up @@ -553,3 +556,50 @@ export function formatBoostRemaining(
minutes: totalMinutes % 60,
});
}

// Resolves the catalog's canonical `affiliateCode` matching `clanTag`
// case-insensitively, or null when no cosmetic in the catalog uses that code
// or when none of the clan's affiliate items are currently purchasable
// (e.g. all off-market or all owned by the viewer).
export function findClanAffiliateCode(
cosmetics: Cosmetics | null | undefined,
clanTag: string,
userMeResponse: UserMeResponse | false = false,
): string | null {
if (!cosmetics) return null;
const trimmed = clanTag.trim();
if (!trimmed) return null;
const lower = trimmed.toLowerCase();
const groups: Record<string, { affiliateCode?: string | null }>[] = [
cosmetics.patterns ?? {},
cosmetics.flags ?? {},
cosmetics.crowns ?? {},
cosmetics.skins ?? {},
...Object.values(cosmetics.effects ?? {}),
];
const candidateCodes = new Set<string>();
for (const group of groups) {
for (const item of Object.values(group)) {
if (
typeof item.affiliateCode === "string" &&
item.affiliateCode.toLowerCase() === lower
) {
candidateCodes.add(item.affiliateCode);
}
}
}
for (const code of candidateCodes) {
const resolved = resolveCosmetics(cosmetics, userMeResponse, code);
const hasPurchasable = resolved.some(
(r) =>
r.relationship === "purchasable" &&
r.cosmetic !== null &&
"affiliateCode" in r.cosmetic &&
r.cosmetic.affiliateCode === code,
);
if (hasPurchasable) {
return code;
}
}
return null;
}
7 changes: 6 additions & 1 deletion tests/client/ClanGameStatsNavigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import {
vi,
} from "vitest";

import { apiMockFactory, clanApiMockFactory } from "./clan/ClanModalTestUtils";
import {
apiMockFactory,
clanApiMockFactory,
cosmeticsMockFactory,
} from "./clan/ClanModalTestUtils";

vi.mock("../../src/client/Api", () => apiMockFactory());
vi.mock("../../src/client/ClanApi", () => clanApiMockFactory());
vi.mock("../../src/client/Cosmetics", () => cosmeticsMockFactory());

vi.mock("../../src/client/ClientEnv", () => ({
ClientEnv: { workerPath: vi.fn(() => "w0") },
Expand Down
2 changes: 2 additions & 0 deletions tests/client/clan/ClanMemberLevels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import type { ClanInfo, ClanMember } from "../../../src/client/ClanApi";
import {
apiMockFactory,
clanApiMockFactory,
cosmeticsMockFactory,
utilsMockFactory,
} from "./ClanModalTestUtils";

vi.mock("../../../src/client/Api", () => apiMockFactory());
vi.mock("../../../src/client/ClanApi", () => clanApiMockFactory());
vi.mock("../../../src/client/Cosmetics", () => cosmeticsMockFactory());
vi.mock("../../../src/client/Utils", () => utilsMockFactory());

import { ClanDetailView } from "../../../src/client/components/clan/ClanDetailView";
Expand Down
2 changes: 2 additions & 0 deletions tests/client/clan/ClanMemberSearchPagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import type {
import {
apiMockFactory,
clanApiMockFactory,
cosmeticsMockFactory,
setElState,
utilsMockFactory,
} from "./ClanModalTestUtils";

vi.mock("../../../src/client/Api", () => apiMockFactory());
vi.mock("../../../src/client/ClanApi", () => clanApiMockFactory());
vi.mock("../../../src/client/Cosmetics", () => cosmeticsMockFactory());
vi.mock("../../../src/client/Utils", () => utilsMockFactory());

import { ClanDetailView } from "../../../src/client/components/clan/ClanDetailView";
Expand Down
2 changes: 2 additions & 0 deletions tests/client/clan/ClanModal.handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
apiMockFactory,
authMockFactory,
clanApiMockFactory,
cosmeticsMockFactory,
crazyGamesSdkMockFactory,
flushAsync,
getElState,
Expand All @@ -19,6 +20,7 @@ import {
vi.mock("@lit-labs/virtualizer/virtualize.js", () => virtualizerMockFactory());
vi.mock("../../../src/client/Api", () => apiMockFactory());
vi.mock("../../../src/client/ClanApi", () => clanApiMockFactory());
vi.mock("../../../src/client/Cosmetics", () => cosmeticsMockFactory());
vi.mock("../../../src/client/Utils", () => utilsMockFactory());
vi.mock("../../../src/client/Auth", () => authMockFactory());
vi.mock("../../../src/client/CrazyGamesSDK", () => crazyGamesSdkMockFactory());
Expand Down
Loading
Loading