Signed-off-by: mbrucedogs <mbrucedogs@gmail.com>
This commit is contained in:
parent
d98a8a6d14
commit
debabcc9e7
@ -8,10 +8,8 @@ import {
|
||||
} from 'ionicons/icons';
|
||||
import { useAppSelector } from '../../redux';
|
||||
import { selectIsAdmin, selectFavorites, selectSongs, selectQueue } from '../../redux';
|
||||
import { useSongOperations } from '../../hooks/useSongOperations';
|
||||
import { useDisabledSongs } from '../../hooks/useDisabledSongs';
|
||||
import { useActionHandlers } from '../../hooks/useActionHandlers';
|
||||
import { useModal } from '../../hooks/useModalContext';
|
||||
import { useToast } from '../../hooks/useToast';
|
||||
|
||||
import { ModalHeader } from './ModalHeader';
|
||||
import { SongInfoDisplay } from './SongItem';
|
||||
@ -28,9 +26,7 @@ const SongInfo: React.FC<SongInfoProps> = ({ isOpen, onClose, song }) => {
|
||||
const favorites = useAppSelector(selectFavorites);
|
||||
const allSongs = useAppSelector(selectSongs);
|
||||
const queue = useAppSelector(selectQueue);
|
||||
const { toggleFavorite } = useSongOperations();
|
||||
const { isSongDisabled, addDisabledSong, removeDisabledSong } = useDisabledSongs();
|
||||
const { showSuccess, showError } = useToast();
|
||||
const { handleToggleFavorite, handleToggleDisabled, isSongDisabled } = useActionHandlers();
|
||||
|
||||
const { openSelectSinger } = useModal();
|
||||
const [showArtistSongs, setShowArtistSongs] = useState(false);
|
||||
@ -51,27 +47,12 @@ const SongInfo: React.FC<SongInfoProps> = ({ isOpen, onClose, song }) => {
|
||||
setShowArtistSongs(true);
|
||||
};
|
||||
|
||||
const handleToggleFavorite = async () => {
|
||||
try {
|
||||
await toggleFavorite(song);
|
||||
showSuccess(isInFavorites ? 'Removed from favorites' : 'Added to favorites');
|
||||
} catch {
|
||||
showError('Failed to update favorites');
|
||||
}
|
||||
const handleToggleFavoriteClick = async () => {
|
||||
await handleToggleFavorite(song);
|
||||
};
|
||||
|
||||
const handleToggleDisabled = async () => {
|
||||
try {
|
||||
if (isDisabled) {
|
||||
await removeDisabledSong(song);
|
||||
showSuccess('Song enabled');
|
||||
} else {
|
||||
await addDisabledSong(song);
|
||||
showSuccess('Song disabled');
|
||||
}
|
||||
} catch {
|
||||
showError('Failed to update song status');
|
||||
}
|
||||
const handleToggleDisabledClick = async () => {
|
||||
await handleToggleDisabled(song);
|
||||
};
|
||||
|
||||
return (
|
||||
@ -132,7 +113,7 @@ const SongInfo: React.FC<SongInfoProps> = ({ isOpen, onClose, song }) => {
|
||||
<IonButton
|
||||
fill="solid"
|
||||
color={isInFavorites ? "danger" : "primary"}
|
||||
onClick={handleToggleFavorite}
|
||||
onClick={handleToggleFavoriteClick}
|
||||
className="h-12 w-80"
|
||||
style={{ width: '320px' }}
|
||||
>
|
||||
@ -145,7 +126,7 @@ const SongInfo: React.FC<SongInfoProps> = ({ isOpen, onClose, song }) => {
|
||||
<IonButton
|
||||
fill="solid"
|
||||
color={isDisabled ? "success" : "warning"}
|
||||
onClick={handleToggleDisabled}
|
||||
onClick={handleToggleDisabledClick}
|
||||
className="h-12 w-80"
|
||||
style={{ width: '320px' }}
|
||||
>
|
||||
|
||||
@ -11,5 +11,6 @@ export { useArtists } from './useArtists';
|
||||
export { useSingers } from './useSingers';
|
||||
export { useSongLists } from './useSongLists';
|
||||
export { useDisabledSongs } from './useDisabledSongs';
|
||||
export { useActionHandlers } from './useActionHandlers';
|
||||
|
||||
export { useSongInfo } from './useSongInfo';
|
||||
69
src/hooks/useActionHandlers.ts
Normal file
69
src/hooks/useActionHandlers.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useSongOperations } from './useSongOperations';
|
||||
import { useToast } from './useToast';
|
||||
import { useDisabledSongs } from './useDisabledSongs';
|
||||
import { historyService } from '../firebase/services';
|
||||
import { useAppSelector } from '../redux';
|
||||
import { selectControllerName } from '../redux';
|
||||
import type { Song } from '../types';
|
||||
|
||||
export const useActionHandlers = () => {
|
||||
const { addToQueue, toggleFavorite } = useSongOperations();
|
||||
const { showSuccess, showError } = useToast();
|
||||
const { isSongDisabled, addDisabledSong, removeDisabledSong } = useDisabledSongs();
|
||||
const controllerName = useAppSelector(selectControllerName);
|
||||
|
||||
const handleAddToQueue = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await addToQueue(song);
|
||||
showSuccess('Song added to queue');
|
||||
} catch {
|
||||
showError('Failed to add song to queue');
|
||||
}
|
||||
}, [addToQueue, showSuccess, showError]);
|
||||
|
||||
const handleToggleFavorite = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await toggleFavorite(song);
|
||||
showSuccess(song.favorite ? 'Removed from favorites' : 'Added to favorites');
|
||||
} catch {
|
||||
showError('Failed to update favorites');
|
||||
}
|
||||
}, [toggleFavorite, showSuccess, showError]);
|
||||
|
||||
const handleToggleDisabled = useCallback(async (song: Song) => {
|
||||
try {
|
||||
if (isSongDisabled(song)) {
|
||||
await removeDisabledSong(song);
|
||||
showSuccess('Song enabled');
|
||||
} else {
|
||||
await addDisabledSong(song);
|
||||
showSuccess('Song disabled');
|
||||
}
|
||||
} catch {
|
||||
showError('Failed to update song disabled status');
|
||||
}
|
||||
}, [isSongDisabled, addDisabledSong, removeDisabledSong, showSuccess, showError]);
|
||||
|
||||
const handleDeleteFromHistory = useCallback(async (song: Song) => {
|
||||
if (!controllerName || !song.key) {
|
||||
showError('Cannot delete history item - missing data');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await historyService.removeFromHistory(controllerName, song.key);
|
||||
showSuccess('Removed from history');
|
||||
} catch {
|
||||
showError('Failed to remove from history');
|
||||
}
|
||||
}, [controllerName, showSuccess, showError]);
|
||||
|
||||
return {
|
||||
handleAddToQueue,
|
||||
handleToggleFavorite,
|
||||
handleToggleDisabled,
|
||||
handleDeleteFromHistory,
|
||||
isSongDisabled,
|
||||
};
|
||||
};
|
||||
@ -1,18 +1,15 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useAppSelector, selectFavoritesArray } from '../redux';
|
||||
import { debugLog } from '../utils/logger';
|
||||
import { useSongOperations } from './useSongOperations';
|
||||
import { useToast } from './useToast';
|
||||
import { useActionHandlers } from './useActionHandlers';
|
||||
import { useDisabledSongs } from './useDisabledSongs';
|
||||
import type { Song } from '../types';
|
||||
|
||||
const ITEMS_PER_PAGE = 20;
|
||||
|
||||
export const useFavorites = () => {
|
||||
const allFavoritesItems = useAppSelector(selectFavoritesArray);
|
||||
const { addToQueue, toggleFavorite } = useSongOperations();
|
||||
const { showSuccess, showError } = useToast();
|
||||
const { disabledSongPaths, isSongDisabled, addDisabledSong, removeDisabledSong, loading: disabledSongsLoading } = useDisabledSongs();
|
||||
const { handleAddToQueue, handleToggleFavorite, handleToggleDisabled, isSongDisabled } = useActionHandlers();
|
||||
const { disabledSongPaths, loading: disabledSongsLoading } = useDisabledSongs();
|
||||
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
|
||||
@ -52,36 +49,6 @@ export const useFavorites = () => {
|
||||
}
|
||||
}, [hasMore, currentPage, allFavoritesItems.length]);
|
||||
|
||||
const handleAddToQueue = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await addToQueue(song);
|
||||
showSuccess('Song added to queue');
|
||||
} catch {
|
||||
showError('Failed to add song to queue');
|
||||
}
|
||||
}, [addToQueue, showSuccess, showError]);
|
||||
|
||||
const handleToggleFavorite = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await toggleFavorite(song);
|
||||
showSuccess('Removed from favorites');
|
||||
} catch {
|
||||
showError('Failed to remove from favorites');
|
||||
}
|
||||
}, [toggleFavorite, showSuccess, showError]);
|
||||
|
||||
const handleToggleDisabled = useCallback(async (song: Song) => {
|
||||
try {
|
||||
if (isSongDisabled(song)) {
|
||||
await removeDisabledSong(song);
|
||||
} else {
|
||||
await addDisabledSong(song);
|
||||
}
|
||||
} catch {
|
||||
showError('Failed to update song disabled status');
|
||||
}
|
||||
}, [isSongDisabled, addDisabledSong, removeDisabledSong, showError]);
|
||||
|
||||
return {
|
||||
favoritesItems,
|
||||
hasMore,
|
||||
|
||||
@ -1,22 +1,15 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useAppSelector, selectHistoryArray } from '../redux';
|
||||
import { debugLog } from '../utils/logger';
|
||||
import { useSongOperations } from './useSongOperations';
|
||||
import { useToast } from './useToast';
|
||||
import { useActionHandlers } from './useActionHandlers';
|
||||
import { useDisabledSongs } from './useDisabledSongs';
|
||||
import { historyService } from '../firebase/services';
|
||||
import { useAppSelector as useAppSelectorRedux } from '../redux';
|
||||
import { selectControllerName } from '../redux';
|
||||
import type { Song } from '../types';
|
||||
|
||||
const ITEMS_PER_PAGE = 20;
|
||||
|
||||
export const useHistory = () => {
|
||||
const allHistoryItems = useAppSelector(selectHistoryArray);
|
||||
const controllerName = useAppSelectorRedux(selectControllerName);
|
||||
const { addToQueue, toggleFavorite } = useSongOperations();
|
||||
const { showSuccess, showError } = useToast();
|
||||
const { disabledSongPaths, isSongDisabled, addDisabledSong, removeDisabledSong, loading: disabledSongsLoading } = useDisabledSongs();
|
||||
const { handleAddToQueue, handleToggleFavorite, handleToggleDisabled, handleDeleteFromHistory, isSongDisabled } = useActionHandlers();
|
||||
const { disabledSongPaths, loading: disabledSongsLoading } = useDisabledSongs();
|
||||
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
|
||||
@ -56,50 +49,6 @@ export const useHistory = () => {
|
||||
}
|
||||
}, [hasMore, currentPage, allHistoryItems.length]);
|
||||
|
||||
const handleAddToQueue = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await addToQueue(song);
|
||||
showSuccess('Song added to queue');
|
||||
} catch {
|
||||
showError('Failed to add song to queue');
|
||||
}
|
||||
}, [addToQueue, showSuccess, showError]);
|
||||
|
||||
const handleToggleFavorite = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await toggleFavorite(song);
|
||||
showSuccess(song.favorite ? 'Removed from favorites' : 'Added to favorites');
|
||||
} catch {
|
||||
showError('Failed to update favorites');
|
||||
}
|
||||
}, [toggleFavorite, showSuccess, showError]);
|
||||
|
||||
const handleToggleDisabled = useCallback(async (song: Song) => {
|
||||
try {
|
||||
if (isSongDisabled(song)) {
|
||||
await removeDisabledSong(song);
|
||||
} else {
|
||||
await addDisabledSong(song);
|
||||
}
|
||||
} catch {
|
||||
showError('Failed to update song disabled status');
|
||||
}
|
||||
}, [isSongDisabled, addDisabledSong, removeDisabledSong, showError]);
|
||||
|
||||
const handleDeleteFromHistory = useCallback(async (song: Song) => {
|
||||
if (!controllerName || !song.key) {
|
||||
showError('Cannot delete history item - missing data');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await historyService.removeFromHistory(controllerName, song.key);
|
||||
showSuccess('Removed from history');
|
||||
} catch {
|
||||
showError('Failed to remove from history');
|
||||
}
|
||||
}, [controllerName, showSuccess, showError]);
|
||||
|
||||
return {
|
||||
historyItems,
|
||||
hasMore,
|
||||
|
||||
@ -1,21 +1,18 @@
|
||||
import { useState, useCallback, useMemo } from 'react';
|
||||
import { useAppSelector, selectSongsArray } from '../redux';
|
||||
import { useSongOperations } from './useSongOperations';
|
||||
import { useToast } from './useToast';
|
||||
import { useActionHandlers } from './useActionHandlers';
|
||||
import { useDisabledSongs } from './useDisabledSongs';
|
||||
import { UI_CONSTANTS } from '../constants';
|
||||
import { filterSongs } from '../utils/dataProcessing';
|
||||
import { debugLog } from '../utils/logger';
|
||||
import type { Song } from '../types';
|
||||
|
||||
const ITEMS_PER_PAGE = 20;
|
||||
|
||||
export const useSearch = () => {
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const { addToQueue, toggleFavorite } = useSongOperations();
|
||||
const { showSuccess, showError } = useToast();
|
||||
const { disabledSongPaths, addDisabledSong, removeDisabledSong, isSongDisabled, loading: disabledSongsLoading } = useDisabledSongs();
|
||||
const { handleAddToQueue, handleToggleFavorite, handleToggleDisabled, isSongDisabled } = useActionHandlers();
|
||||
const { disabledSongPaths, loading: disabledSongsLoading } = useDisabledSongs();
|
||||
|
||||
// Get all songs from Redux (this is memoized)
|
||||
const allSongs = useAppSelector(selectSongsArray);
|
||||
@ -86,36 +83,6 @@ export const useSearch = () => {
|
||||
}
|
||||
}, [searchResults.hasMore]);
|
||||
|
||||
const handleAddToQueue = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await addToQueue(song);
|
||||
showSuccess('Song added to queue');
|
||||
} catch {
|
||||
showError('Failed to add song to queue');
|
||||
}
|
||||
}, [addToQueue, showSuccess, showError]);
|
||||
|
||||
const handleToggleFavorite = useCallback(async (song: Song) => {
|
||||
try {
|
||||
await toggleFavorite(song);
|
||||
showSuccess(song.favorite ? 'Removed from favorites' : 'Added to favorites');
|
||||
} catch {
|
||||
showError('Failed to update favorites');
|
||||
}
|
||||
}, [toggleFavorite, showSuccess, showError]);
|
||||
|
||||
const handleToggleDisabled = useCallback(async (song: Song) => {
|
||||
try {
|
||||
if (isSongDisabled(song)) {
|
||||
await removeDisabledSong(song);
|
||||
} else {
|
||||
await addDisabledSong(song);
|
||||
}
|
||||
} catch {
|
||||
showError('Failed to update song disabled status');
|
||||
}
|
||||
}, [isSongDisabled, addDisabledSong, removeDisabledSong, showError]);
|
||||
|
||||
return {
|
||||
searchTerm,
|
||||
searchResults,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user