From debabcc9e78109d8cd360f05b66f88338099c755 Mon Sep 17 00:00:00 2001 From: mbrucedogs Date: Sun, 20 Jul 2025 20:16:33 -0500 Subject: [PATCH] Signed-off-by: mbrucedogs --- src/components/common/SongInfo.tsx | 35 ++++----------- src/hooks/index.ts | 1 + src/hooks/useActionHandlers.ts | 69 ++++++++++++++++++++++++++++++ src/hooks/useFavorites.ts | 39 ++--------------- src/hooks/useHistory.ts | 57 ++---------------------- src/hooks/useSearch.ts | 39 ++--------------- 6 files changed, 87 insertions(+), 153 deletions(-) create mode 100644 src/hooks/useActionHandlers.ts diff --git a/src/components/common/SongInfo.tsx b/src/components/common/SongInfo.tsx index b3a15e1..a512819 100644 --- a/src/components/common/SongInfo.tsx +++ b/src/components/common/SongInfo.tsx @@ -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 = ({ 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 = ({ 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 = ({ isOpen, onClose, song }) => { @@ -145,7 +126,7 @@ const SongInfo: React.FC = ({ isOpen, onClose, song }) => { diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 2e1b9b8..04f2d98 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -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'; \ No newline at end of file diff --git a/src/hooks/useActionHandlers.ts b/src/hooks/useActionHandlers.ts new file mode 100644 index 0000000..879b808 --- /dev/null +++ b/src/hooks/useActionHandlers.ts @@ -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, + }; +}; \ No newline at end of file diff --git a/src/hooks/useFavorites.ts b/src/hooks/useFavorites.ts index d33deec..ca68368 100644 --- a/src/hooks/useFavorites.ts +++ b/src/hooks/useFavorites.ts @@ -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, diff --git a/src/hooks/useHistory.ts b/src/hooks/useHistory.ts index acfc626..ccad200 100644 --- a/src/hooks/useHistory.ts +++ b/src/hooks/useHistory.ts @@ -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, diff --git a/src/hooks/useSearch.ts b/src/hooks/useSearch.ts index 909a6f5..1cb1983 100644 --- a/src/hooks/useSearch.ts +++ b/src/hooks/useSearch.ts @@ -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,