2025-02-07 05:31:46 +08:00
|
|
|
import React, { useState } from 'react';
|
2025-03-01 02:49:57 +08:00
|
|
|
import { SaveToCollectionsDropdown } from '../dropdowns/SaveToCollectionsDropdown';
|
2025-03-01 02:30:39 +08:00
|
|
|
import { useMemoizedFn } from 'ahooks';
|
2025-02-07 05:31:46 +08:00
|
|
|
import { useBusterNotifications } from '@/context/BusterNotifications';
|
2025-02-13 12:57:05 +08:00
|
|
|
import { useBusterCollectionListContextSelector } from '@/context/Collections';
|
2025-02-08 03:45:47 +08:00
|
|
|
import { useBusterDashboardContextSelector } from '@/context/Dashboards';
|
2025-03-01 02:30:39 +08:00
|
|
|
import { CollectionButton } from './CollectionsButton';
|
2025-02-07 05:31:46 +08:00
|
|
|
|
|
|
|
export const SaveDashboardToCollectionButton: React.FC<{
|
|
|
|
dashboardIds: string[];
|
|
|
|
buttonType?: 'text' | 'default';
|
|
|
|
useText?: boolean;
|
|
|
|
}> = React.memo(({ dashboardIds, buttonType = 'text', useText = false }) => {
|
|
|
|
const { openInfoMessage } = useBusterNotifications();
|
2025-02-13 12:57:05 +08:00
|
|
|
const collectionsList = useBusterCollectionListContextSelector((state) => state.collectionsList);
|
|
|
|
|
2025-02-07 05:31:46 +08:00
|
|
|
const [selectedCollections, setSelectedCollections] = useState<
|
|
|
|
Parameters<typeof SaveToCollectionsDropdown>[0]['selectedCollections']
|
|
|
|
>([]);
|
2025-02-08 03:45:47 +08:00
|
|
|
const saveDashboardToCollection = useBusterDashboardContextSelector(
|
|
|
|
(state) => state.onAddToCollection
|
|
|
|
);
|
|
|
|
const removeDashboardFromCollection = useBusterDashboardContextSelector(
|
2025-02-07 05:31:46 +08:00
|
|
|
(state) => state.onRemoveFromCollection
|
|
|
|
);
|
|
|
|
|
|
|
|
const onSaveToCollection = useMemoizedFn(async (collectionIds: string[]) => {
|
|
|
|
setSelectedCollections(collectionIds);
|
|
|
|
// const allSaves: Promise<void>[] = metricIds.map((metricId) => {
|
|
|
|
// return saveMetricToCollection({
|
|
|
|
// metricId,
|
|
|
|
// collectionIds
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// await Promise.all(allSaves);
|
|
|
|
openInfoMessage('Metrics saved to collections');
|
|
|
|
});
|
|
|
|
|
|
|
|
const onRemoveFromCollection = useMemoizedFn(async (collectionId: string) => {
|
|
|
|
setSelectedCollections((prev) => prev.filter((id) => id !== collectionId));
|
|
|
|
// const allSelectedButLast = selectedRowKeys.slice(0, -1);
|
|
|
|
// const lastMetricId = selectedRowKeys[selectedRowKeys.length - 1];
|
|
|
|
// const allRemoves: Promise<void>[] = allSelectedButLast.map((metricId) => {
|
|
|
|
// return removeMetricFromCollection({ metricId, collectionId, ignoreFavoriteUpdates: true });
|
|
|
|
// });
|
|
|
|
// await removeMetricFromCollection({
|
|
|
|
// metricId: lastMetricId,
|
|
|
|
// collectionId,
|
|
|
|
// ignoreFavoriteUpdates: false
|
|
|
|
// });
|
|
|
|
// await Promise.all(allRemoves);
|
|
|
|
openInfoMessage('Metrics removed from collections');
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SaveToCollectionsDropdown
|
|
|
|
onSaveToCollection={onSaveToCollection}
|
|
|
|
onRemoveFromCollection={onRemoveFromCollection}
|
|
|
|
selectedCollections={selectedCollections}>
|
|
|
|
<CollectionButton buttonType={buttonType} useText={useText} />
|
|
|
|
</SaveToCollectionsDropdown>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
SaveDashboardToCollectionButton.displayName = 'SaveDashboardToCollectionButton';
|