aboutsummaryrefslogtreecommitdiff
path: root/web/src/main.svelte
blob: baf02c9b4852f5da896686562fb99d52cdfc5f25 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<script lang="ts">
	import { onDestroy, onMount } from 'svelte';
	import addSVG from './assets/add.svg';
	import playSVG from './assets/play.svg';
	import pauseSVG from './assets/pause.svg';
	import type { WsMessage, Song, SearchResponse } from './lib/api';
	import API from './lib/api';
	API.baseUrl = '/';

	let status: 'paused' | 'playing' = 'paused';
	let currentSong: Song | null = null;
	let playlist: Array<Song> = [];
	let votes: Map<string, 'up' | 'down'> = new Map();

	let search = '';
	let searchResults: Array<Song> = [];

	let socket: WebSocket;
	onMount(function () {
		const proto = document.location.protocol === "https:" ? "wss:": "ws:";
		const port = document.location.port;
		const host = document.location.hostname;

		socket = new WebSocket(`${proto}//${host}:${port}/ws`);
		// Expose websocket for debugging
		// ts-ignore
		window.WS = socket;
		console.info('Attempting Connection...');

		socket.onopen = () => {
			console.info('Successfully Connected');
		};

		socket.onclose = (event) => {
			console.info('Socket Closed Connection: ', event);
		};

		socket.onerror = (error) => {
			console.error('Socket Error: ', error);
		};

		socket.onmessage = ({ data }) => {
			const { cmd, songs }: WsMessage = JSON.parse(data);
			console.debug('Ws Message', cmd, songs);
			switch (cmd) {
				case 'add':
					playlist = [...playlist, ...songs];
					break;
				case 'update':
					for (const song of songs) {
						playlist[
							playlist.findIndex(({ uri }) => {
								return uri === song.uri;
							})
						] = song;
					}
					break;
				case 'pause':
					status = 'paused';
					break;
				case 'play':
					status = 'playing';
					currentSong = songs[0];
					playlist = playlist.filter(({ uri }) => uri !== currentSong?.uri);
					break;
				case 'upvoted':
					for (const { uri } of songs) {
						votes.set(uri, 'up');
					}
					votes = votes;
					break;
				case 'downvoted':
					for (const { uri } of songs) {
						votes.set(uri, 'down');
					}
					votes = votes;
					break;
			}
		};
	});
	onDestroy(function () {
		socket?.close();
	});

	$: playlist = playlist.sort(function (a, b) {
		return b.weight - a.weight;
	});

	async function submitSearch() {
		const response = await API.get('search', { pattern: search });
		const { songs }: SearchResponse = await response.json();
		searchResults = songs;
	}

	function vote(direction: 'up' | 'down', uri: string) {
		return async function () {
			let action;
			if (votes.get(uri) === direction) {
				votes.delete(uri);
				action = 'unvote';
			} else {
				votes.set(uri, direction);
				action = direction;
			}
			const response = await API.get(action, { song: uri });
			console.debug(response);
		};
	}
</script>

<div class="content">
	<form on:submit|preventDefault={submitSearch}>
		<h1>Add your song</h1>
		<label><input bind:value={search} id="search-bar" placeholder="Title/Artist/Album/..." /></label
		>
		<button formaction="submit">Search</button>
	</form>
	<div class="list">
		{#each searchResults as { title, artist, source, uri, weight } (uri)}
			<div class="flex-row search-results">
				<button
					class="icon-button"
					on:click={() =>
						API.post('add', { title, artist, source, uri, weight }).then(console.debug)}
				>
					<img src={addSVG} alt="Add" />
				</button>
				{artist + ' - ' + title}
				<small>{source}</small>
			</div>
		{/each}
	</div>
	<h1>Playlist</h1>
	<div class="list">
		{#each playlist as { title, artist, source, uri, weight } (uri)}
			<div>
				<button class="vote" on:click={vote('up', uri)}>Up</button>
				<button class="vote" on:click={vote('down', uri)}>Down</button>
				{`${weight} ${artist} - ${title}`}
				<small>{`(${source})`}</small>
			</div>
		{/each}
	</div>
</div>

<div class="player flex-row">
	<button class="icon-button" on:click={() => API.get('playpause').then(console.debug)}>
		{#if status === 'playing'}
			<img src={pauseSVG} alt="Pause" />
		{:else}
			<img src={playSVG} alt="Play" />
		{/if}
	</button>
	<p>
		{#if currentSong !== null}
			{currentSong.title} <br /> <small>{currentSong.artist}</small>
		{:else}
			No songs schedulded :( <br /> <a href="#search-bar">Try adding some.</a>
		{/if}
	</p>
</div>

<style>
	.content {
		width: 100vw;
		height: calc(100vh - 5em);
	}

	@media (max-width: 50em) {
		.content {
			overflow-y: scroll;
			display: flex;
			flex-direction: column;
			align-items: center;
		}
	}
	@media not (max-width: 50em) {
		.content {
			display: grid;
			grid-template-columns: 1fr 1fr;
			grid-template-rows: min-content 1fr;
		}
		.content > * {
			grid-row: 1;
		}
		.content > form {
			grid-column: 1;
		}
		.content > h1 {
			grid-column: 2;
		}

		.content > .list {
			grid-row: 2;
		}
		.content > div.list:first-child {
			grid-column: 1;
		}
		.content > div.list:last-child {
			grid-column: 2;
		}

		.list {
			overflow-y: scroll;
		}
	}

	.player {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 5em;
		border-top: 1px solid white;
	}

	.search-results > button {
		--icon-size: 2em;
	}
	.search-results > small {
		color: var(--secondary);
		margin-left: 0.5em;
	}
</style>