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

67 lines
2.2 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) => {
2023-07-01 19:10:05 -07:00
const createMutation = useCreateFavorite({});
const deleteMutation = useDeleteFavorite({});
2023-01-07 16:33:14 -08:00
2023-07-01 19:10:05 -07:00
const handleToggleFavorite = () => {
const newFavoriteValue = !value;
2023-01-07 16:33:14 -08:00
2023-07-01 19:10:05 -07:00
if (newFavoriteValue) {
createMutation.mutate(
{
query: {
id: [data.id],
type: data.itemType,
},
serverId: data.serverId,
},
{
onSuccess: () => {
node.setData({ ...data, userFavorite: newFavoriteValue });
},
},
);
} else {
deleteMutation.mutate(
{
query: {
id: [data.id],
type: data.itemType,
},
serverId: data.serverId,
},
{
onSuccess: () => {
node.setData({ ...data, userFavorite: newFavoriteValue });
},
},
);
}
};
2023-01-06 00:39:49 -08:00
2023-07-01 19:10:05 -07:00
return (
<CellContainer $position="center">
2023-07-01 19:10:05 -07:00
<Button
compact
sx={{
svg: {
fill: !value
? 'var(--main-fg-secondary) !important'
: 'var(--primary-color) !important',
},
}}
variant="subtle"
onClick={handleToggleFavorite}
>
{!value ? <RiHeartLine size="1.3em" /> : <RiHeartFill size="1.3em" />}
</Button>
</CellContainer>
);
2023-01-06 00:39:49 -08:00
};