36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useAppSelector } from '../redux';
|
|
import { selectHistoryArray } from '../redux/selectors';
|
|
import { useSongOperations } from './useSongOperations';
|
|
import { useToast } from './useToast';
|
|
import type { Song } from '../types';
|
|
|
|
export const useHistory = () => {
|
|
const historyItems = useAppSelector(selectHistoryArray);
|
|
const { addToQueue, toggleFavorite } = useSongOperations();
|
|
const { showSuccess, showError } = useToast();
|
|
|
|
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]);
|
|
|
|
return {
|
|
historyItems,
|
|
handleAddToQueue,
|
|
handleToggleFavorite,
|
|
};
|
|
};
|