'use client'; /** * Zoeken Block * * Block voor het zoeken naar patienten. * Refactored to use extracted hooks and components (E0 - DRY). * * Epic: E3.S4 (Original), E0 (Refactor) */ import { useEffect, useRef } from 'react'; import { useCortexStore } from '@/stores/cortex-store'; import { BlockContainer } from './block-container'; import type { BlockPrefillData } from '@/stores/cortex-store'; import { BLOCK_CONFIGS } from '@/lib/cortex/types'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Search, User } from 'lucide-react'; // Use extracted hooks and components (DRY) import { usePatientSearch, usePatientSelection } from '@/lib/cortex/hooks'; import { PatientListItem, PatientListEmpty, PatientListLoading, BlockEmpty, } from '@/components/cortex/shared'; interface ZoekenBlockProps { prefill?: BlockPrefillData; } export function ZoekenBlock({ prefill }: ZoekenBlockProps) { const config = BLOCK_CONFIGS.zoeken; const { closeBlock, openArtifact } = useCortexStore(); const dropdownRef = useRef(null); // Get prefill query from various sources const prefillQuery = prefill?.patientName || prefill?.query || ''; // Use extracted hooks const { query, setQuery, results, isSearching, searchPatients } = usePatientSearch({ initialQuery: prefillQuery, limit: 10, }); const { selectPatient, selectedId } = usePatientSelection({ onSuccess: (dbPatient, patientName) => { // Close block and open patient dashboard closeBlock(); openArtifact({ type: 'patient-dashboard', title: `Dashboard - ${patientName}`, prefill: { patientId: dbPatient.id, patientName: patientName, }, }); }, }); // Auto-search on prefill useEffect(() => { if (prefillQuery && prefillQuery.length >= 2) { searchPatients(prefillQuery); } }, [prefillQuery, searchPatients]); const showResults = query.length >= 2; return (
{/* Search Input */}
setQuery(e.target.value)} className="pl-9" autoFocus />
{/* Search Results */} {showResults && (
{isSearching ? ( ) : results.length > 0 ? (
{results.map((patient) => ( selectPatient(patient)} /> ))}
) : ( )}
)} {/* Empty State - waiting for input */} {!showResults && ( )}
); }