|
| 1 | +<script lang="ts"> |
| 2 | + let { id } = $props(); |
| 3 | +
|
| 4 | + // Components |
| 5 | + import { Button } from '$lib/components/ui/button/index.js'; |
| 6 | + import { toast } from 'svelte-sonner'; |
| 7 | +
|
| 8 | + // Icons |
| 9 | + import Refresh from 'lucide-svelte/icons/refresh-cw'; |
| 10 | + import Fullscreen from 'lucide-svelte/icons/maximize'; |
| 11 | + import OpenInNewTab from 'lucide-svelte/icons/external-link'; |
| 12 | + import Share from 'lucide-svelte/icons/share'; |
| 13 | + import Bookmark from 'lucide-svelte/icons/bookmark'; |
| 14 | +
|
| 15 | + // App imports |
| 16 | + import { getGameById } from '$lib/gmaes'; |
| 17 | + import { preferencesStore, favoritesStore, historyStore } from '$lib/stores'; |
| 18 | + import { onMount } from 'svelte'; |
| 19 | +
|
| 20 | + function openNewTab(url: string) { |
| 21 | + url = location.origin + url; |
| 22 | + var openedTab; |
| 23 | + if ($preferencesStore.open === 'tab') { |
| 24 | + openedTab = window.open('', '_blank'); |
| 25 | + } else if ($preferencesStore.open === 'window') { |
| 26 | + openedTab = window.open('', '_blank'); |
| 27 | + } else { |
| 28 | + $preferencesStore.open = 'tab'; |
| 29 | + openNewTab(url); |
| 30 | + return; |
| 31 | + } |
| 32 | + if (!openedTab) return; |
| 33 | + const newDocument = openedTab.document; |
| 34 | + const style = newDocument.createElement('style'); |
| 35 | + style.textContent = 'body, html { margin: 0; padding: 0; height: 100%; }'; |
| 36 | + newDocument.head.appendChild(style); |
| 37 | + const iframe = newDocument.createElement('iframe'); |
| 38 | + iframe.src = url; |
| 39 | + iframe.style.cssText = 'width: 100%; height: 100%; border: none;'; |
| 40 | + newDocument.body.appendChild(iframe); |
| 41 | + } |
| 42 | +
|
| 43 | + const gmaedata = $derived(getGameById(id)); |
| 44 | +
|
| 45 | + onMount(() => { |
| 46 | + let history = $historyStore; |
| 47 | + if (history.includes(id)) { |
| 48 | + history = history.filter((historyId) => historyId !== id); |
| 49 | + } |
| 50 | +
|
| 51 | + history.push(id); |
| 52 | +
|
| 53 | + historyStore.set(history); |
| 54 | + }); |
| 55 | +</script> |
| 56 | + |
| 57 | +<div class="flex h-full w-full flex-row gap-3 p-3"> |
| 58 | + <iframe src={gmaedata?.url} frameborder="0" class="flex-grow rounded" title={gmaedata?.name} |
| 59 | + ></iframe> |
| 60 | + |
| 61 | + <div class="flex h-full w-72 flex-col"> |
| 62 | + <h1 class="text-4xl font-bold">{gmaedata?.name}</h1> |
| 63 | + <p class="text-xl">{gmaedata?.description}</p> |
| 64 | + <div class="flex-grow"></div> |
| 65 | + <div class="flex flex-col gap-3"> |
| 66 | + <Button |
| 67 | + variant="outline" |
| 68 | + onclick={() => { |
| 69 | + const iframe = document.querySelector('iframe'); |
| 70 | + if (iframe && gmaedata?.url) { |
| 71 | + iframe.src = gmaedata.url; |
| 72 | + } |
| 73 | + }} |
| 74 | + > |
| 75 | + <Refresh class="h-6 w-6" /> |
| 76 | + Reload |
| 77 | + </Button> |
| 78 | + <Button |
| 79 | + variant="outline" |
| 80 | + onclick={() => { |
| 81 | + document.querySelector('iframe')?.requestFullscreen(); |
| 82 | + }} |
| 83 | + > |
| 84 | + <Fullscreen class="h-6 w-6" /> |
| 85 | + Fullscreen |
| 86 | + </Button> |
| 87 | + <Button |
| 88 | + variant="outline" |
| 89 | + onclick={() => { |
| 90 | + if (gmaedata?.url) openNewTab(gmaedata.url); |
| 91 | + }} |
| 92 | + > |
| 93 | + <OpenInNewTab class="h-6 w-6" /> |
| 94 | + New tab |
| 95 | + </Button> |
| 96 | + <Button |
| 97 | + variant="outline" |
| 98 | + onclick={() => { |
| 99 | + if (navigator.share) { |
| 100 | + navigator |
| 101 | + .share({ |
| 102 | + text: window.location.href |
| 103 | + }) |
| 104 | + .catch(() => { |
| 105 | + navigator.clipboard.writeText(window.location.href); |
| 106 | + toast.error('Failed to share link, copied to clipboard instead.'); |
| 107 | + }); |
| 108 | + } else { |
| 109 | + navigator.clipboard.writeText(window.location.href); |
| 110 | + toast.error('Failed to share link, copied to clipboard instead.'); |
| 111 | + } |
| 112 | + }} |
| 113 | + > |
| 114 | + <Share class="h-6 w-6" /> |
| 115 | + Share |
| 116 | + </Button> |
| 117 | + <Button |
| 118 | + variant="outline" |
| 119 | + onclick={() => { |
| 120 | + if (gmaedata?.id) { |
| 121 | + if ($favoritesStore.includes(gmaedata.id)) { |
| 122 | + $favoritesStore = $favoritesStore.filter((id) => id !== gmaedata.id); |
| 123 | + } else { |
| 124 | + $favoritesStore = [...$favoritesStore, gmaedata.id]; |
| 125 | + } |
| 126 | + } |
| 127 | + }} |
| 128 | + > |
| 129 | + {#if gmaedata?.id && $favoritesStore.includes(gmaedata.id)} |
| 130 | + <Bookmark class="h-6 w-6 text-red-400" /> |
| 131 | + Saved |
| 132 | + {:else} |
| 133 | + <Bookmark class="h-6 w-6" /> |
| 134 | + Save |
| 135 | + {/if} |
| 136 | + </Button> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | +</div> |
0 commit comments