2023-01-06 00:39:49 -08:00
|
|
|
import type { ICellRendererParams } from '@ag-grid-community/core';
|
|
|
|
|
import { RiHeartFill, RiHeartLine } from 'react-icons/ri';
|
|
|
|
|
import { Button } from '/@/renderer/components/button';
|
|
|
|
|
import { CellContainer } from '/@/renderer/components/virtual-table/cells/generic-cell';
|
2023-01-07 16:33:14 -08:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
import { HTTPError } from 'ky';
|
|
|
|
|
import { api } from '/@/renderer/api';
|
|
|
|
|
import { RawFavoriteResponse, FavoriteArgs, LibraryItem } from '/@/renderer/api/types';
|
2023-01-08 00:52:53 -08:00
|
|
|
import {
|
|
|
|
|
useCurrentServer,
|
|
|
|
|
useSetAlbumListItemDataById,
|
|
|
|
|
useSetQueueFavorite,
|
|
|
|
|
} from '/@/renderer/store';
|
2023-01-07 16:33:14 -08:00
|
|
|
|
|
|
|
|
const useCreateFavorite = () => {
|
|
|
|
|
const server = useCurrentServer();
|
2023-01-08 00:52:53 -08:00
|
|
|
const setAlbumListData = useSetAlbumListItemDataById();
|
2023-01-07 16:33:14 -08:00
|
|
|
|
|
|
|
|
return useMutation<RawFavoriteResponse, HTTPError, Omit<FavoriteArgs, 'server'>, null>({
|
|
|
|
|
mutationFn: (args) => api.controller.createFavorite({ ...args, server }),
|
2023-01-08 00:52:53 -08:00
|
|
|
onSuccess: (_data, variables) => {
|
|
|
|
|
for (const id of variables.query.id) {
|
|
|
|
|
// Set the userFavorite property to true for the album in the album list data store
|
|
|
|
|
if (variables.query.type === LibraryItem.ALBUM) {
|
|
|
|
|
setAlbumListData(id, { userFavorite: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-01-07 16:33:14 -08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const useDeleteFavorite = () => {
|
|
|
|
|
const server = useCurrentServer();
|
2023-01-08 00:52:53 -08:00
|
|
|
const setAlbumListData = useSetAlbumListItemDataById();
|
2023-01-07 16:33:14 -08:00
|
|
|
|
|
|
|
|
return useMutation<RawFavoriteResponse, HTTPError, Omit<FavoriteArgs, 'server'>, null>({
|
|
|
|
|
mutationFn: (args) => api.controller.deleteFavorite({ ...args, server }),
|
2023-01-08 00:52:53 -08:00
|
|
|
onSuccess: (_data, variables) => {
|
|
|
|
|
for (const id of variables.query.id) {
|
|
|
|
|
// Set the userFavorite property to false for the album in the album list data store
|
|
|
|
|
if (variables.query.type === LibraryItem.ALBUM) {
|
|
|
|
|
setAlbumListData(id, { userFavorite: false });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-01-07 16:33:14 -08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const FavoriteCell = ({ value, data, node }: ICellRendererParams) => {
|
|
|
|
|
const createMutation = useCreateFavorite();
|
|
|
|
|
const deleteMutation = useDeleteFavorite();
|
|
|
|
|
|
|
|
|
|
// Since the queue is using client-side state, we need to update it manually
|
|
|
|
|
const setFavorite = useSetQueueFavorite();
|
|
|
|
|
|
|
|
|
|
const handleToggleFavorite = () => {
|
|
|
|
|
const newFavoriteValue = !value;
|
|
|
|
|
|
|
|
|
|
if (newFavoriteValue) {
|
|
|
|
|
createMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
id: [data.id],
|
|
|
|
|
type: data.itemType,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
if (data.itemType === LibraryItem.SONG) {
|
|
|
|
|
setFavorite([data.id], newFavoriteValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node.setData({ ...data, userFavorite: newFavoriteValue });
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
deleteMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
id: [data.id],
|
|
|
|
|
type: data.itemType,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
if (data.itemType === LibraryItem.SONG) {
|
|
|
|
|
setFavorite([data.id], newFavoriteValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node.setData({ ...data, userFavorite: newFavoriteValue });
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-01-06 00:39:49 -08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CellContainer position="center">
|
|
|
|
|
<Button
|
|
|
|
|
compact
|
2023-01-07 16:33:14 -08:00
|
|
|
sx={{
|
|
|
|
|
svg: {
|
2023-01-07 23:44:53 -08:00
|
|
|
fill: !value
|
|
|
|
|
? 'var(--main-fg-secondary) !important'
|
|
|
|
|
: 'var(--primary-color) !important',
|
2023-01-07 16:33:14 -08:00
|
|
|
},
|
|
|
|
|
}}
|
2023-01-06 00:39:49 -08:00
|
|
|
variant="subtle"
|
2023-01-07 16:33:14 -08:00
|
|
|
onClick={handleToggleFavorite}
|
2023-01-06 00:39:49 -08:00
|
|
|
>
|
2023-01-07 23:44:53 -08:00
|
|
|
{!value ? <RiHeartLine size="1.3em" /> : <RiHeartFill size="1.3em" />}
|
2023-01-06 00:39:49 -08:00
|
|
|
</Button>
|
|
|
|
|
</CellContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|