|
4 | 4 | import TwitchPlayer from '$lib/components/players/Twitch.svelte' |
5 | 5 | import Chat from '$lib/components/Chat.svelte' |
6 | 6 | import { notify } from '$lib/components/Notification.svelte' |
| 7 | + import { Platform } from '$shared/enums' |
7 | 8 |
|
8 | 9 | let username = $state('') |
9 | 10 | let url = $state('') |
10 | 11 | let streamInfo = $state() as StreamInfo |
| 12 | + let subscribed = $state(false) |
11 | 13 |
|
12 | 14 | let loading = $state(true) |
13 | 15 |
|
|
52 | 54 | return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}` |
53 | 55 | } |
54 | 56 |
|
| 57 | + async function handleSubscription() { |
| 58 | + try { |
| 59 | + if (subscribed) { |
| 60 | + const deleted = await window.user.remove(Platform.Twitch, username) |
| 61 | + notify(`Unsubscribed from ${deleted}`) |
| 62 | + subscribed = false |
| 63 | + } else { |
| 64 | + const deleted = await window.user.add(Platform.Twitch, username) |
| 65 | + notify(`Subscribed to ${deleted}`) |
| 66 | + subscribed = true |
| 67 | + } |
| 68 | + } catch (err) { |
| 69 | + notify('Error updating subscription', err) |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + async function getPossibleUser() { |
| 74 | + const user = await window.user.get(Platform.Twitch, username) |
| 75 | + if (user) { |
| 76 | + subscribed = true |
| 77 | + } |
| 78 | + } |
| 79 | +
|
55 | 80 | onMount(async () => { |
56 | 81 | const routeURL = new URL(window.location.href) |
57 | 82 | username = routeURL.searchParams.get('username')! |
58 | 83 |
|
59 | 84 | try { |
60 | 85 | const data = await window.stream.get(username, false) |
61 | 86 | url = data |
| 87 | +
|
| 88 | + await getPossibleUser() |
62 | 89 | } catch { |
63 | 90 | notify('Stream not found') |
64 | 91 | loading = false |
|
146 | 173 | </span> |
147 | 174 | </div> |
148 | 175 |
|
| 176 | + <div class="flex"> |
| 177 | + <button |
| 178 | + class="cursor-pointer border border-white/25 p-1 px-2 hover:bg-neutral-400/50" |
| 179 | + onclick={async () => await handleSubscription()} |
| 180 | + > |
| 181 | + {subscribed ? 'SUBSCRIBED' : 'SUBSCRIBE'} |
| 182 | + </button> |
| 183 | + </div> |
| 184 | + |
149 | 185 | <div class="flex items-center gap-2"> |
150 | 186 | <img src={streamInfo.box_art} alt={streamInfo.game} width={72} height={96} /> |
151 | 187 |
|
|
0 commit comments