summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/RemoveMonsterMenu.svelte
blob: b0df4ef4f34d7b202275358f7d9a630357d41f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!--
	Remove Monsters (the reference `RemoveMonsterMenu`): checkbox list of the
	current monsters; "Remove All" clears everything.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { isMonster } from '$lib/protocol/types';
	import { t } from '$lib/i18n';
	import Menu from '../Menu.svelte';

	let {
		session,
		open = $bindable(true)
	}: {
		session: GameSession;
		open?: boolean;
	} = $props();

	const gameState = $derived(session.state);
	const player = $derived(session.player);

	let selected = $state<Set<string>>(new Set());

	$effect(() => {
		if (!open) return;
		const ids = ($gameState?.currentList ?? []).filter(isMonster).map((m) => m.id);
		selected = new Set(ids);
	});

	const monsters = $derived(($gameState?.currentList ?? []).filter(isMonster));

	function toggle(id: string) {
		const next = new Set(selected);
		if (next.has(id)) next.delete(id);
		else next.add(id);
		selected = next;
	}

	function remove() {
		if (!$player || selected.size === 0) return;
		session.command('removeMonster', { ids: [...selected] });
		open = false;
	}

	function removeAll() {
		if (!$player || monsters.length === 0) return;
		session.command('removeMonster', { ids: monsters.map((m) => m.id) });
		open = false;
	}
</script>

<Menu open={open} title={t('menuRemoveMonsters')}>
	{#if monsters.length === 0}
		<p style="padding: 12px; opacity: 0.7">{t('noResultsFound')}</p>
	{:else}
		<ul class="menulist">
			{#each monsters as m (m.id)}
				<li>
					<label class="menulist__item" style="cursor: pointer">
						<input type="checkbox" checked={selected.has(m.id)} onchange={() => toggle(m.id)} />
						{m.id} ({m.monsterInstances.length})
					</label>
				</li>
			{/each}
		</ul>

		<div style="display: flex; gap: 8px; justify-content: flex-end; padding: 8px 4px">
			<button class="stepbtn" style="border-radius: 6px" disabled={!$player} onclick={removeAll}>
				{t('removeAll')}
			</button>
			<button
				class="stepbtn"
				style="border-radius: 6px; background: #7a1f1f"
				disabled={!$player || selected.size === 0}
				onclick={remove}
			>
				{t('menuRemoveMonsters')} ({selected.size})
			</button>
		</div>
	{/if}
</Menu>