mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-02 02:43:33 +00:00
Fix short album/artist name breaking Discord presence (#1191)
* Fix short album/artist name breaking Discord presence * fix lint rule that isn't firing, cleanup types --------- Co-authored-by: Kendall Garner <17521368+kgarner7@users.noreply.github.com>
This commit is contained in:
parent
b16e57710b
commit
848b3f58b3
1 changed files with 30 additions and 18 deletions
|
|
@ -12,10 +12,12 @@ import {
|
||||||
useGeneralSettings,
|
useGeneralSettings,
|
||||||
usePlayerStore,
|
usePlayerStore,
|
||||||
} from '/@/renderer/store';
|
} from '/@/renderer/store';
|
||||||
|
import { sentenceCase } from '/@/renderer/utils';
|
||||||
import { QueueSong, ServerType } from '/@/shared/types/domain-types';
|
import { QueueSong, ServerType } from '/@/shared/types/domain-types';
|
||||||
import { PlayerStatus } from '/@/shared/types/types';
|
import { PlayerStatus } from '/@/shared/types/types';
|
||||||
|
|
||||||
const discordRpc = isElectron() ? window.api.discordRpc : null;
|
const discordRpc = isElectron() ? window.api.discordRpc : null;
|
||||||
|
type ActivityState = [QueueSong | undefined, number, PlayerStatus];
|
||||||
|
|
||||||
export const useDiscordRpc = () => {
|
export const useDiscordRpc = () => {
|
||||||
const discordSettings = useDiscordSettings();
|
const discordSettings = useDiscordSettings();
|
||||||
|
|
@ -24,19 +26,17 @@ export const useDiscordRpc = () => {
|
||||||
const [lastUniqueId, setlastUniqueId] = useState('');
|
const [lastUniqueId, setlastUniqueId] = useState('');
|
||||||
|
|
||||||
const setActivity = useCallback(
|
const setActivity = useCallback(
|
||||||
async (
|
async (current: ActivityState, previous: ActivityState) => {
|
||||||
current: (number | PlayerStatus | QueueSong | undefined)[],
|
|
||||||
previous: (number | PlayerStatus | QueueSong | undefined)[],
|
|
||||||
) => {
|
|
||||||
if (
|
if (
|
||||||
!current[0] || // No track
|
!current[0] || // No track
|
||||||
current[1] === 0 || // Start of track
|
current[1] === 0 || // Start of track
|
||||||
(current[2] === 'paused' && !discordSettings.showPaused) // Track paused with show paused setting disabled
|
(current[2] === 'paused' && !discordSettings.showPaused) // Track paused with show paused setting disabled
|
||||||
)
|
) {
|
||||||
return discordRpc?.clearActivity();
|
return discordRpc?.clearActivity();
|
||||||
|
}
|
||||||
|
|
||||||
// Handle change detection
|
// Handle change detection
|
||||||
const song = current[0] as QueueSong;
|
const song = current[0];
|
||||||
const trackChanged = lastUniqueId !== song.uniqueId;
|
const trackChanged = lastUniqueId !== song.uniqueId;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -47,13 +47,15 @@ export const useDiscordRpc = () => {
|
||||||
*/
|
*/
|
||||||
if (
|
if (
|
||||||
previous[1] === 0 ||
|
previous[1] === 0 ||
|
||||||
Math.abs((current[1] as number) - (previous[1] as number)) > 1.2 ||
|
Math.abs(current[1] - previous[1]) > 1.2 ||
|
||||||
trackChanged ||
|
trackChanged ||
|
||||||
current[2] !== previous[2]
|
current[2] !== previous[2]
|
||||||
) {
|
) {
|
||||||
if (trackChanged) setlastUniqueId(song.uniqueId);
|
if (trackChanged) {
|
||||||
|
setlastUniqueId(song.uniqueId);
|
||||||
|
}
|
||||||
|
|
||||||
const start = Math.round(Date.now() - (current[1] as number) * 1000);
|
const start = Math.round(Date.now() - current[1] * 1000);
|
||||||
const end = Math.round(start + song.duration);
|
const end = Math.round(start + song.duration);
|
||||||
|
|
||||||
const artists = song?.artists.map((artist) => artist.name).join(', ');
|
const artists = song?.artists.map((artist) => artist.name).join(', ');
|
||||||
|
|
@ -65,13 +67,13 @@ export const useDiscordRpc = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const activity: SetActivity = {
|
const activity: SetActivity = {
|
||||||
details: song?.name.padEnd(2, ' ') || 'Idle',
|
details: (song?.name && song.name.padEnd(2, ' ')) || 'Idle',
|
||||||
instance: false,
|
instance: false,
|
||||||
largeImageKey: undefined,
|
largeImageKey: undefined,
|
||||||
largeImageText: song?.album || 'Unknown album',
|
largeImageText: (song?.album && song.album.padEnd(2, ' ')) || 'Unknown album',
|
||||||
smallImageKey: undefined,
|
smallImageKey: undefined,
|
||||||
smallImageText: current[2] as string,
|
smallImageText: sentenceCase(current[2]),
|
||||||
state: artists || 'Unknown artist',
|
state: (artists && artists.padEnd(2, ' ')) || 'Unknown artist',
|
||||||
statusDisplayType: statusDisplayMap[discordSettings.displayType],
|
statusDisplayType: statusDisplayMap[discordSettings.displayType],
|
||||||
// I would love to use the actual type as opposed to hardcoding to 2,
|
// 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
|
// but manually installing the discord-types package appears to break things
|
||||||
|
|
@ -106,7 +108,7 @@ export const useDiscordRpc = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((current[2] as PlayerStatus) === PlayerStatus.PLAYING) {
|
if (current[2] === PlayerStatus.PLAYING) {
|
||||||
if (start && end) {
|
if (start && end) {
|
||||||
activity.startTimestamp = start;
|
activity.startTimestamp = start;
|
||||||
activity.endTimestamp = end;
|
activity.endTimestamp = end;
|
||||||
|
|
@ -162,7 +164,9 @@ export const useDiscordRpc = () => {
|
||||||
|
|
||||||
// Initialize if needed
|
// Initialize if needed
|
||||||
const isConnected = await discordRpc?.isConnected();
|
const isConnected = await discordRpc?.isConnected();
|
||||||
if (!isConnected) await discordRpc?.initialize(discordSettings.clientId);
|
if (!isConnected) {
|
||||||
|
await discordRpc?.initialize(discordSettings.clientId);
|
||||||
|
}
|
||||||
|
|
||||||
discordRpc?.setActivity(activity);
|
discordRpc?.setActivity(activity);
|
||||||
}
|
}
|
||||||
|
|
@ -180,7 +184,9 @@ export const useDiscordRpc = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!discordSettings.enabled || privateMode) return discordRpc?.quit();
|
if (!discordSettings.enabled || privateMode) {
|
||||||
|
return discordRpc?.quit();
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
discordRpc?.quit();
|
discordRpc?.quit();
|
||||||
|
|
@ -188,9 +194,15 @@ export const useDiscordRpc = () => {
|
||||||
}, [discordSettings.clientId, privateMode, discordSettings.enabled]);
|
}, [discordSettings.clientId, privateMode, discordSettings.enabled]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!discordSettings.enabled || privateMode) return;
|
if (!discordSettings.enabled || privateMode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const unsubSongChange = usePlayerStore.subscribe(
|
const unsubSongChange = usePlayerStore.subscribe(
|
||||||
(state) => [state.current.song, state.current.time, state.current.status],
|
(state): ActivityState => [
|
||||||
|
state.current.song,
|
||||||
|
state.current.time,
|
||||||
|
state.current.status,
|
||||||
|
],
|
||||||
setActivity,
|
setActivity,
|
||||||
);
|
);
|
||||||
return () => {
|
return () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue