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

62 lines
2 KiB
TypeScript
Raw Normal View History

2023-01-06 00:39:49 -08:00
import type { ICellRendererParams } from '@ag-grid-community/core';
2023-01-06 00:39:49 -08:00
import { CellContainer } from '/@/renderer/components/virtual-table/cells/generic-cell';
2025-09-22 22:39:46 -07:00
import { useCreateFavorite } from '/@/renderer/features/shared/mutations/create-favorite-mutation';
import { useDeleteFavorite } from '/@/renderer/features/shared/mutations/delete-favorite-mutation';
import { ActionIcon } from '/@/shared/components/action-icon/action-icon';
2023-01-07 16:33:14 -08:00
export const FavoriteCell = ({ data, node, value }: 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">
<ActionIcon
icon="favorite"
iconProps={{
fill: !value ? undefined : 'primary',
2023-07-01 19:10:05 -07:00
}}
onClick={handleToggleFavorite}
size="sm"
2023-07-01 19:10:05 -07:00
variant="subtle"
/>
2023-07-01 19:10:05 -07:00
</CellContainer>
);
2023-01-06 00:39:49 -08:00
};