Files
Psychotoxical-psysonic/src/hooks/useOrbitSongRowBehavior.ts
T
Psychotoxical cfb7e7f6c1 feat(orbit): per-guest suggestion mute + global pending-cap setting
Two anti-spam knobs the host can dial during a live session:

1. Per-guest suggestion mute — Mic / MicOff toggle next to the
   kick/ban buttons in the participants popover. Symmetric (re-enable
   later). State lives in OrbitState.suggestionBlocked: string[]; the
   guest reads it and disables its own Suggest controls so the user
   sees a clear "muted" state instead of silent failures. Host-side
   sweep also drops their outbox entries as a safety net.

2. Max pending approvals cap — number input in the session-settings
   popover, default 0 (= unlimited so existing sessions are unaffected).
   When set, the host sweep stops folding new outbox entries into the
   approval list once the cap is reached. The OrbitQueueHead surfaces
   "X / Y pending" so guests can see when they're getting close.

State changes are additive on the wire — both fields are optional, with
parseOrbitState defaulting them, so older clients keep working.

evaluateOrbitSuggestGate() centralises the guest-side allow/block check
shared between useOrbitSongRowBehavior and the ContextMenu Add-to-Session
items, plus suggestOrbitTrack as a defensive last line.

i18n: en + de + fr + nl + zh + nb + ru + es.

Also fixes an earlier dedupe-key collision: the (user, trackId) cache
keys were missing their separator (NULL byte slipped in during the
previous patch), so two tracks could share a key and one of them
silently overwrite the other. Restored the space separator.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 01:21:51 +02:00

80 lines
3.1 KiB
TypeScript

import { useCallback, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { useOrbitStore } from '../store/orbitStore';
import {
suggestOrbitTrack,
hostEnqueueToOrbit,
evaluateOrbitSuggestGate,
OrbitSuggestBlockedError,
} from '../utils/orbit';
import { showToast } from '../utils/toast';
/**
* Shared behaviour for song rows that in "normal mode" swallow a full list
* into the queue on single-click (AlbumDetail, PlaylistDetail, Favorites,
* ArtistDetail top-songs, SearchResults, RandomMix, AdvancedSearch).
*
* In an active Orbit session this is too destructive — the list would
* propagate to every guest's player. Instead:
*
* - `queueHint()` — show a toast telling the user to double-click.
* Safe to call on every single-click; 220 ms debounce
* suppresses the pileup that browsers emit before a
* dblclick fires.
* - `addTrackToOrbit(songId)` — cancel any pending hint and add just that
* one track: suggestOrbitTrack for guests,
* hostEnqueueToOrbit for the host.
*
* `orbitActive` is the gate — when false, callers should skip the hint and
* run their original bulk-play path unchanged.
*/
export function useOrbitSongRowBehavior() {
const { t } = useTranslation();
const orbitRole = useOrbitStore(s => s.role);
const orbitActive = orbitRole === 'host' || orbitRole === 'guest';
const clickTimerRef = useRef<number | null>(null);
const queueHint = useCallback(() => {
if (clickTimerRef.current !== null) return;
clickTimerRef.current = window.setTimeout(() => {
clickTimerRef.current = null;
showToast(t('albumDetail.orbitDoubleClickHint'), 2400, 'info');
}, 220);
}, [t]);
const addTrackToOrbit = useCallback((songId: string) => {
if (clickTimerRef.current !== null) {
clearTimeout(clickTimerRef.current);
clickTimerRef.current = null;
}
if (orbitRole === 'guest') {
const gate = evaluateOrbitSuggestGate();
if (!gate.allowed && gate.reason === 'muted') {
showToast(t('orbit.suggestBlockedMuted'), 3500, 'error');
return;
}
if (!gate.allowed && gate.reason === 'cap-reached') {
showToast(t('orbit.suggestBlockedCap'), 3500, 'info');
return;
}
suggestOrbitTrack(songId)
.then(() => showToast(t('orbit.ctxSuggestedToast'), 2200, 'info'))
.catch(err => {
if (err instanceof OrbitSuggestBlockedError && err.reason === 'muted') {
showToast(t('orbit.suggestBlockedMuted'), 3500, 'error');
} else if (err instanceof OrbitSuggestBlockedError && err.reason === 'cap-reached') {
showToast(t('orbit.suggestBlockedCap'), 3500, 'info');
} else {
showToast(t('orbit.ctxSuggestFailed'), 3000, 'error');
}
});
} else if (orbitRole === 'host') {
hostEnqueueToOrbit(songId)
.then(() => showToast(t('orbit.ctxAddedHostToast'), 2200, 'info'))
.catch(() => showToast(t('orbit.ctxAddHostFailed'), 3000, 'error'));
}
}, [orbitRole, t]);
return { orbitActive, queueHint, addTrackToOrbit };
}