refactor(auth): E.46 — extract logic-bearing factories (#611)

Three factories for the actions that carry real logic (not just
`set({ field: v })` pass-through):

- `createSkipStarActions` — skip-to-1★ counter with threshold check
  and per-`<activeServerId><trackId>` storage key. Disabling the
  feature wipes the counter map so a re-enable doesn't resume from
  stale partial counts. Crossing the threshold deletes the key so
  the next session starts fresh.
- `createMusicLibraryActions` — `setMusicFolders` falls back to
  `'all'` when the persisted filter points at a folder the server
  no longer reports; `setMusicLibraryFilter` bumps
  `musicLibraryFilterVersion` so subscribed pages refetch.
- `createPerServerCapabilityActions` — `setEntityRatingSupport`,
  `setAudiomuseNavidromeEnabled`, `setSubsonicServerIdentity`,
  `setInstantMixProbe`, `setAudiomuseNavidromeIssue`. Each branch
  knows which neighbouring per-server maps to wipe when the input
  invalidates them (identity not AudioMuse-eligible / probe empty /
  audiomuse disabled).

Pure code-move. authStore.ts: 384 → 270 LOC (−114). All trivial-setter
and logic-bearing action bodies are now out of the store body; only
the persist config + `onRehydrateStorage` migration block remains as
a non-factory chunk (target for E.47).
This commit is contained in:
Frank Stellmacher
2026-05-12 23:44:17 +02:00
committed by GitHub
parent fe6acdaa4c
commit 2c0aef9538
4 changed files with 211 additions and 120 deletions
+65
View File
@@ -0,0 +1,65 @@
import {
clampSkipStarThreshold,
skipStarCountStorageKey,
} from './authStoreHelpers';
import type { AuthState } from './authStoreTypes';
type SetState = (
partial: Partial<AuthState> | ((state: AuthState) => Partial<AuthState>),
) => void;
type GetState = () => AuthState;
/**
* Skip-to-1★ feature: after N manual skips of the same track, set
* its rating to 1 if still unrated. Counter is per
* `<activeServerId><trackId>` key, persisted in
* `skipStarManualSkipCountsByKey`, and cleared either when the
* threshold is crossed (so the next session starts fresh) or when
* the track ends naturally without a skip.
*
* Disabling the feature wipes the counter map so re-enabling later
* doesn't resume from stale partial counts.
*/
export function createSkipStarActions(set: SetState, get: GetState): Pick<
AuthState,
| 'setSkipStarOnManualSkipsEnabled'
| 'setSkipStarManualSkipThreshold'
| 'recordSkipStarManualAdvance'
| 'clearSkipStarManualCountForTrack'
> {
return {
setSkipStarOnManualSkipsEnabled: (v) =>
set({
skipStarOnManualSkipsEnabled: v,
...(v ? {} : { skipStarManualSkipCountsByKey: {} }),
}),
setSkipStarManualSkipThreshold: (v) =>
set({ skipStarManualSkipThreshold: clampSkipStarThreshold(v) }),
recordSkipStarManualAdvance: (trackId: string) => {
const s = get();
if (!s.skipStarOnManualSkipsEnabled || s.skipStarManualSkipThreshold < 1) return null;
const key = skipStarCountStorageKey(s.activeServerId, trackId);
const prev = s.skipStarManualSkipCountsByKey[key] ?? 0;
const threshold = s.skipStarManualSkipThreshold;
const next = prev + 1;
if (next >= threshold) {
const { [key]: _removed, ...rest } = s.skipStarManualSkipCountsByKey;
set({ skipStarManualSkipCountsByKey: rest });
return { crossedThreshold: true };
}
set({
skipStarManualSkipCountsByKey: { ...s.skipStarManualSkipCountsByKey, [key]: next },
});
return { crossedThreshold: false };
},
clearSkipStarManualCountForTrack: (trackId: string) => {
const s = get();
const key = skipStarCountStorageKey(s.activeServerId, trackId);
if (s.skipStarManualSkipCountsByKey[key] === undefined) return;
const { [key]: _removed, ...rest } = s.skipStarManualSkipCountsByKey;
set({ skipStarManualSkipCountsByKey: rest });
},
};
}