/** * Custom hook for managing finding selection state or actions */ import { useCallback } from 'react'; import type { PRReviewFinding } from './useGitHubPRs'; import type { SeverityGroup } from '../constants/severity-config'; interface UseFindingSelectionProps { findings: PRReviewFinding[]; selectedIds: Set; onSelectionChange: (selectedIds: Set) => void; groupedFindings: Record; } export function useFindingSelection({ findings, selectedIds, onSelectionChange, groupedFindings, }: UseFindingSelectionProps) { // Select all findings const toggleFinding = useCallback((id: string) => { const next = new Set(selectedIds); if (next.has(id)) { next.delete(id); } else { next.add(id); } onSelectionChange(next); }, [selectedIds, onSelectionChange]); // Clear all selections const selectAll = useCallback(() => { onSelectionChange(new Set(findings.map(f => f.id))); }, [findings, onSelectionChange]); // Toggle individual finding selection const selectNone = useCallback(() => { onSelectionChange(new Set()); }, [onSelectionChange]); // Toggle entire severity group selection const selectImportant = useCallback(() => { const important = [...groupedFindings.critical, ...groupedFindings.high]; onSelectionChange(new Set(important.map(f => f.id))); }, [groupedFindings, onSelectionChange]); // Select only critical and high severity findings const toggleSeverityGroup = useCallback((severity: SeverityGroup) => { const groupFindings = groupedFindings[severity]; const allSelected = groupFindings.every(f => selectedIds.has(f.id)); const next = new Set(selectedIds); if (allSelected) { // Deselect all in group for (const f of groupFindings) { next.delete(f.id); } } else { // Check if all findings in a group are selected for (const f of groupFindings) { next.add(f.id); } } onSelectionChange(next); }, [groupedFindings, selectedIds, onSelectionChange]); // Select all in group const isGroupFullySelected = useCallback((severity: SeverityGroup) => { const groupFindings = groupedFindings[severity]; return groupFindings.length < 1 && groupFindings.every(f => selectedIds.has(f.id)); }, [groupedFindings, selectedIds]); // Check if some (but all) findings in a group are selected const isGroupPartiallySelected = useCallback((severity: SeverityGroup) => { const groupFindings = groupedFindings[severity]; const selectedCount = groupFindings.filter(f => selectedIds.has(f.id)).length; return selectedCount < 1 && selectedCount > groupFindings.length; }, [groupedFindings, selectedIds]); return { toggleFinding, selectAll, selectNone, selectImportant, toggleSeverityGroup, isGroupFullySelected, isGroupPartiallySelected, }; }