|
6 | 6 | import ViewButton from '$lib/view-button.svelte'; |
7 | 7 | import { DynamicHead, Error } from '@gleich/ui'; |
8 | 8 | import SpotifyIcon from '$lib/icons/spotify-icon.svelte'; |
9 | | - import { loadPlaylistFromLCP } from '$lib/lcp/applemusic'; |
| 9 | + import { loadPlaylistFromLCP, type AppleMusicSong } from '$lib/lcp/applemusic'; |
10 | 10 | import type { PlaylistData } from './+page.server'; |
11 | 11 | import { onMount } from 'svelte'; |
| 12 | + import PageLoading from '$lib/loading/page-loading.svelte'; |
| 13 | + import { page } from '$app/state'; |
12 | 14 |
|
13 | 15 | const { data }: { data: PlaylistData } = $props(); |
14 | 16 |
|
15 | | - let tracks = $derived(data.response?.playlist?.tracks ?? []); |
| 17 | + let tracks: AppleMusicSong[] | undefined = $state(); |
16 | 18 | let currentPage = $state(1); |
17 | 19 | let loading = $state(false); |
18 | | - let hasMore = $derived( |
19 | | - data.response ? tracks.length < data.response.playlist.track_count : false |
20 | | - ); |
| 20 | + let hasMore = $state(false); |
21 | 21 |
|
22 | 22 | onMount(() => { |
23 | | - tracks = data.response?.playlist.tracks ?? []; |
24 | | - currentPage = 1; |
25 | | - loading = false; |
26 | | - hasMore = data.response ? tracks.length < data.response?.playlist.track_count : false; |
| 23 | + data.response.then((resp) => { |
| 24 | + tracks = resp?.playlist.tracks ?? []; |
| 25 | + currentPage = 1; |
| 26 | + loading = false; |
| 27 | + hasMore = resp?.pagination.next != null; |
| 28 | + }); |
27 | 29 | }); |
28 | 30 |
|
29 | 31 | async function loadNextPage() { |
30 | | - if (!data || !data.response || loading || !hasMore) return; |
| 32 | + if (!data || !data.response || loading || !hasMore || !page.params.id) return; |
31 | 33 |
|
32 | 34 | loading = true; |
33 | 35 | try { |
34 | 36 | const nextPage = currentPage + 1; |
35 | | - const next = await loadPlaylistFromLCP(data.response?.playlist.id, nextPage, fetch); |
| 37 | + const next = await loadPlaylistFromLCP(page.params.id, nextPage, fetch); |
36 | 38 | const nextTracks = next?.playlist?.tracks ?? []; |
37 | 39 |
|
38 | | - if (nextTracks.length > 0) { |
| 40 | + if (nextTracks.length > 0 && tracks) { |
39 | 41 | tracks = [...tracks, ...nextTracks]; |
40 | 42 | currentPage = nextPage; |
41 | 43 | } |
|
45 | 47 | return; |
46 | 48 | } |
47 | 49 |
|
48 | | - hasMore = tracks.length < data.response.playlist.track_count; |
| 50 | + hasMore = next.pagination.next != null; |
49 | 51 | } catch (e) { |
50 | 52 | console.error(e); |
51 | 53 | } finally { |
|
70 | 72 |
|
71 | 73 | <svelte:window on:scroll={onScroll} /> |
72 | 74 |
|
73 | | -{#if data.response} |
74 | | - <DynamicHead |
75 | | - title={`${data.response.playlist.name} Playlist`} |
76 | | - description={`${data.response.playlist.tracks.length} tracks`} |
77 | | - opengraphImage={data.response.playlist?.tracks[0].album_art_url != null |
78 | | - ? { url: data.response.playlist?.tracks[0].album_art_url, height: '600', width: '600' } |
79 | | - : undefined} |
80 | | - /> |
81 | | -{:else} |
82 | | - <DynamicHead title="404 Not found" description="Playlist Not Found" /> |
83 | | -{/if} |
84 | | - |
85 | | -{#if data.response} |
86 | | - <div class="header"> |
87 | | - <div class="header-info"> |
88 | | - <h2>{data.response.playlist.name} Playlist</h2> |
89 | | - <div class="stats"> |
90 | | - <p>Last updated <Since time={data.response.playlist.last_modified} /></p> |
91 | | - <p> |
92 | | - {data.response.playlist.track_count} songs - {renderDuration( |
93 | | - data.response.playlist.duration_in_millis / 1000 |
94 | | - )} |
95 | | - </p> |
| 75 | +{#await data.response} |
| 76 | + <PageLoading /> |
| 77 | +{:then response} |
| 78 | + {#if response} |
| 79 | + <DynamicHead |
| 80 | + title={`${response.playlist.name} Playlist`} |
| 81 | + description={`${response.playlist.tracks.length} tracks`} |
| 82 | + opengraphImage={response.playlist?.tracks[0].album_art_url != null |
| 83 | + ? { url: response.playlist?.tracks[0].album_art_url, height: '600', width: '600' } |
| 84 | + : undefined} |
| 85 | + /> |
| 86 | + <div class="header"> |
| 87 | + <div class="header-info"> |
| 88 | + <h2>{response.playlist.name} Playlist</h2> |
| 89 | + <div class="stats"> |
| 90 | + <p>Last updated <Since time={response.playlist.last_modified} /></p> |
| 91 | + <p> |
| 92 | + {response.playlist.track_count} songs - {renderDuration( |
| 93 | + response.playlist.duration_in_millis / 1000 |
| 94 | + )} |
| 95 | + </p> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + <div class="view-on-buttons"> |
| 99 | + <a |
| 100 | + class="view-on-button" |
| 101 | + href={`https://open.spotify.com/playlist/${response.playlist.spotify_id}`} |
| 102 | + target="_blank" |
| 103 | + > |
| 104 | + <ViewButton on="Spotify" icon={SpotifyIcon} iconPaddingBottom="1px" iconColor="#24db68" /> |
| 105 | + </a> |
| 106 | + <a class="view-on-button" href={response.playlist.url} target="_blank"> |
| 107 | + <ViewButton |
| 108 | + on="Apple Music" |
| 109 | + icon={AppleMusicIcon} |
| 110 | + iconPaddingBottom="0.5px" |
| 111 | + iconColor="#fb455d" |
| 112 | + /> |
| 113 | + </a> |
96 | 114 | </div> |
97 | 115 | </div> |
98 | | - <div class="view-on-buttons"> |
99 | | - <a |
100 | | - class="view-on-button" |
101 | | - href={`https://open.spotify.com/playlist/${data.response.playlist.spotify_id}`} |
102 | | - target="_blank" |
103 | | - > |
104 | | - <ViewButton on="Spotify" icon={SpotifyIcon} iconPaddingBottom="1px" iconColor="#24db68" /> |
105 | | - </a> |
106 | | - <a class="view-on-button" href={data.response.playlist.url} target="_blank"> |
107 | | - <ViewButton |
108 | | - on="Apple Music" |
109 | | - icon={AppleMusicIcon} |
110 | | - iconPaddingBottom="0.5px" |
111 | | - iconColor="#fb455d" |
112 | | - /> |
113 | | - </a> |
| 116 | + <div class="songs"> |
| 117 | + {#each tracks as song (song)} |
| 118 | + <div class="song"> |
| 119 | + <Song {song} /> |
| 120 | + </div> |
| 121 | + {/each} |
114 | 122 | </div> |
115 | | - </div> |
116 | | - <div class="songs"> |
117 | | - {#each tracks as song (song)} |
118 | | - <div class="song"> |
119 | | - <Song {song} /> |
120 | | - </div> |
121 | | - {/each} |
122 | | - </div> |
123 | | -{:else} |
124 | | - <Error msg="404: Playlist not found" /> |
125 | | -{/if} |
| 123 | + {:else} |
| 124 | + <DynamicHead title="404 Not found" description="Playlist Not Found" /> |
| 125 | + <Error msg="404: Playlist Not Found" /> |
| 126 | + {/if} |
| 127 | +{/await} |
126 | 128 |
|
127 | 129 | <style> |
128 | 130 | .header { |
|
0 commit comments