'use client'; /** * useIntakeContext Hook * * Provides patient and intake context for Cortex blocks. * Falls back to activePatient from store when prefill is not provided. * * Epic: E2.S2 - Custom Hooks */ import { useMemo } from 'react'; import { useCortexStore } from '@/stores/cortex-store'; import type { BlockPrefillData } from '@/stores/cortex-store'; import { formatPatientName } from '@/lib/fhir/patient-mapper'; // ============================================================================ // Types // ============================================================================ /** * Extended prefill data with intake-specific fields. * Blocks can pass this to get intake context. */ export interface IntakePrefillData extends BlockPrefillData { /** Intake ID for intake-specific operations */ intakeId?: string; } interface UseIntakeContextResult { /** Patient ID (van prefill of activePatient) */ patientId: string | null; /** Intake ID (van prefill) */ intakeId: string | null; /** Patient naam (voor display) */ patientName: string | null; /** Of er patient context is */ hasPatientContext: boolean; /** Of er intake context is */ hasIntakeContext: boolean; } // ============================================================================ // Hook // ============================================================================ /** * Hook for getting patient and intake context in Cortex blocks. * * Priority: * 1. Explicit prefill data (from intent classification) * 2. activePatient from store (fallback) * * @example * const { patientId, intakeId, hasPatientContext } = useIntakeContext(prefill); * * if (!hasPatientContext) { * return ; * } */ export function useIntakeContext( prefill?: IntakePrefillData ): UseIntakeContextResult { const { activePatient } = useCortexStore(); return useMemo(() => { // Patient context: prefill takes priority, then activePatient const patientId = prefill?.patientId || activePatient?.id || null; const patientName = prefill?.patientName || (activePatient ? formatPatientName(activePatient) : null); // Intake context: only from prefill for now // TODO: Add activeIntake to cortex-store for persistent intake context const intakeId = prefill?.intakeId || null; return { patientId, intakeId, patientName, hasPatientContext: Boolean(patientId), hasIntakeContext: Boolean(intakeId), }; }, [prefill, activePatient]); }