2023-04-30 22:01:52 -07:00
|
|
|
/* eslint-disable import/no-cycle */
|
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-04-30 22:01:52 -07:00
|
|
|
import { useCreateFavorite, useDeleteFavorite } from '/@/renderer/features/shared';
|
2023-01-07 16:33:14 -08:00
|
|
|
|
|
|
|
|
export const FavoriteCell = ({ value, data, node }: ICellRendererParams) => {
|
2023-04-30 22:01:52 -07:00
|
|
|
const createMutation = useCreateFavorite({});
|
|
|
|
|
const deleteMutation = useDeleteFavorite({});
|
2023-01-07 16:33:14 -08:00
|
|
|
|
|
|
|
|
const handleToggleFavorite = () => {
|
|
|
|
|
const newFavoriteValue = !value;
|
|
|
|
|
|
|
|
|
|
if (newFavoriteValue) {
|
|
|
|
|
createMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
id: [data.id],
|
|
|
|
|
type: data.itemType,
|
|
|
|
|
},
|
2023-05-13 22:54:24 -07:00
|
|
|
serverId: data.serverId,
|
2023-01-07 16:33:14 -08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
node.setData({ ...data, userFavorite: newFavoriteValue });
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
deleteMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
id: [data.id],
|
|
|
|
|
type: data.itemType,
|
|
|
|
|
},
|
2023-05-13 22:54:24 -07:00
|
|
|
serverId: data.serverId,
|
2023-01-07 16:33:14 -08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
};
|