mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-04 11:17:02 +02:00
Adds a warning when about to archive features that have lastSeenAt of less than a week (green usage) Closes # [1-224](https://linear.app/unleash/issue/1-1224/bulk-edit-show-last-seen-usage-warning)  --------- Signed-off-by: andreas-unleash <andreas@getunleash.ai>
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { useMemo, useState, VFC } from 'react';
|
|
import { Button } from '@mui/material';
|
|
import { PermissionHOC } from 'component/common/PermissionHOC/PermissionHOC';
|
|
import { DELETE_FEATURE } from 'component/providers/AccessProvider/permissions';
|
|
import useProject from 'hooks/api/getters/useProject/useProject';
|
|
import { FeatureArchiveDialog } from 'component/common/FeatureArchiveDialog/FeatureArchiveDialog';
|
|
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
|
|
import { FeatureSchema } from 'openapi';
|
|
import { addDays, isBefore } from 'date-fns';
|
|
|
|
interface IArchiveButtonProps {
|
|
projectId: string;
|
|
featureIds: string[];
|
|
features: FeatureSchema[];
|
|
}
|
|
|
|
const DEFAULT_USAGE_THRESHOLD_DAYS = 7;
|
|
|
|
const isFeatureInUse = (feature?: FeatureSchema): boolean => {
|
|
const aWeekAgo = addDays(new Date(), -DEFAULT_USAGE_THRESHOLD_DAYS);
|
|
return !!(
|
|
feature &&
|
|
feature.lastSeenAt &&
|
|
isBefore(new Date(feature.lastSeenAt), aWeekAgo)
|
|
);
|
|
};
|
|
|
|
export const ArchiveButton: VFC<IArchiveButtonProps> = ({
|
|
projectId,
|
|
featureIds,
|
|
features,
|
|
}) => {
|
|
const { refetch } = useProject(projectId);
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
|
const { trackEvent } = usePlausibleTracker();
|
|
|
|
const featuresWithUsage = useMemo(() => {
|
|
return featureIds.filter(name => {
|
|
const feature = features.find(f => f.name === name);
|
|
return isFeatureInUse(feature);
|
|
});
|
|
}, [JSON.stringify(features), featureIds]);
|
|
|
|
const onConfirm = async () => {
|
|
setIsDialogOpen(false);
|
|
await refetch();
|
|
trackEvent('batch_operations', {
|
|
props: {
|
|
eventType: 'features archived',
|
|
},
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<PermissionHOC projectId={projectId} permission={DELETE_FEATURE}>
|
|
{({ hasAccess }) => (
|
|
<Button
|
|
disabled={!hasAccess || isDialogOpen}
|
|
variant="outlined"
|
|
size="small"
|
|
onClick={() => setIsDialogOpen(true)}
|
|
>
|
|
Archive
|
|
</Button>
|
|
)}
|
|
</PermissionHOC>
|
|
<FeatureArchiveDialog
|
|
projectId={projectId}
|
|
featureIds={featureIds}
|
|
featuresWithUsage={featuresWithUsage}
|
|
onConfirm={onConfirm}
|
|
isOpen={isDialogOpen}
|
|
onClose={() => setIsDialogOpen(false)}
|
|
/>
|
|
</>
|
|
);
|
|
};
|