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
@@ -1,7 +1,10 @@
import { useEffect, useState } from 'react';
import { createWithEqualityFn } from 'zustand/traditional';
import { PlayerStatus } from '/@/shared/types/types';
const SCROBBLE_DEBUG_POLL_INTERVAL_MS = 1000;
export type ScrobbleDebugSnapshot = {
eligibilityMet: boolean;
lastListenSampleTimeSec: null | number;
@@ -38,6 +41,26 @@ export const useScrobbleDebugStore = createWithEqualityFn<ScrobbleDebugStore>()(
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>) => {
useScrobbleDebugStore.setState((state) => ({
snapshot: { ...state.snapshot, ...partial },