refactor player timestamp hook with fixed interval

This commit is contained in:
jeffvli
2026-07-08 19:56:47 -07:00
parent 88b570cacb
commit bd5cd672ef
4 changed files with 56 additions and 6 deletions
@@ -32,7 +32,6 @@ export const PlayerbarSlider = () => {
const formattedDuration = formatDuration(songDuration * 1000 || 0); const formattedDuration = formatDuration(songDuration * 1000 || 0);
const formattedTimeRemaining = formatDuration((currentTime - songDuration) * 1000 || 0); const formattedTimeRemaining = formatDuration((currentTime - songDuration) * 1000 || 0);
const formattedTime = formatDuration(currentTime * 1000 || 0);
const showTimeRemaining = useAppStore((state) => state.showTimeRemaining); const showTimeRemaining = useAppStore((state) => state.showTimeRemaining);
const { setShowTimeRemaining } = useAppStoreActions(); const { setShowTimeRemaining } = useAppStoreActions();
@@ -43,7 +42,7 @@ export const PlayerbarSlider = () => {
<> <>
<div className={styles.sliderContainer}> <div className={styles.sliderContainer}>
<div className={styles.sliderValueWrapper}> <div className={styles.sliderValueWrapper}>
<ScrobbleStatus formattedTime={formattedTime} /> <ScrobbleStatus />
</div> </div>
<div className={styles.sliderWrapper}> <div className={styles.sliderWrapper}>
{isWaveform ? ( {isWaveform ? (
@@ -1,10 +1,16 @@
import formatDuration from 'format-duration';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { import {
invokeScrobbleForceSubmit, invokeScrobbleForceSubmit,
invokeScrobbleResetListenedState, invokeScrobbleResetListenedState,
} from '/@/renderer/features/player/hooks/use-scrobble'; } from '/@/renderer/features/player/hooks/use-scrobble';
import { useAppStore, useScrobbleDebugStore, useSettingsStore } from '/@/renderer/store'; import {
useAppStore,
usePlayerTimestamp,
useScrobbleDebugSnapshot,
useSettingsStore,
} from '/@/renderer/store';
import { Button } from '/@/shared/components/button/button'; import { Button } from '/@/shared/components/button/button';
import { Group } from '/@/shared/components/group/group'; import { Group } from '/@/shared/components/group/group';
import { HoverCard } from '/@/shared/components/hover-card/hover-card'; import { HoverCard } from '/@/shared/components/hover-card/hover-card';
@@ -32,11 +38,12 @@ const ScrobbleConditionProgress = ({ value }: { value: number }) => (
<Progress {...scrobbleProgressProps} value={value} w="100%" /> <Progress {...scrobbleProgressProps} value={value} w="100%" />
); );
export const ScrobbleStatus = ({ formattedTime }: { formattedTime: string }) => { export const ScrobbleStatus = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const scrobbleEnabled = useSettingsStore((state) => state.playback.scrobble.enabled); const scrobbleEnabled = useSettingsStore((state) => state.playback.scrobble.enabled);
const privateMode = useAppStore((state) => state.privateMode); const privateMode = useAppStore((state) => state.privateMode);
const snapshot = useScrobbleDebugStore((state) => state.snapshot); const snapshot = useScrobbleDebugSnapshot();
const formattedTime = formatDuration(usePlayerTimestamp() * 1000 || 0);
const hookInactive = !scrobbleEnabled || privateMode; const hookInactive = !scrobbleEnabled || privateMode;
@@ -1,7 +1,10 @@
import { useEffect, useState } from 'react';
import { createWithEqualityFn } from 'zustand/traditional'; import { createWithEqualityFn } from 'zustand/traditional';
import { PlayerStatus } from '/@/shared/types/types'; import { PlayerStatus } from '/@/shared/types/types';
const SCROBBLE_DEBUG_POLL_INTERVAL_MS = 1000;
export type ScrobbleDebugSnapshot = { export type ScrobbleDebugSnapshot = {
eligibilityMet: boolean; eligibilityMet: boolean;
lastListenSampleTimeSec: null | number; lastListenSampleTimeSec: null | number;
@@ -38,6 +41,26 @@ export const useScrobbleDebugStore = createWithEqualityFn<ScrobbleDebugStore>()(
snapshot: initialSnapshot, snapshot: initialSnapshot,
})); }));
export const useScrobbleDebugSnapshot = () => {
const [snapshot, setLocalSnapshot] = useState(() => useScrobbleDebugStore.getState().snapshot);
useEffect(() => {
const syncSnapshot = () => {
const nextSnapshot = useScrobbleDebugStore.getState().snapshot;
setLocalSnapshot((prevSnapshot) =>
prevSnapshot !== nextSnapshot ? nextSnapshot : prevSnapshot,
);
};
syncSnapshot();
const interval = setInterval(syncSnapshot, SCROBBLE_DEBUG_POLL_INTERVAL_MS);
return () => clearInterval(interval);
}, []);
return snapshot;
};
export const publishScrobbleDebug = (partial: Partial<ScrobbleDebugSnapshot>) => { export const publishScrobbleDebug = (partial: Partial<ScrobbleDebugSnapshot>) => {
useScrobbleDebugStore.setState((state) => ({ useScrobbleDebugStore.setState((state) => ({
snapshot: { ...state.snapshot, ...partial }, snapshot: { ...state.snapshot, ...partial },
+22 -1
View File
@@ -1,7 +1,10 @@
import { del, get, set } from 'idb-keyval'; import { del, get, set } from 'idb-keyval';
import { useEffect, useState } from 'react';
import { persist, subscribeWithSelector } from 'zustand/middleware'; import { persist, subscribeWithSelector } from 'zustand/middleware';
import { createWithEqualityFn } from 'zustand/traditional'; import { createWithEqualityFn } from 'zustand/traditional';
const PLAYER_TIMESTAMP_POLL_INTERVAL_MS = 500;
interface TimestampState { interface TimestampState {
setTimestamp: (timestamp: number) => void; setTimestamp: (timestamp: number) => void;
timestamp: number; timestamp: number;
@@ -60,7 +63,25 @@ export const usePlayerProgress = () => {
}; };
export const usePlayerTimestamp = () => { export const usePlayerTimestamp = () => {
return useTimestampStoreBase((state) => state.timestamp); const [timestamp, setLocalTimestamp] = useState(
() => useTimestampStoreBase.getState().timestamp,
);
useEffect(() => {
const syncTimestamp = () => {
const nextTimestamp = useTimestampStoreBase.getState().timestamp;
setLocalTimestamp((prevTimestamp) =>
prevTimestamp !== nextTimestamp ? nextTimestamp : prevTimestamp,
);
};
syncTimestamp();
const interval = setInterval(syncTimestamp, PLAYER_TIMESTAMP_POLL_INTERVAL_MS);
return () => clearInterval(interval);
}, []);
return timestamp;
}; };
export const setTimestamp = (timestamp: number) => { export const setTimestamp = (timestamp: number) => {