summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/ActionLogMenu.svelte
blob: 61921dcef3d4c7328967e607c0263bbcc6008010 (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
82
83
<!--
	The action log (the reference `ActionLogMenu`): the last 20 command
	descriptions, newest first. Tapping an entry rolls the game back to that
	action by sending that many `undo` messages (each confirmed by the
	server with its own update).
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { t } from '$lib/i18n';
	import Menu from '../Menu.svelte';

	let {
		session,
		/** Newest last. */
		log,
		open = $bindable(true)
	}: {
		session: GameSession;
		log: string[];
		open?: boolean;
	} = $props();

	let confirming = $state<number | null>(null);

	// The number of undos back to the entry being confirmed (newest entry
	// is 0 away). Null when nothing is confirmed.
	const undosPending = $derived(
		confirming == null ? null : log.length - 1 - confirming
	);
	const confirmDesc = $derived(
		confirming == null ? '' : log[log.length - 1 - confirming]
	);

	function rollBack() {
		if (undosPending == null) return;
		confirming = null;
		open = false;
		for (let i = 0; i < undosPending; i++) session.undo();
	}
</script>

<Menu open={open} title={t('actionLogTitle')}>
	{#if log.length === 0}
		<p style="padding: 12px; text-align: center; opacity: 0.7">{t('actionLogEmpty')}</p>
	{:else}
		<p style="padding: 8px 4px 0; opacity: 0.7; font-size: 13px">{t('actionLogRollbackHint')}</p>
		<ul class="menulist">
			{#each log.slice().reverse() as desc, i (i + '::' + desc)}
				<li>
					<button
						class="menulist__item"
						disabled={i === 0}
						onclick={() => (confirming = i)}
					>
						{desc}
					</button>
				</li>
			{/each}
		</ul>
	{/if}

	{#if confirming != null}
		<div style="padding: 12px 4px">
			<div style="font-size: 15px; margin-bottom: 8px">
				{t('actionLogRollbackTitle')}
			</div>
			<div style="font-size: 13px; opacity: 0.85; margin-bottom: 12px">
				{t('actionLogRollbackBody', {
					action: confirmDesc,
					count: undosPending ?? 0
				})}
			</div>
			<div style="display: flex; gap: 8px; justify-content: flex-end">
				<button class="stepbtn" style="border-radius: 6px" onclick={() => (confirming = null)}>
					{t('close')}
				</button>
				<button class="stepbtn" style="border-radius: 6px; background: #7a1f1f" onclick={rollBack}>
					{t('actionLogRollbackConfirm')}
				</button>
			</div>
		</div>
	{/if}
</Menu>