import { html, LitElement } from "lit"; import { customElement, property } from "lit/decorators.js"; import { EventBus } from "../../../core/EventBus"; import { PlayerType } from "../../../core/game/Game"; import { PlayerView } from "../../../core/game/GameView"; import { actionButton } from "../../components/ui/ActionButton"; import { SendKickPlayerIntentEvent } from "../../Transport"; import { translateText } from "../../Utils"; import kickIcon from "/images/ExitIconWhite.svg?url"; import shieldIcon from "/images/ShieldIconWhite.svg?url"; @customElement("player-moderation-modal") export class PlayerModerationModal extends LitElement { @property({ attribute: false }) eventBus: EventBus | null = null; @property({ attribute: false }) myPlayer: PlayerView | null = null; @property({ attribute: false }) target: PlayerView | null = null; @property({ type: Boolean }) open: boolean = false; @property({ type: Boolean }) alreadyKicked: boolean = false; createRenderRoot() { return this; } updated(changed: Map) { if (changed.has("open") && this.open) { queueMicrotask(() => (this.querySelector('[role="dialog"]') as HTMLElement | null)?.focus(), ); } } private closeModal() { this.dispatchEvent(new CustomEvent("close")); } private handleKeydown = (e: KeyboardEvent) => { if (e.key === "Escape") { e.preventDefault(); this.closeModal(); } }; private canKick(my: PlayerView, other: PlayerView): boolean { return ( my.isLobbyCreator() && other !== my && other.type() === PlayerType.Human && !!other.clientID() ); } private handleKickClick = (e: MouseEvent) => { e.stopPropagation(); const my = this.myPlayer; const other = this.target; const eventBus = this.eventBus; if (!my || !other) return; if (!this.canKick(my, other) || this.alreadyKicked) return; if (!eventBus) return; const targetClientID = other.clientID(); if (!targetClientID || targetClientID.length === 0) return; const confirmed = confirm( translateText("player_panel.kick_confirm", { name: other.displayName() }), ); if (!confirmed) return; eventBus.emit(new SendKickPlayerIntentEvent(targetClientID)); this.dispatchEvent( new CustomEvent("kicked", { detail: { playerId: String(other.id()) } }), ); this.closeModal(); }; render() { if (!this.open) return html``; const my = this.myPlayer; const other = this.target; if (!my || !other) return html``; const canKick = this.canKick(my, other); const alreadyKicked = this.alreadyKicked; const moderationTitle = translateText("player_panel.moderation"); const kickTitle = alreadyKicked ? translateText("player_panel.kicked") : translateText("player_panel.kick"); return html`
this.closeModal()} >
`; } }