From 10be5ad19f8268317bd27fb8fbe01365a9d370e2 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Fri, 14 Mar 2025 17:32:46 -0600 Subject: [PATCH] favorite for three dot menu --- .../components/features/list/FavoriteStar.tsx | 61 +++++++++++++------ .../MetricThreeDotMenu.tsx | 50 +++++++++------ 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/web/src/components/features/list/FavoriteStar.tsx b/web/src/components/features/list/FavoriteStar.tsx index 314978384..5b86c5df0 100644 --- a/web/src/components/features/list/FavoriteStar.tsx +++ b/web/src/components/features/list/FavoriteStar.tsx @@ -37,26 +37,7 @@ export const FavoriteStar: React.FC<{ className?: string; iconStyle?: 'default' | 'tertiary'; }> = React.memo(({ title: name, id, type, className = '', iconStyle = 'default' }) => { - const { data: userFavorites } = useGetUserFavorites(); - const { mutateAsync: removeItemFromFavorite } = useDeleteUserFavorite(); - const { mutateAsync: addItemToFavorite } = useAddUserFavorite(); - - const isFavorited = useMemo(() => { - return userFavorites?.some((favorite) => favorite.id === id || favorite.collection_id === id); - }, [userFavorites, id]); - - const onFavoriteClick = useMemoizedFn(async (e: React.MouseEvent) => { - e.stopPropagation(); - e.preventDefault(); - if (!isFavorited) - return await addItemToFavorite({ - asset_type: type, - id, - name - }); - - await removeItemFromFavorite(id); - }); + const { isFavorited, onFavoriteClick } = useFavoriteStar({ id, type, name }); const tooltipText = isFavorited ? 'Remove from favorites' : 'Add to favorites'; @@ -82,3 +63,43 @@ export const FavoriteStar: React.FC<{ ); }); FavoriteStar.displayName = 'FavoriteStar'; + +export const useFavoriteStar = ({ + id, + type, + name +}: { + id: string; + type: ShareAssetType; + name: string; +}) => { + const { data: userFavorites } = useGetUserFavorites(); + const { mutateAsync: removeItemFromFavorite } = useDeleteUserFavorite(); + const { mutateAsync: addItemToFavorite } = useAddUserFavorite(); + + const isFavorited = useMemo(() => { + return userFavorites?.some((favorite) => favorite.id === id || favorite.collection_id === id); + }, [userFavorites, id]); + + console.log('userFavorites', isFavorited, userFavorites); + + const onFavoriteClick = useMemoizedFn(async (e?: React.MouseEvent) => { + e?.stopPropagation(); + e?.preventDefault(); + if (!isFavorited) + return await addItemToFavorite({ + asset_type: type, + id, + name + }); + await removeItemFromFavorite(id); + }); + + return useMemo( + () => ({ + isFavorited, + onFavoriteClick + }), + [isFavorited, onFavoriteClick] + ); +}; diff --git a/web/src/layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons/MetricThreeDotMenu.tsx b/web/src/layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons/MetricThreeDotMenu.tsx index bd2b2cf36..a28b9730c 100644 --- a/web/src/layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons/MetricThreeDotMenu.tsx +++ b/web/src/layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons/MetricThreeDotMenu.tsx @@ -34,9 +34,10 @@ import { StatusNotRequestedIcon } from '@/assets'; import { useSaveToDashboardDropdownContent } from '@/components/features/dropdowns/SaveToDashboardDropdown'; import { useMemoizedFn } from '@/hooks'; import { useSaveToCollectionsDropdownContent } from '@/components/features/dropdowns/SaveToCollectionsDropdown'; -import { VerificationStatus } from '@/api/asset_interfaces/share'; +import { ShareAssetType, VerificationStatus } from '@/api/asset_interfaces/share'; import { useStatusDropdownContent } from '@/components/features/metrics/StatusBadgeIndicator/StatusDropdownContent'; import { StatusBadgeIndicator } from '@/components/features/metrics/StatusBadgeIndicator'; +import { useFavoriteStar } from '@/components/features/list/FavoriteStar'; export const ThreeDotMenuButton = React.memo(({ metricId }: { metricId: string }) => { const { mutateAsync: deleteMetric, isPending: isDeletingMetric } = useDeleteMetric(); @@ -47,6 +48,7 @@ export const ThreeDotMenuButton = React.memo(({ metricId }: { metricId: string } const versionHistoryItems = useVersionHistorySelectMenu({ metricId }); const collectionSelectMenu = useCollectionSelectMenu({ metricId }); const statusSelectMenu = useStatusSelectMenu({ metricId }); + const favoriteMetric = useFavoriteMetric({ metricId }); const items: DropdownItems = useMemo( () => [ @@ -54,23 +56,13 @@ export const ThreeDotMenuButton = React.memo(({ metricId }: { metricId: string } label: 'Share', value: 'share', icon: , - items: [], - onClick: () => { - console.log('share metric'); - } + items: [] }, statusSelectMenu, { type: 'divider' }, dashboardSelectMenu, collectionSelectMenu, - { - label: 'Add to favorites', - value: 'add-to-favorites', - icon: , - onClick: () => { - console.log('add to favorites'); - } - }, + favoriteMetric, { type: 'divider' }, { label: 'Edit chart', @@ -138,12 +130,14 @@ export const ThreeDotMenuButton = React.memo(({ metricId }: { metricId: string } ], [ dashboardSelectMenu, - deleteMetric, isDeletingMetric, metricId, openSuccessMessage, onSetSelectedFile, - versionHistoryItems + versionHistoryItems, + favoriteMetric, + statusSelectMenu, + collectionSelectMenu ] ); @@ -210,10 +204,7 @@ const useVersionHistorySelectMenu = ({ metricId }: { metricId: string }) => { label: `Version ${x.version_number}`, secondaryLabel: timeFromNow(x.updated_at, false), value: x.version_number.toString(), - selected: x.version_number === version_number, - onClick: () => { - console.log('version history', x.version_number); - } + selected: x.version_number === version_number })); }, [versions, version_number]); @@ -308,3 +299,24 @@ const useStatusSelectMenu = ({ metricId }: { metricId: string }) => { return statusDropdownItem; }; + +const useFavoriteMetric = ({ metricId }: { metricId: string }) => { + const { data: title } = useGetMetric(metricId, (x) => x.title); + const { isFavorited, onFavoriteClick } = useFavoriteStar({ + id: metricId, + type: ShareAssetType.METRIC, + name: title || '' + }); + + const item: DropdownItem = useMemo( + () => ({ + label: isFavorited ? 'Remove from favorites' : 'Add to favorites', + value: 'add-to-favorites', + icon: isFavorited ? : , + onClick: onFavoriteClick + }), + [isFavorited, onFavoriteClick] + ); + + return item; +};