|
1 | 1 | <script lang="ts"> |
2 | 2 | import { onMount } from 'svelte'; |
3 | | - import { getAllGames, getAllPlayers } from '$lib/db'; |
| 3 | + import { getAllGames, getAllPlayers, exportDatabase, importDatabase, type DatabaseExport } from '$lib/db'; |
| 4 | + import { resolve } from '$app/paths'; |
4 | 5 |
|
5 | 6 | type GameWithDetails = Awaited<ReturnType<typeof getAllGames>>[number]; |
6 | 7 | type Player = Awaited<ReturnType<typeof getAllPlayers>>[number]; |
7 | 8 |
|
8 | 9 | let games = $state<GameWithDetails[]>([]); |
9 | 10 | let players = $state<Player[]>([]); |
10 | 11 | let loading = $state(true); |
| 12 | + let importMessage = $state<{ type: 'success' | 'error'; text: string } | null>(null); |
11 | 13 |
|
12 | 14 | function getMapClass(map: string): string { |
13 | 15 | const mapName = map.toLowerCase(); |
|
18 | 20 | return 'map-autumn'; |
19 | 21 | } |
20 | 22 |
|
| 23 | + async function handleExport() { |
| 24 | + try { |
| 25 | + const data = await exportDatabase(); |
| 26 | + const json = JSON.stringify(data, null, 2); |
| 27 | + const blob = new Blob([json], { type: 'application/json' }); |
| 28 | + const url = URL.createObjectURL(blob); |
| 29 | + const a = document.createElement('a'); |
| 30 | + a.href = url; |
| 31 | + a.download = `root-games-${new Date().toISOString().split('T')[0]}.json`; |
| 32 | + a.click(); |
| 33 | + URL.revokeObjectURL(url); |
| 34 | + } catch (error) { |
| 35 | + alert('Failed to export data'); |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | + async function handleImport(event: Event) { |
| 40 | + const input = event.target as HTMLInputElement; |
| 41 | + const file = input.files?.[0]; |
| 42 | + if (!file) return; |
| 43 | +
|
| 44 | + try { |
| 45 | + const text = await file.text(); |
| 46 | + const data = JSON.parse(text) as DatabaseExport; |
| 47 | + |
| 48 | + if (!data.version || !data.games || !data.players) { |
| 49 | + throw new Error('Invalid file format'); |
| 50 | + } |
| 51 | +
|
| 52 | + const merge = confirm('Do you want to merge with existing data?\n\nClick OK to merge (keep existing + add new)\nClick Cancel to replace all data'); |
| 53 | + |
| 54 | + const result = await importDatabase(data, merge); |
| 55 | + |
| 56 | + // Refresh data |
| 57 | + games = await getAllGames(); |
| 58 | + players = await getAllPlayers(); |
| 59 | + |
| 60 | + importMessage = { |
| 61 | + type: 'success', |
| 62 | + text: `Imported ${result.imported} records${result.skipped > 0 ? `, skipped ${result.skipped} duplicates` : ''}` |
| 63 | + }; |
| 64 | + |
| 65 | + setTimeout(() => importMessage = null, 5000); |
| 66 | + } catch (error) { |
| 67 | + importMessage = { |
| 68 | + type: 'error', |
| 69 | + text: 'Failed to import: Invalid file format' |
| 70 | + }; |
| 71 | + setTimeout(() => importMessage = null, 5000); |
| 72 | + } |
| 73 | + |
| 74 | + // Reset input |
| 75 | + input.value = ''; |
| 76 | + } |
| 77 | +
|
21 | 78 | onMount(async () => { |
22 | 79 | games = await getAllGames(); |
23 | 80 | players = await getAllPlayers(); |
|
38 | 95 |
|
39 | 96 | <div class="mb-8 flex justify-center gap-4"> |
40 | 97 | <a |
41 | | - href="/games/new" |
| 98 | + href="{resolve("/games/new")}" |
42 | 99 | class="rounded-lg bg-amber-600 px-6 py-3 font-semibold text-white shadow-md transition hover:bg-amber-700" |
43 | 100 | > |
44 | 101 | + New Game |
45 | 102 | </a> |
46 | 103 | <a |
47 | | - href="/stats" |
| 104 | + href="{resolve("/stats")}" |
48 | 105 | class="rounded-lg bg-amber-100 px-6 py-3 font-semibold text-amber-800 shadow-md transition hover:bg-amber-200" |
49 | 106 | > |
50 | 107 | 📊 Stats |
|
66 | 123 | {:else} |
67 | 124 | <div class="space-y-4"> |
68 | 125 | {#each games as game} |
69 | | - <a href="/games/{game.id}" class="block"> |
| 126 | + <a href="{resolve(`/games/${game.id}`)}" class="block"> |
70 | 127 | <div class="map-card-bg {getMapClass(game.map)} rounded-lg bg-white p-6 shadow transition hover:shadow-lg"> |
71 | 128 | <div class="mb-4 flex items-center justify-between"> |
72 | 129 | <span class="font-medium text-amber-800">{game.map}</span> |
|
129 | 186 | {/if} |
130 | 187 | </div> |
131 | 188 | </section> |
| 189 | + |
| 190 | + <section class="mt-8"> |
| 191 | + <h2 class="mb-4 text-2xl font-semibold text-amber-800">Data Management</h2> |
| 192 | + <div class="rounded-lg bg-white p-6 shadow"> |
| 193 | + {#if importMessage} |
| 194 | + <div class="mb-4 rounded-lg p-3 {importMessage.type === 'success' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}"> |
| 195 | + {importMessage.text} |
| 196 | + </div> |
| 197 | + {/if} |
| 198 | + <div class="flex flex-wrap gap-4"> |
| 199 | + <button |
| 200 | + onclick={handleExport} |
| 201 | + class="rounded-lg bg-blue-600 px-4 py-2 font-medium text-white transition hover:bg-blue-700" |
| 202 | + > |
| 203 | + 📤 Export Data |
| 204 | + </button> |
| 205 | + <label class="cursor-pointer rounded-lg bg-green-600 px-4 py-2 font-medium text-white transition hover:bg-green-700"> |
| 206 | + 📥 Import Data |
| 207 | + <input |
| 208 | + type="file" |
| 209 | + accept=".json" |
| 210 | + onchange={handleImport} |
| 211 | + class="hidden" |
| 212 | + /> |
| 213 | + </label> |
| 214 | + </div> |
| 215 | + <p class="mt-3 text-sm text-gray-500"> |
| 216 | + Export your game data as JSON to backup or transfer to another device. |
| 217 | + </p> |
| 218 | + </div> |
| 219 | + </section> |
132 | 220 | {/if} |
133 | 221 | </div> |
134 | 222 | </div> |
0 commit comments