Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 44x 44x 2x 2x 2x 2x 2x | import { Dispatch, SetStateAction } from "react"; import { getIdFromUri } from "./getIdFromUri"; import { AudioPlayer } from "hooks/useSpotifyPlayer"; import { IPageDetails, ITrack } from "types/spotify"; import { NewToast } from "types/toast"; import { play } from "utils/spotifyCalls"; export function getUris( isSingle?: boolean, allTracks?: ITrack[], trackUri?: string, uri?: string ): string[] { let uris = []; if (isSingle) { uris = trackUri ? [trackUri] : []; } else { const allTracksUris = allTracks ? allTracks.map((t) => t.uri) : []; const filteredUris = allTracksUris.filter( (t) => t !== null && t !== undefined ); uris = allTracks ? filteredUris : []; } const singleUri = trackUri ?? uri; Iif (uris.length === 0 && singleUri) { uris.push(singleUri); } return uris; } export function getPlayOptions( track?: ITrack, isSingle?: boolean, uri?: string, pageDetails?: IPageDetails | null, position?: number, allTracks?: ITrack[] ): { context_uri?: string; uris?: string[]; offset?: number } { Iif ((uri || pageDetails?.uri) && !isSingle) { return { context_uri: uri ?? pageDetails?.uri, }; } return { uris: getUris(isSingle, allTracks, track?.uri, uri), offset: position ?? 0, }; } export async function getCurrentState( player: Spotify.Player | undefined, addToast: (toast: NewToast) => void ): Promise<Spotify.PlaybackState | null> { Iif (!player) return null; Iif (!player.getCurrentState) { addToast({ variant: "error", message: "Not ready to play, if the issue persist refresh the page", }); return null; } const data = await player.getCurrentState(); return data; } export async function handlePremiumPlay( player: Spotify.Player | undefined, deviceId: string, addToast: (toast: NewToast) => void, setReconnectionError: Dispatch<SetStateAction<boolean>>, setPlaylistPlayingId: Dispatch<SetStateAction<string | undefined>>, setPlayedSource: Dispatch<SetStateAction<string | undefined>>, track?: ITrack, isSingle?: boolean, playlistPlayingId?: string, uriId?: string, pageDetails?: IPageDetails | null, uri?: string, isPlaying?: boolean, position?: number, allTracks?: ITrack[] ): Promise<void> { player?.activateElement(); const playbackState = await getCurrentState(player, addToast); const trackWindowUri = playbackState?.track_window?.current_track?.uri; const contextUri = playbackState?.context.uri; const isThisTrack = trackWindowUri && trackWindowUri === track?.uri; const isThisPlaylist = playlistPlayingId && playlistPlayingId === uriId && !isSingle; const isThisArtist = contextUri && contextUri === pageDetails?.uri && !track && !uri; const isSameAsPlayback = isThisTrack || isThisPlaylist || isThisArtist; Iif (isSameAsPlayback) { if (isPlaying) { player?.pause(); } else { player?.resume(); } return; } const playOptions = getPlayOptions( track, isSingle, uri, pageDetails, position, allTracks ); const playRes = await play(deviceId, playOptions); Iif (playRes.status === 404) { player?.disconnect(); addToast({ variant: "error", message: "Unable to play, trying to reconnect, please wait...", }); setReconnectionError(true); } Iif (playRes.ok) { const id = getIdFromUri(pageDetails?.uri, "id"); setPlaylistPlayingId(uriId ?? id); const source = track || isSingle ? (track?.uri ?? pageDetails?.uri) : (uri ?? pageDetails?.uri); const isCollection = source?.split(":")?.[3]; setPlayedSource( isCollection && pageDetails?.type && id ? `spotify:${pageDetails?.type}:${id}` : source ); } } export function handleNonPremiumPlay( player: AudioPlayer | undefined, isThisPlaying: boolean, setIsPlaying: Dispatch<SetStateAction<boolean>>, setCurrentlyPlaying: Dispatch<SetStateAction<ITrack | undefined>>, setPlaylistPlayingId: Dispatch<SetStateAction<string | undefined>>, allTracks: ITrack[], pageDetails?: IPageDetails | null, track?: ITrack ): void { Iif (!player) return; Iif (isThisPlaying || (pageDetails && !track)) { player?.togglePlay(); return; } player.allTracks = allTracks; Iif (track) { setIsPlaying(false); player.allTracks = [track]; } if (track?.preview_url || allTracks[0]?.preview_url) { const audio = track?.preview_url ?? allTracks[0].preview_url; player.src = audio as string; player.play?.(); setCurrentlyPlaying(track || allTracks[0]); } else { player.nextTrack(); } Iif (pageDetails) { setPlaylistPlayingId(getIdFromUri(pageDetails?.uri, "id")); } } |