Skip to content

Commit

Permalink
feat: display link to artist specified by artist.url
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Jul 19, 2023
1 parent 60fd80e commit f5e448d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
5 changes: 4 additions & 1 deletion demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { APlayer, type AudioInfo } from "../src";
const playlist1 = [
{
name: "Dancing with my phone",
artist: "HYBS",
artist: {
name: "HYBS",
url: "https://music.163.com/#/artist?id=49713779",
},
url: "https://music.163.com/song/media/outer/url?id=1969744125",
cover:
"https://p1.music.126.net/tOtUdKjS9rktAFRamcomWQ==/109951167748733958.jpg",
Expand Down
13 changes: 11 additions & 2 deletions src/components/list.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { clsx } from "clsx";
import { defaultThemeColor } from "../constants";
import type { AudioInfo } from "../types";
import type { ArtistInfo, AudioInfo } from "../types";
import { useCallback } from "react";

type PlaylistProps = {
open: boolean;
Expand All @@ -20,6 +21,14 @@ export function Playlist({
themeColor = defaultThemeColor,
}: PlaylistProps) {
const olStyle = listMaxHeight ? { maxHeight: listMaxHeight } : undefined;

const renderArtist = useCallback((artist?: string | ArtistInfo) => {
if (!artist) return "Audio artist";
if (typeof artist === "string") return artist;

return artist.name ?? "Audio artist";
}, []);

return (
<div
className={clsx("aplayer-list", {
Expand Down Expand Up @@ -50,7 +59,7 @@ export function Playlist({
{audioInfo.name ?? "Audio name"}
</span>
<span className="aplayer-list-author">
{audioInfo.artist ?? "Audio artist"}
{renderArtist(audioInfo.artist)}
</span>
</li>
))}
Expand Down
19 changes: 17 additions & 2 deletions src/components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { clsx } from "clsx";

import { ReactComponent as IconPlay } from "../assets/play.svg";
import { ReactComponent as IconPause } from "../assets/pause.svg";
import type { AudioInfo } from "../types";
import type { ArtistInfo, AudioInfo } from "../types";
import { Playlist } from "./list";
import { PlaybackControls } from "./controller";
import { useAudioControl } from "../hooks/useAudioControl";
Expand Down Expand Up @@ -145,6 +145,21 @@ export function APlayer({
[cancelAutoSkip, prioritize]
);

const renderArtist = useCallback((artist?: string | ArtistInfo) => {
if (!artist) return "Audio artist";
if (typeof artist === "string") return artist;

if (!artist.url) {
return artist.name ?? "Audio artist";
}

return (
<a href={artist.url} target="_blank" rel="noreferrer">
{artist.name ?? "Audio artist"}
</a>
);
}, []);

return (
<div
className={clsx("aplayer", {
Expand Down Expand Up @@ -177,7 +192,7 @@ export function APlayer({
</span>
<span className="aplayer-author">
{" "}
- {playlist.currentSong?.artist ?? "Audio artist"}
- {renderArtist(playlist.currentSong?.artist)}
</span>
</div>
<Lyrics
Expand Down
7 changes: 7 additions & 0 deletions src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@
font-size: 12px;
color: #666;
}
.aplayer .aplayer-info .aplayer-music .aplayer-author > a {
color: inherit;
text-decoration: none;
}
.aplayer .aplayer-info .aplayer-music .aplayer-author > a:hover {
text-decoration: underline;
}
.aplayer .aplayer-info .aplayer-controller {
position: relative;
display: flex;
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export type AudioInfo = {
name?: string;
artist?: string;
artist?: string | ArtistInfo;
url: string;
cover?: string;
lrc?: string;
theme?: string;
type?: "auto" | "hls" | "normal";
};

export type ArtistInfo = {
name?: string;
url?: string;
};

0 comments on commit f5e448d

Please # to comment.