fix how we favorite stuff

This commit is contained in:
Nate Kelley 2025-03-12 14:42:54 -06:00
parent 7eaee18f09
commit c84ddaa571
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
6 changed files with 60 additions and 30 deletions

View File

@ -103,7 +103,7 @@ export const useAddUserFavorite = () => {
mutationFn: createUserFavorite, mutationFn: createUserFavorite,
onMutate: (params) => { onMutate: (params) => {
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => { queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
return [params, ...(prev || [])]; return [...params, ...(prev || [])];
}); });
}, },
onSettled: () => { onSettled: () => {

View File

@ -1,11 +1,11 @@
import type { ShareAssetType } from '@/api/asset_interfaces/share'; import type { ShareAssetType } from '@/api/asset_interfaces/share';
export interface UsersFavoritePostPayload { export type UsersFavoritePostPayload = {
id: string; id: string;
asset_type: ShareAssetType; asset_type: ShareAssetType;
index?: number; index?: number;
name: string; name: string;
} }[];
export type UserFavoriteDeletePayload = { export type UserFavoriteDeletePayload = {
id: string; id: string;

View File

@ -1,6 +1,5 @@
import { ShareAssetType } from '@/api/asset_interfaces'; import { ShareAssetType } from '@/api/asset_interfaces';
import { AppTooltip } from '@/components/ui/tooltip'; import { AppTooltip } from '@/components/ui/tooltip';
import { useUserConfigContextSelector } from '@/context/Users';
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import { useMemoizedFn } from '@/hooks'; import { useMemoizedFn } from '@/hooks';
import { Button } from '@/components/ui/buttons'; import { Button } from '@/components/ui/buttons';
@ -49,11 +48,13 @@ export const FavoriteStar: React.FC<{
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
if (!isFavorited) if (!isFavorited)
return await addItemToFavorite({ return await addItemToFavorite([
{
asset_type: type, asset_type: type,
id, id,
name name
}); }
]);
await removeItemFromFavorite([ await removeItemFromFavorite([
{ {

View File

@ -142,7 +142,9 @@ const ThreeDotButton: React.FC<{
selectedRowKeys: string[]; selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void; onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => { }> = ({ selectedRowKeys, onSelectChange }) => {
const { mutateAsync: removeUserFavorite } = useDeleteUserFavorite(); const { mutateAsync: removeUserFavorite, isPending: removingFromFavorites } =
useDeleteUserFavorite();
const { mutateAsync: addUserFavorite, isPending: addingToFavorites } = useAddUserFavorite();
const { data: userFavorites } = useGetUserFavorites(); const { data: userFavorites } = useGetUserFavorites();
const dropdownOptions: DropdownItems = [ const dropdownOptions: DropdownItems = [
@ -150,15 +152,21 @@ const ThreeDotButton: React.FC<{
label: 'Add to favorites', label: 'Add to favorites',
icon: <Star />, icon: <Star />,
value: 'add-to-favorites', value: 'add-to-favorites',
loading: addingToFavorites,
onClick: async () => { onClick: async () => {
const allFavorites: string[] = [...userFavorites.map((f) => f.id), ...selectedRowKeys]; await addUserFavorite(
// bulkEditFavorites(allFavorites); selectedRowKeys.map((id) => ({
alert('TODO - feature not implemented yet'); id,
asset_type: ShareAssetType.METRIC,
name: 'Metric'
}))
);
} }
}, },
{ {
label: 'Remove from favorites', label: 'Remove from favorites',
icon: <Xmark />, icon: <Xmark />,
loading: removingFromFavorites,
value: 'remove-from-favorites', value: 'remove-from-favorites',
onClick: async () => { onClick: async () => {
const allFavorites: Parameters<typeof removeUserFavorite>[0] = userFavorites const allFavorites: Parameters<typeof removeUserFavorite>[0] = userFavorites

View File

@ -9,6 +9,12 @@ import { SaveToCollectionsDropdown } from '@/components/features/dropdowns/SaveT
import { useBusterDashboardContextSelector } from '@/context/Dashboards'; import { useBusterDashboardContextSelector } from '@/context/Dashboards';
import { ASSET_ICONS } from '@/components/features/config/assetIcons'; import { ASSET_ICONS } from '@/components/features/config/assetIcons';
import { Dots, Star, Trash, Xmark } from '@/components/ui/icons'; import { Dots, Star, Trash, Xmark } from '@/components/ui/icons';
import {
useAddUserFavorite,
useDeleteUserFavorite,
useGetUserFavorites
} from '@/api/buster_rest/users';
import { ShareAssetType } from '@/api/asset_interfaces/share';
export const DashboardSelectedOptionPopup: React.FC<{ export const DashboardSelectedOptionPopup: React.FC<{
selectedRowKeys: string[]; selectedRowKeys: string[];
@ -119,8 +125,9 @@ const ThreeDotButton: React.FC<{
selectedRowKeys: string[]; selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void; onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => { }> = ({ selectedRowKeys, onSelectChange }) => {
const bulkEditFavorites = useUserConfigContextSelector((state) => state.bulkEditFavorites); const { mutateAsync: addUserFavorite } = useAddUserFavorite();
const userFavorites = useUserConfigContextSelector((state) => state.userFavorites); const { mutateAsync: removeUserFavorite } = useDeleteUserFavorite();
const { data: userFavorites } = useGetUserFavorites();
const dropdownOptions: DropdownItems = [ const dropdownOptions: DropdownItems = [
{ {
@ -128,9 +135,12 @@ const ThreeDotButton: React.FC<{
icon: <Star />, icon: <Star />,
value: 'add-to-favorites', value: 'add-to-favorites',
onClick: async () => { onClick: async () => {
const allFavorites: string[] = [...userFavorites.map((f) => f.id), ...selectedRowKeys]; const allFavorites: Parameters<typeof addUserFavorite>[0] = selectedRowKeys.map((id) => ({
// bulkEditFavorites(allFavorites); id,
alert('TODO - feature not implemented yet'); asset_type: ShareAssetType.DASHBOARD,
name: 'Dashboard'
}));
await addUserFavorite(allFavorites);
} }
}, },
{ {
@ -138,10 +148,11 @@ const ThreeDotButton: React.FC<{
icon: <Xmark />, icon: <Xmark />,
value: 'remove-from-favorites', value: 'remove-from-favorites',
onClick: async () => { onClick: async () => {
const allFavorites: string[] = userFavorites const allFavorites: Parameters<typeof removeUserFavorite>[0] = userFavorites
.map((f) => f.id) .map((f) => f.id)
.filter((id) => !selectedRowKeys.includes(id)); .filter((id) => !selectedRowKeys.includes(id))
bulkEditFavorites(allFavorites); .map((id) => ({ id, asset_type: ShareAssetType.DASHBOARD }));
await removeUserFavorite(allFavorites);
} }
} }
]; ];

View File

@ -1,6 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { BusterListSelectedOptionPopupContainer } from '@/components/ui/list'; import { BusterListSelectedOptionPopupContainer } from '@/components/ui/list';
import { VerificationStatus } from '@/api/asset_interfaces'; import { ShareAssetType, VerificationStatus } from '@/api/asset_interfaces';
import { useBusterMetricsIndividualContextSelector } from '@/context/Metrics'; import { useBusterMetricsIndividualContextSelector } from '@/context/Metrics';
import { useUserConfigContextSelector } from '@/context/Users'; import { useUserConfigContextSelector } from '@/context/Users';
import { useMemoizedFn } from '@/hooks'; import { useMemoizedFn } from '@/hooks';
@ -12,6 +12,11 @@ import { Dropdown, DropdownItems } from '@/components/ui/dropdown';
import { StatusBadgeButton } from '@/components/features/metrics/StatusBadgeIndicator'; import { StatusBadgeButton } from '@/components/features/metrics/StatusBadgeIndicator';
import { Dots, Star, Trash, Xmark } from '@/components/ui/icons'; import { Dots, Star, Trash, Xmark } from '@/components/ui/icons';
import { useDeleteMetric } from '@/api/buster_rest/metrics'; import { useDeleteMetric } from '@/api/buster_rest/metrics';
import {
useAddUserFavorite,
useDeleteUserFavorite,
useGetUserFavorites
} from '@/api/buster_rest/users';
export const MetricSelectedOptionPopup: React.FC<{ export const MetricSelectedOptionPopup: React.FC<{
selectedRowKeys: string[]; selectedRowKeys: string[];
@ -174,8 +179,9 @@ const ThreeDotButton: React.FC<{
selectedRowKeys: string[]; selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void; onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => { }> = ({ selectedRowKeys, onSelectChange }) => {
const bulkEditFavorites = useUserConfigContextSelector((state) => state.bulkEditFavorites); const { mutateAsync: addUserFavorite } = useAddUserFavorite();
const userFavorites = useUserConfigContextSelector((state) => state.userFavorites); const { mutateAsync: removeUserFavorite } = useDeleteUserFavorite();
const { data: userFavorites } = useGetUserFavorites();
const dropdownOptions: DropdownItems = [ const dropdownOptions: DropdownItems = [
{ {
@ -183,9 +189,12 @@ const ThreeDotButton: React.FC<{
icon: <Star />, icon: <Star />,
value: 'add-to-favorites', value: 'add-to-favorites',
onClick: async () => { onClick: async () => {
const allFavorites: string[] = [...userFavorites.map((f) => f.id), ...selectedRowKeys]; const allFavorites: Parameters<typeof addUserFavorite>[0] = selectedRowKeys.map((id) => ({
// bulkEditFavorites(allFavorites); id,
alert('TODO - feature not implemented yet'); asset_type: ShareAssetType.METRIC,
name: 'Metric'
}));
await addUserFavorite(allFavorites);
} }
}, },
{ {
@ -193,10 +202,11 @@ const ThreeDotButton: React.FC<{
icon: <Xmark />, icon: <Xmark />,
value: 'remove-from-favorites', value: 'remove-from-favorites',
onClick: async () => { onClick: async () => {
const allFavorites: string[] = userFavorites const allFavorites: Parameters<typeof removeUserFavorite>[0] = userFavorites
.map((f) => f.id) .map((f) => f.id)
.filter((id) => !selectedRowKeys.includes(id)); .filter((id) => !selectedRowKeys.includes(id))
bulkEditFavorites(allFavorites); .map((id) => ({ id, asset_type: ShareAssetType.METRIC }));
await removeUserFavorite(allFavorites);
} }
} }
]; ];