feat(track): icon-based status badges, detect locally-cached tracks
Replace the labelled availability/metadata badges in track rows with small icon+tooltip indicators (cloud/hard-drives/warning/etc, derived from TrackAvailability and MetadataStatus). Add a `connection` slice fed by a single status poller (Sidebar) so other components can cheaply check backend reachability. TrackRow uses this plus the offline audio cache to show "Local" instead of a stale "On server" when the backend is down but the track is already cached.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { getApiBaseUrl } from '../config/runtime-config';
|
||||
import { useAppDispatch, useAppSelector } from './useAppDispatch';
|
||||
import {
|
||||
setConnectionStatus,
|
||||
type ConnectionStatus,
|
||||
} from '../store/slices/connection';
|
||||
|
||||
type ConnectionStatus = 'connected' | 'connecting' | 'disconnected' | 'error';
|
||||
export type { ConnectionStatus };
|
||||
|
||||
/** Pings `${baseUrl}/health` (defaults to the active instance's base URL). */
|
||||
export function useConnectionStatus(baseUrl?: string) {
|
||||
@@ -36,3 +41,26 @@ export function useConnectionStatus(baseUrl?: string) {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like `useConnectionStatus`, but also mirrors the result into the
|
||||
* `connection` slice so other components can read the active instance's
|
||||
* reachability via `useIsOffline` without running their own poller.
|
||||
* Mount once (in `Sidebar`, which lives for the app's whole lifetime).
|
||||
*/
|
||||
export function useConnectionStatusSync(): ConnectionStatus {
|
||||
const status = useConnectionStatus();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(setConnectionStatus(status));
|
||||
}, [status, dispatch]);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/** Whether the active backend instance is currently unreachable. */
|
||||
export function useIsOffline(): boolean {
|
||||
const status = useAppSelector((s) => s.connection.status);
|
||||
return status === 'disconnected' || status === 'error';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user