feishin/src/renderer/features/discord-rpc/use-discord-rpc.ts

210 lines
8.3 KiB
TypeScript
Raw Normal View History

2025-08-01 16:43:34 +01:00
import { SetActivity, StatusDisplayType } from '@xhayper/discord-rpc';
2023-10-23 06:58:39 -07:00
import isElectron from 'is-electron';
2025-06-21 14:38:06 -05:00
import { useCallback, useEffect, useState } from 'react';
import { controller } from '/@/renderer/api/controller';
2023-10-23 06:58:39 -07:00
import {
2025-08-01 16:43:34 +01:00
DiscordDisplayType,
DiscordLinkType,
2025-07-31 16:12:03 +01:00
useAppStore,
2025-09-04 19:38:17 -07:00
useDiscordSettings,
useGeneralSettings,
2023-10-23 06:58:39 -07:00
usePlayerStore,
} from '/@/renderer/store';
import { sentenceCase } from '/@/renderer/utils';
2025-06-21 14:38:06 -05:00
import { QueueSong, ServerType } from '/@/shared/types/domain-types';
2025-05-20 19:23:36 -07:00
import { PlayerStatus } from '/@/shared/types/types';
2023-10-23 06:58:39 -07:00
const discordRpc = isElectron() ? window.api.discordRpc : null;
type ActivityState = [QueueSong | undefined, number, PlayerStatus];
2023-10-23 06:58:39 -07:00
export const useDiscordRpc = () => {
2025-09-04 19:38:17 -07:00
const discordSettings = useDiscordSettings();
const generalSettings = useGeneralSettings();
2025-07-31 16:12:03 +01:00
const { privateMode } = useAppStore();
2025-06-21 14:38:06 -05:00
const [lastUniqueId, setlastUniqueId] = useState('');
const setActivity = useCallback(
async (current: ActivityState, previous: ActivityState) => {
if (
!current[0] || // No track
current[1] === 0 || // Start of track
(current[2] === 'paused' && !discordSettings.showPaused) // Track paused with show paused setting disabled
) {
2025-06-21 14:38:06 -05:00
return discordRpc?.clearActivity();
}
2025-06-21 14:38:06 -05:00
// Handle change detection
const song = current[0];
2025-06-21 14:38:06 -05:00
const trackChanged = lastUniqueId !== song.uniqueId;
/*
1. If the song has just started, update status
2. If we jump more then 1.2 seconds from last state, update status to match
3. If the current song id is completely different, update status
4. If the player state changed, update status
2025-06-21 14:38:06 -05:00
*/
if (
previous[1] === 0 ||
Math.abs(current[1] - previous[1]) > 1.2 ||
2025-06-21 14:38:06 -05:00
trackChanged ||
current[2] !== previous[2]
) {
if (trackChanged) {
setlastUniqueId(song.uniqueId);
}
2025-06-21 14:38:06 -05:00
const start = Math.round(Date.now() - current[1] * 1000);
2025-06-21 14:38:06 -05:00
const end = Math.round(start + song.duration);
const artists = song?.artists.map((artist) => artist.name).join(', ');
2025-08-01 16:43:34 +01:00
const statusDisplayMap = {
[DiscordDisplayType.ARTIST_NAME]: StatusDisplayType.STATE,
[DiscordDisplayType.FEISHIN]: StatusDisplayType.NAME,
[DiscordDisplayType.SONG_NAME]: StatusDisplayType.DETAILS,
};
2025-06-21 14:38:06 -05:00
const activity: SetActivity = {
details: (song?.name && song.name.padEnd(2, ' ')) || 'Idle',
2025-06-21 14:38:06 -05:00
instance: false,
largeImageKey: undefined,
largeImageText: (song?.album && song.album.padEnd(2, ' ')) || 'Unknown album',
2025-06-21 14:38:06 -05:00
smallImageKey: undefined,
smallImageText: sentenceCase(current[2]),
state: (artists && artists.padEnd(2, ' ')) || 'Unknown artist',
2025-08-01 16:43:34 +01:00
statusDisplayType: statusDisplayMap[discordSettings.displayType],
2025-06-21 14:38:06 -05:00
// I would love to use the actual type as opposed to hardcoding to 2,
// but manually installing the discord-types package appears to break things
type: discordSettings.showAsListening ? 2 : 0,
};
if (
(discordSettings.linkType == DiscordLinkType.LAST_FM ||
discordSettings.linkType == DiscordLinkType.MBZ_LAST_FM) &&
song?.artistName
) {
activity.stateUrl =
'https://www.last.fm/music/' + encodeURIComponent(song.artists[0].name);
activity.detailsUrl =
'https://www.last.fm/music/' +
encodeURIComponent(song.albumArtists[0].name) +
'/' +
encodeURIComponent(song.album || '_') +
'/' +
encodeURIComponent(song.name);
}
if (
discordSettings.linkType == DiscordLinkType.MBZ ||
discordSettings.linkType == DiscordLinkType.MBZ_LAST_FM
) {
if (song?.mbzTrackId) {
activity.detailsUrl = 'https://musicbrainz.org/track/' + song.mbzTrackId;
} else if (song?.mbzRecordingId) {
activity.detailsUrl =
'https://musicbrainz.org/recording/' + song.mbzRecordingId;
}
}
if (current[2] === PlayerStatus.PLAYING) {
2025-06-21 14:38:06 -05:00
if (start && end) {
activity.startTimestamp = start;
activity.endTimestamp = end;
}
2023-10-23 06:58:39 -07:00
2025-06-21 14:38:06 -05:00
activity.smallImageKey = 'playing';
} else {
activity.smallImageKey = 'paused';
}
2023-10-23 06:58:39 -07:00
2025-06-21 14:38:06 -05:00
if (discordSettings.showServerImage && song) {
if (song.serverType === ServerType.JELLYFIN && song.imageUrl) {
activity.largeImageKey = song.imageUrl;
} else if (song.serverType === ServerType.NAVIDROME) {
try {
const info = await controller.getAlbumInfo({
apiClientProps: { serverId: song.serverId },
2025-06-21 14:38:06 -05:00
query: { id: song.albumId },
});
if (info.imageUrl) {
activity.largeImageKey = info.imageUrl;
}
} catch {
/* empty */
}
2025-05-15 19:10:15 -07:00
}
}
2025-06-21 14:38:06 -05:00
if (
activity.largeImageKey === undefined &&
generalSettings.lastfmApiKey &&
song?.album &&
song?.albumArtists.length
) {
const albumInfo = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=${generalSettings.lastfmApiKey}&artist=${encodeURIComponent(song.albumArtists[0].name)}&album=${encodeURIComponent(song.album)}&format=json`,
);
const albumInfoJson = await albumInfo.json();
if (albumInfoJson.album?.image?.[3]['#text']) {
activity.largeImageKey = albumInfoJson.album.image[3]['#text'];
}
}
// Fall back to default icon if not set
if (!activity.largeImageKey) {
activity.largeImageKey = 'icon';
}
// Initialize if needed
const isConnected = await discordRpc?.isConnected();
if (!isConnected) {
await discordRpc?.initialize(discordSettings.clientId);
}
2025-06-21 14:38:06 -05:00
discordRpc?.setActivity(activity);
2025-05-15 19:10:15 -07:00
}
2025-06-21 14:38:06 -05:00
},
[
discordSettings.showAsListening,
discordSettings.showServerImage,
discordSettings.showPaused,
2025-06-21 14:38:06 -05:00
generalSettings.lastfmApiKey,
discordSettings.clientId,
2025-08-01 16:43:34 +01:00
discordSettings.displayType,
discordSettings.linkType,
2025-06-21 14:38:06 -05:00
lastUniqueId,
],
);
2023-10-23 06:58:39 -07:00
useEffect(() => {
if (!discordSettings.enabled || privateMode) {
return discordRpc?.quit();
}
2023-10-23 06:58:39 -07:00
return () => {
discordRpc?.quit();
};
2025-07-31 16:12:03 +01:00
}, [discordSettings.clientId, privateMode, discordSettings.enabled]);
2023-10-23 06:58:39 -07:00
useEffect(() => {
if (!discordSettings.enabled || privateMode) {
return;
}
2025-06-21 14:38:06 -05:00
const unsubSongChange = usePlayerStore.subscribe(
(state): ActivityState => [
state.current.song,
state.current.time,
state.current.status,
],
2025-06-21 14:38:06 -05:00
setActivity,
);
return () => {
unsubSongChange();
};
2025-07-31 16:12:03 +01:00
}, [discordSettings.enabled, privateMode, setActivity]);
2023-10-23 06:58:39 -07:00
};