diff --git a/resources/lang/en.json b/resources/lang/en.json index df82c48a29e..7e8107611d6 100644 --- a/resources/lang/en.json +++ b/resources/lang/en.json @@ -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?", diff --git a/src/client/ClanModal.ts b/src/client/ClanModal.ts index ba5b0c1fc99..36030f9cd96 100644 --- a/src/client/ClanModal.ts +++ b/src/client/ClanModal.ts @@ -595,6 +595,7 @@ export class ClanModal extends BaseModal { }, ]; }} + @close-clan-modal=${() => this.close()} >`; } diff --git a/src/client/components/clan/ClanDetailView.ts b/src/client/components/clan/ClanDetailView.ts index 23520c545cf..dd2bcf7d467 100644 --- a/src/client/components/clan/ClanDetailView.ts +++ b/src/client/components/clan/ClanDetailView.ts @@ -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, @@ -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"; @@ -23,6 +27,7 @@ import { type ClanRole, defaultOrderForSort, filterMembersBySearch, + findClanAffiliateCode, formatBoostRemaining, membersHaveLevels, renderLoadingSpinner, @@ -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; @@ -78,13 +84,18 @@ export class ClanDetailView extends LitElement { @state() private boostOpen = false; private memberSearchDebounce: ReturnType | 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 | null = null; + private onUserMeEvent = () => { + void this.loadAffiliateCode(this.clanTag, this.asyncGeneration); + }; connectedCallback() { super.connectedCallback(); + document.addEventListener("userMeResponse", this.onUserMeEvent); this.boostTicker = setInterval(() => { if (this.selectedClan?.boostEndsAt) this.requestUpdate(); }, 60_000); @@ -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 @@ -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)}`; + } + 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(); } @@ -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; @@ -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 @@ -950,6 +996,18 @@ export class ClanDetailView extends LitElement { `); } + if (this.affiliateCode) { + const affiliateCode = this.affiliateCode; + buttons.push(html` + + `); + } if (isMember && !isLeader) { buttons.push(html`