buster/web/src/components/features/buttons/SaveDashboardToCollectionBu...

64 lines
2.2 KiB
TypeScript
Raw Normal View History

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-08 07:02:56 +08:00
import { useMemoizedFn } from '@/hooks';
2025-02-07 05:31:46 +08:00
import { useBusterNotifications } from '@/context/BusterNotifications';
2025-03-01 02:30:39 +08:00
import { CollectionButton } from './CollectionsButton';
2025-03-13 12:55:49 +08:00
import {
useAddDashboardToCollection,
useRemoveDashboardFromCollection
} from '@/api/buster_rest/dashboards';
2025-04-17 14:17:14 +08:00
import uniq from 'lodash/uniq';
2025-02-07 05:31:46 +08:00
export const SaveDashboardToCollectionButton: React.FC<{
dashboardIds: string[];
2025-03-08 07:02:56 +08:00
buttonType?: 'ghost' | 'default';
2025-02-07 05:31:46 +08:00
useText?: boolean;
2025-04-17 14:17:14 +08:00
selectedCollections: string[];
}> = React.memo(
({
dashboardIds,
buttonType = 'ghost',
useText = false,
selectedCollections: selectedCollectionsProp
}) => {
const { openInfoMessage } = useBusterNotifications();
const [selectedCollections, setSelectedCollections] =
useState<Parameters<typeof SaveToCollectionsDropdown>[0]['selectedCollections']>(
selectedCollectionsProp
);
const { mutateAsync: addDashboardToCollection } = useAddDashboardToCollection();
const { mutateAsync: removeDashboardFromCollection } = useRemoveDashboardFromCollection();
const onSaveToCollection = useMemoizedFn(async (collectionIds: string[]) => {
setSelectedCollections((prev) => uniq([...prev, ...collectionIds]));
await addDashboardToCollection({
dashboardIds,
collectionIds
});
openInfoMessage('Dashboards saved to collections');
2025-03-22 12:21:42 +08:00
});
2025-04-03 01:24:45 +08:00
2025-04-17 14:17:14 +08:00
const onRemoveFromCollection = useMemoizedFn(async (collectionId: string) => {
setSelectedCollections((prev) => prev.filter((id) => id !== collectionId));
await removeDashboardFromCollection({
dashboardIds,
collectionIds: [collectionId]
});
openInfoMessage('Dashboards removed from collections');
2025-03-22 12:21:42 +08:00
});
2025-02-07 05:31:46 +08:00
2025-04-17 14:17:14 +08:00
return (
<SaveToCollectionsDropdown
onSaveToCollection={onSaveToCollection}
onRemoveFromCollection={onRemoveFromCollection}
selectedCollections={selectedCollections}>
<CollectionButton buttonType={buttonType} useText={useText} />
</SaveToCollectionsDropdown>
);
}
);
2025-02-07 05:31:46 +08:00
SaveDashboardToCollectionButton.displayName = 'SaveDashboardToCollectionButton';