feishin/src/renderer/components/virtual-table/cells/favorite-cell.tsx

67 lines
1.8 KiB
TypeScript
Raw Normal View History

/* 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';
import { useCreateFavorite, useDeleteFavorite } from '/@/renderer/features/shared';
2023-01-07 16:33:14 -08:00
export const FavoriteCell = ({ value, data, node }: ICellRendererParams) => {
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>
);
};