mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-21 22:15:40 +00:00
3643a78cd6
- NowPlaying polling: only active while dropdown is open + respects Page Visibility API — was firing every 10s unconditionally (~8.6k req/day) - Connection check interval: 30s → 120s (4× reduction) - Queue sync debounce: 1.5s → 5s (prevents burst on rapid skip) - Rating prefetch: add 7-minute in-memory TTL cache for artist/album ratings — repeated random album loads no longer re-fetch known ratings Fixes server log flooding reported by users behind reverse proxies (Traefik). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { useState, useEffect, useCallback, useRef } from 'react';
|
|
import { useAuthStore } from '../store/authStore';
|
|
import { pingWithCredentials } from '../api/subsonic';
|
|
|
|
export type ConnectionStatus = 'connected' | 'disconnected' | 'checking';
|
|
|
|
export function isLanUrl(url: string): boolean {
|
|
try {
|
|
const hostname = new URL(url.startsWith('http') ? url : `http://${url}`).hostname;
|
|
return (
|
|
hostname === 'localhost' ||
|
|
hostname.endsWith('.local') ||
|
|
/^127\./.test(hostname) ||
|
|
/^10\./.test(hostname) ||
|
|
/^192\.168\./.test(hostname) ||
|
|
/^172\.(1[6-9]|2\d|3[01])\./.test(hostname)
|
|
);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function useConnectionStatus() {
|
|
const [status, setStatus] = useState<ConnectionStatus>('checking');
|
|
const [isRetrying, setIsRetrying] = useState(false);
|
|
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
|
|
const check = useCallback(async () => {
|
|
const server = useAuthStore.getState().getActiveServer();
|
|
if (!server) {
|
|
setStatus('disconnected');
|
|
return;
|
|
}
|
|
|
|
if (!navigator.onLine) {
|
|
setStatus('disconnected');
|
|
return;
|
|
}
|
|
|
|
const ok = await pingWithCredentials(server.url, server.username, server.password);
|
|
setStatus(ok ? 'connected' : 'disconnected');
|
|
}, []);
|
|
|
|
const retry = useCallback(async () => {
|
|
setIsRetrying(true);
|
|
await check();
|
|
setIsRetrying(false);
|
|
}, [check]);
|
|
|
|
useEffect(() => {
|
|
check();
|
|
intervalRef.current = setInterval(check, 120_000);
|
|
|
|
const handleOnline = () => check();
|
|
const handleOffline = () => setStatus('disconnected');
|
|
|
|
window.addEventListener('online', handleOnline);
|
|
window.addEventListener('offline', handleOffline);
|
|
|
|
return () => {
|
|
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
window.removeEventListener('online', handleOnline);
|
|
window.removeEventListener('offline', handleOffline);
|
|
};
|
|
}, [check]);
|
|
|
|
const server = useAuthStore(s => s.getActiveServer());
|
|
|
|
return {
|
|
status,
|
|
isRetrying,
|
|
retry,
|
|
isLan: server ? isLanUrl(server.url) : false,
|
|
serverName: server?.name ?? '',
|
|
};
|
|
}
|