35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
/** A piece of media that has a shareable page on the web app. */
|
|
export type MediaShareTarget =
|
|
| { type: "album"; albumId: string }
|
|
| { type: "song"; albumId: string; songId: string }
|
|
| { type: "playlist"; playlistId: string; isFavorites?: boolean }
|
|
| { type: "artist"; artistId: string }
|
|
|
|
/** Builds the web app URL for `share`, relative to `origin` (no trailing slash needed). */
|
|
export function mediaShareUrl(origin: string, share: MediaShareTarget) {
|
|
const base = origin.replace(/\/+$/, "")
|
|
switch (share.type) {
|
|
case "album":
|
|
return `${base}/album/${share.albumId}`
|
|
case "song":
|
|
return `${base}/album/${share.albumId}?s=${share.songId}`
|
|
case "playlist":
|
|
return share.isFavorites
|
|
? `${base}/playlist/favorites`
|
|
: `${base}/playlist/${share.playlistId}`
|
|
case "artist":
|
|
return `${base}/artist/${share.artistId}`
|
|
}
|
|
}
|
|
|
|
/** Songs are shared one at a time; collections are shared whatever is selected. */
|
|
export function shouldShowShare(
|
|
share: MediaShareTarget | undefined,
|
|
selectedCount: number
|
|
) {
|
|
if (!share) {
|
|
return false
|
|
}
|
|
return share.type !== "song" || selectedCount === 1
|
|
}
|