mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-26 08:27:38 +00:00
7c724a642f
* chore(eslint): add eslint toolchain and configs * fix(eslint): resolve gradual-config errors (rules-of-hooks, no-empty, prefer-const, …) * chore(eslint): clear unused vars in config, contexts, app and test * chore(eslint): clear unused vars in api and music-network * chore(eslint): clear unused vars in utils * chore(eslint): clear unused vars in store * chore(eslint): clear unused vars in cover and hooks * chore(eslint): clear unused vars in components * chore(eslint): clear unused vars in pages * chore(eslint): remove explicit any in src * chore(eslint): align react-hooks exhaustive-deps * chore(eslint): zero gradual config on src * chore(eslint): strict hook rules in store and utils * chore(eslint): strict hook rules in hooks and cover * chore(eslint): strict hook rules in components * chore(eslint): strict hook rules in pages and contexts * chore(eslint): document scripts ignore in eslint config * chore(eslint): add lint script and zero strict findings * chore(eslint): address review round 1 (gradual 0/0, per-site disable reasons) * chore(eslint): tighten two set-state disable comments (review round 2 LOW) * chore(nix): sync npmDepsHash with package-lock.json * docs(changelog): add Under the Hood entry for ESLint setup (PR #1165) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
140 lines
4.3 KiB
TypeScript
140 lines
4.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import type { SubsonicAlbum, SubsonicArtist, SubsonicSong } from '../api/subsonicTypes';
|
|
import {
|
|
resolveShareSearchAlbum,
|
|
resolveShareSearchArtist,
|
|
resolveShareSearchPayload,
|
|
} from '../utils/share/enqueueShareSearchPayload';
|
|
import type { ShareSearchMatch } from '../utils/share/shareSearch';
|
|
|
|
export interface ShareSearchPreviewState {
|
|
shareTrackSong: SubsonicSong | null;
|
|
shareTrackResolving: boolean;
|
|
shareTrackUnavailable: boolean;
|
|
shareAlbum: SubsonicAlbum | null;
|
|
shareAlbumResolving: boolean;
|
|
shareAlbumUnavailable: boolean;
|
|
shareArtist: SubsonicArtist | null;
|
|
shareArtistResolving: boolean;
|
|
shareArtistUnavailable: boolean;
|
|
shareComposer: SubsonicArtist | null;
|
|
shareComposerResolving: boolean;
|
|
shareComposerUnavailable: boolean;
|
|
}
|
|
|
|
const EMPTY_PREVIEW: ShareSearchPreviewState = {
|
|
shareTrackSong: null,
|
|
shareTrackResolving: false,
|
|
shareTrackUnavailable: false,
|
|
shareAlbum: null,
|
|
shareAlbumResolving: false,
|
|
shareAlbumUnavailable: false,
|
|
shareArtist: null,
|
|
shareArtistResolving: false,
|
|
shareArtistUnavailable: false,
|
|
shareComposer: null,
|
|
shareComposerResolving: false,
|
|
shareComposerUnavailable: false,
|
|
};
|
|
|
|
export function useShareSearchPreview(shareMatch: ShareSearchMatch | null): ShareSearchPreviewState {
|
|
const [preview, setPreview] = useState<ShareSearchPreviewState>(EMPTY_PREVIEW);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
// React Compiler set-state-in-effect rule: state set from an async result resolved in this effect.
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
setPreview(EMPTY_PREVIEW);
|
|
|
|
if (shareMatch?.type === 'queueable' && shareMatch.payload.k === 'track') {
|
|
setPreview({ ...EMPTY_PREVIEW, shareTrackResolving: true });
|
|
void resolveShareSearchPayload(shareMatch.payload)
|
|
.then(result => {
|
|
if (cancelled) return;
|
|
setPreview({
|
|
...EMPTY_PREVIEW,
|
|
shareTrackSong: result.type === 'ok' ? (result.songs[0] ?? null) : null,
|
|
shareTrackUnavailable: result.type !== 'ok' || result.songs.length === 0,
|
|
});
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) {
|
|
setPreview(current => ({ ...current, shareTrackResolving: false }));
|
|
}
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}
|
|
|
|
if (shareMatch?.type === 'artist') {
|
|
setPreview({ ...EMPTY_PREVIEW, shareArtistResolving: true });
|
|
void resolveShareSearchArtist(shareMatch.payload)
|
|
.then(result => {
|
|
if (cancelled) return;
|
|
setPreview({
|
|
...EMPTY_PREVIEW,
|
|
shareArtist: result.type === 'ok' ? result.artist : null,
|
|
shareArtistUnavailable: result.type !== 'ok',
|
|
});
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) {
|
|
setPreview(current => ({ ...current, shareArtistResolving: false }));
|
|
}
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}
|
|
|
|
if (shareMatch?.type === 'composer') {
|
|
setPreview({ ...EMPTY_PREVIEW, shareComposerResolving: true });
|
|
void resolveShareSearchArtist(shareMatch.payload)
|
|
.then(result => {
|
|
if (cancelled) return;
|
|
setPreview({
|
|
...EMPTY_PREVIEW,
|
|
shareComposer: result.type === 'ok' ? result.artist : null,
|
|
shareComposerUnavailable: result.type !== 'ok',
|
|
});
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) {
|
|
setPreview(current => ({ ...current, shareComposerResolving: false }));
|
|
}
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}
|
|
|
|
if (shareMatch?.type === 'album') {
|
|
setPreview({ ...EMPTY_PREVIEW, shareAlbumResolving: true });
|
|
void resolveShareSearchAlbum(shareMatch.payload)
|
|
.then(result => {
|
|
if (cancelled) return;
|
|
setPreview({
|
|
...EMPTY_PREVIEW,
|
|
shareAlbum: result.type === 'ok' ? result.album : null,
|
|
shareAlbumUnavailable: result.type !== 'ok',
|
|
});
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) {
|
|
setPreview(current => ({ ...current, shareAlbumResolving: false }));
|
|
}
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [shareMatch]);
|
|
|
|
return preview;
|
|
}
|