buster/web/src/controllers/MetricListContainer/MetricItemsSelectedPopup.tsx

193 lines
6.0 KiB
TypeScript
Raw Normal View History

import React, { useState } from 'react';
2025-02-20 10:53:49 +08:00
import { BusterListSelectedOptionPopupContainer } from '@/components/ui/list';
2025-03-13 04:42:54 +08:00
import { ShareAssetType, VerificationStatus } from '@/api/asset_interfaces';
import { useUserConfigContextSelector } from '@/context/Users';
2025-03-08 07:02:56 +08:00
import { useMemoizedFn } from '@/hooks';
2025-02-28 03:43:48 +08:00
import { SaveToCollectionsDropdown } from '@/components/features/dropdowns/SaveToCollectionsDropdown';
import { useBusterNotifications } from '@/context/BusterNotifications';
2025-03-08 07:02:56 +08:00
import { Button } from '@/components/ui/buttons';
import { ASSET_ICONS } from '@/components/features/config/assetIcons';
2025-04-09 23:54:40 +08:00
import { Dropdown } from '@/components/ui/dropdown';
2025-03-08 07:02:56 +08:00
import { StatusBadgeButton } from '@/components/features/metrics/StatusBadgeIndicator';
2025-04-09 23:54:40 +08:00
import { Dots, Trash } from '@/components/ui/icons';
2025-03-13 05:31:20 +08:00
import {
useDeleteMetric,
useRemoveMetricFromCollection,
2025-03-21 05:03:57 +08:00
useSaveMetricToCollections,
2025-03-14 00:11:57 +08:00
useUpdateMetric
2025-03-13 05:31:20 +08:00
} from '@/api/buster_rest/metrics';
2025-04-09 23:54:40 +08:00
import { useThreeDotFavoritesOptions } from '@/components/features/dropdowns/useThreeDotFavoritesOptions';
2025-02-02 13:17:37 +08:00
export const MetricSelectedOptionPopup: React.FC<{
selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void;
hasSelected: boolean;
2025-04-09 23:54:40 +08:00
}> = React.memo(({ selectedRowKeys, onSelectChange, hasSelected }) => {
return (
<BusterListSelectedOptionPopupContainer
selectedRowKeys={selectedRowKeys}
onSelectChange={onSelectChange}
buttons={[
<CollectionsButton
key="collections"
selectedRowKeys={selectedRowKeys}
onSelectChange={onSelectChange}
/>,
<DashboardButton
key="dashboard"
selectedRowKeys={selectedRowKeys}
onSelectChange={onSelectChange}
/>,
<StatusButton
key="status"
selectedRowKeys={selectedRowKeys}
onSelectChange={onSelectChange}
/>,
<DeleteButton
key="delete"
selectedRowKeys={selectedRowKeys}
onSelectChange={onSelectChange}
/>,
<ThreeDotButton
key="three-dot"
selectedRowKeys={selectedRowKeys}
onSelectChange={onSelectChange}
/>
]}
show={hasSelected}
/>
);
2025-04-09 23:54:40 +08:00
});
MetricSelectedOptionPopup.displayName = 'MetricSelectedOptionPopup';
const CollectionsButton: React.FC<{
selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => {
const { openInfoMessage } = useBusterNotifications();
2025-03-21 05:03:57 +08:00
const { mutateAsync: saveMetricToCollection } = useSaveMetricToCollections();
2025-03-13 05:31:20 +08:00
const { mutateAsync: removeMetricFromCollection } = useRemoveMetricFromCollection();
const [selectedCollections, setSelectedCollections] = useState<
Parameters<typeof SaveToCollectionsDropdown>[0]['selectedCollections']
>([]);
const onSaveToCollection = useMemoizedFn(async (collectionIds: string[]) => {
setSelectedCollections(collectionIds);
2025-03-22 12:21:42 +08:00
await saveMetricToCollection({
metricIds: selectedRowKeys,
collectionIds
});
openInfoMessage('Metrics saved to collections');
});
const onRemoveFromCollection = useMemoizedFn(async (collectionId: string) => {
setSelectedCollections((prev) => prev.filter((id) => id !== collectionId));
2025-03-22 12:21:42 +08:00
await removeMetricFromCollection({
metricIds: selectedRowKeys,
collectionIds: [collectionId]
});
openInfoMessage('Metrics removed from collections');
});
return (
<SaveToCollectionsDropdown
onSaveToCollection={onSaveToCollection}
onRemoveFromCollection={onRemoveFromCollection}
selectedCollections={selectedCollections}>
2025-03-08 07:02:56 +08:00
<Button prefix={<ASSET_ICONS.collections />}>Collections</Button>
</SaveToCollectionsDropdown>
);
};
const DashboardButton: React.FC<{
selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => {
return (
2025-03-08 07:02:56 +08:00
<Dropdown items={[{ label: 'Dashboard', value: 'dashboard' }]}>
<Button prefix={<ASSET_ICONS.dashboards />} type="button">
Dashboard
</Button>
</Dropdown>
);
};
const StatusButton: React.FC<{
selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => {
2025-04-04 01:02:17 +08:00
const { mutateAsync: updateMetric } = useUpdateMetric({
updateVersion: false,
saveToServer: true,
updateOnSave: true
});
2025-03-02 14:55:59 +08:00
const isAdmin = useUserConfigContextSelector((state) => state.isAdmin);
2025-03-02 14:54:44 +08:00
const onVerify = useMemoizedFn(async (d: { id: string; status: VerificationStatus }[]) => {
2025-03-14 00:11:57 +08:00
await Promise.all(
d.map(({ id, status }) => {
return updateMetric({
id,
status
});
})
);
2025-03-02 14:54:44 +08:00
onSelectChange([]);
});
return (
<StatusBadgeButton
2025-03-04 11:10:44 +08:00
status={VerificationStatus.NOT_REQUESTED}
id={selectedRowKeys}
2025-03-02 14:54:44 +08:00
onVerify={onVerify}
2025-03-02 14:55:59 +08:00
isAdmin={isAdmin}
/>
);
};
const DeleteButton: React.FC<{
selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void;
}> = ({ selectedRowKeys, onSelectChange }) => {
const { mutateAsync: deleteMetric } = useDeleteMetric();
const { openConfirmModal } = useBusterNotifications();
const onDeleteClick = async () => {
openConfirmModal({
2025-02-01 06:21:50 +08:00
title: 'Delete metric',
content: 'Are you sure you want to delete these metrics?',
onOk: async () => {
2025-02-16 12:35:57 +08:00
await deleteMetric({ ids: selectedRowKeys });
onSelectChange([]);
}
});
};
return (
2025-03-08 07:02:56 +08:00
<Button prefix={<Trash />} onClick={onDeleteClick}>
Delete
</Button>
);
};
const ThreeDotButton: React.FC<{
selectedRowKeys: string[];
onSelectChange: (selectedRowKeys: string[]) => void;
2025-04-09 23:54:40 +08:00
}> = React.memo(({ selectedRowKeys, onSelectChange }) => {
const dropdownOptions = useThreeDotFavoritesOptions({
itemIds: selectedRowKeys,
assetType: ShareAssetType.METRIC,
onFinish: () => onSelectChange([])
});
return (
2025-03-08 07:02:56 +08:00
<Dropdown items={dropdownOptions}>
<Button prefix={<Dots />} />
</Dropdown>
);
2025-04-09 23:54:40 +08:00
});
ThreeDotButton.displayName = 'ThreeDotButton';