84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import { toast } from "sonner"
|
|
|
|
export type MediaShareContext =
|
|
| { type: "album"; albumId: string }
|
|
| { type: "song"; albumId: string; songId: string }
|
|
| { type: "playlist"; playlistId: string; isFavorites?: boolean }
|
|
| { type: "artist"; artistId: string }
|
|
|
|
function mediaOrigin() {
|
|
if (typeof window === "undefined") {
|
|
return ""
|
|
}
|
|
return window.location.origin
|
|
}
|
|
|
|
export function albumShareUrl(albumId: string) {
|
|
return `${mediaOrigin()}/album/${albumId}`
|
|
}
|
|
|
|
export function songShareUrl(albumId: string, songId: string) {
|
|
return `${mediaOrigin()}/album/${albumId}?s=${songId}`
|
|
}
|
|
|
|
export function playlistShareUrl(
|
|
playlistId: string,
|
|
options?: { isFavorites?: boolean }
|
|
) {
|
|
if (options?.isFavorites) {
|
|
return `${mediaOrigin()}/playlist/favorites`
|
|
}
|
|
return `${mediaOrigin()}/playlist/${playlistId}`
|
|
}
|
|
|
|
export function artistShareUrl(artistId: string) {
|
|
return `${mediaOrigin()}/artist/${artistId}`
|
|
}
|
|
|
|
export function mediaShareUrl(share: MediaShareContext) {
|
|
switch (share.type) {
|
|
case "album":
|
|
return albumShareUrl(share.albumId)
|
|
case "song":
|
|
return songShareUrl(share.albumId, share.songId)
|
|
case "playlist":
|
|
return playlistShareUrl(share.playlistId, {
|
|
isFavorites: share.isFavorites,
|
|
})
|
|
case "artist":
|
|
return artistShareUrl(share.artistId)
|
|
}
|
|
}
|
|
|
|
export async function copyTextToClipboard(text: string) {
|
|
await navigator.clipboard.writeText(text)
|
|
toast.success("Link Copied")
|
|
}
|
|
|
|
export async function copyMediaShareLink(share: MediaShareContext) {
|
|
await copyTextToClipboard(mediaShareUrl(share))
|
|
}
|
|
|
|
export async function copyAlbumLink(albumId: string) {
|
|
await copyMediaShareLink({ type: "album", albumId })
|
|
}
|
|
|
|
export async function copySongLink(albumId: string, songId: string) {
|
|
await copyMediaShareLink({ type: "song", albumId, songId })
|
|
}
|
|
|
|
export async function copyPlaylistLink(
|
|
playlistId: string,
|
|
options?: { isFavorites?: boolean }
|
|
) {
|
|
await copyMediaShareLink({
|
|
type: "playlist",
|
|
playlistId,
|
|
isFavorites: options?.isFavorites,
|
|
})
|
|
}
|
|
|
|
export async function copyArtistLink(artistId: string) {
|
|
await copyMediaShareLink({ type: "artist", artistId })
|
|
}
|