feat(cortex): add Intake Blocks MVP for voice/text commands
Adds 4 new intents for intake workflow: - intake_status: shows completion percentage and section checklist - intake_navigeer: navigation to specific intake tabs - risico_query: displays risk assessments with severity indicators - diagnose_query: shows primary/secondary diagnoses New components: - IntakeStatusBlock, RisicoBlock, DiagnoseBlock - Shared block components (BlockLoading, BlockError, BlockEmpty, BlockSection, BlockItem, BlockFooter) - useBlockData and useIntakeContext hooks New API routes: - GET /api/cortex/intake/status - GET /api/cortex/intake/risico - GET /api/cortex/intake/diagnose Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,5 +4,10 @@
|
||||
* Reusable hooks for Cortex functionality.
|
||||
*/
|
||||
|
||||
// Patient hooks
|
||||
export { usePatientSearch, type PatientSearchResult } from './use-patient-search';
|
||||
export { usePatientSelection } from './use-patient-selection';
|
||||
|
||||
// Block hooks (E2)
|
||||
export { useBlockData } from './use-block-data';
|
||||
export { useIntakeContext, type IntakePrefillData } from './use-intake-context';
|
||||
|
||||
123
lib/cortex/hooks/use-block-data.ts
Normal file
123
lib/cortex/hooks/use-block-data.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
'use client';
|
||||
|
||||
/**
|
||||
* useBlockData Hook
|
||||
*
|
||||
* Generic data fetching hook for Cortex blocks.
|
||||
* Handles loading, error states, and provides refetch capability.
|
||||
*
|
||||
* Epic: E2.S1 - Custom Hooks
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
import { safeFetch, getErrorInfo } from '@/lib/cortex/error-handler';
|
||||
|
||||
// ============================================================================
|
||||
// Types
|
||||
// ============================================================================
|
||||
|
||||
interface UseBlockDataOptions<T> {
|
||||
/** API endpoint (relatief pad) */
|
||||
endpoint: string;
|
||||
/** Query parameters */
|
||||
params?: Record<string, string | undefined>;
|
||||
/** Of data moet worden opgehaald */
|
||||
enabled?: boolean;
|
||||
/** Callback bij error */
|
||||
onError?: (error: Error) => void;
|
||||
/** Operatie naam voor error messages */
|
||||
operationName?: string;
|
||||
}
|
||||
|
||||
interface UseBlockDataResult<T> {
|
||||
/** Opgehaalde data */
|
||||
data: T | null;
|
||||
/** Loading state */
|
||||
isLoading: boolean;
|
||||
/** Error message */
|
||||
error: string | null;
|
||||
/** Refetch functie */
|
||||
refetch: () => Promise<void>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Hook
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Generic data fetching hook for Cortex blocks.
|
||||
*
|
||||
* @example
|
||||
* const { data, isLoading, error, refetch } = useBlockData<RiskData>({
|
||||
* endpoint: `/api/cortex/intake/${intakeId}/risks`,
|
||||
* enabled: Boolean(intakeId),
|
||||
* operationName: 'Risico\'s laden',
|
||||
* });
|
||||
*/
|
||||
export function useBlockData<T>({
|
||||
endpoint,
|
||||
params,
|
||||
enabled = true,
|
||||
onError,
|
||||
operationName = 'Data laden',
|
||||
}: UseBlockDataOptions<T>): UseBlockDataResult<T> {
|
||||
const [data, setData] = useState<T | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(enabled);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const { toast } = useToast();
|
||||
|
||||
// Stringify params for dependency comparison
|
||||
const paramsKey = params ? JSON.stringify(params) : '';
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
if (!enabled) return;
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Build URL with params
|
||||
const url = new URL(endpoint, window.location.origin);
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
url.searchParams.set(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const response = await safeFetch(url.toString(), undefined, {
|
||||
operation: operationName,
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
setData(result);
|
||||
} catch (err) {
|
||||
const statusCode = (err as any)?.statusCode;
|
||||
const errorInfo = getErrorInfo(err, {
|
||||
operation: operationName,
|
||||
statusCode,
|
||||
});
|
||||
|
||||
setError(errorInfo.description);
|
||||
onError?.(err as Error);
|
||||
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: errorInfo.title,
|
||||
description: errorInfo.description,
|
||||
});
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [endpoint, paramsKey, enabled, onError, operationName, toast]);
|
||||
|
||||
useEffect(() => {
|
||||
if (enabled) {
|
||||
fetchData();
|
||||
}
|
||||
}, [fetchData, enabled]);
|
||||
|
||||
return { data, isLoading, error, refetch: fetchData };
|
||||
}
|
||||
85
lib/cortex/hooks/use-intake-context.ts
Normal file
85
lib/cortex/hooks/use-intake-context.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
'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 <BlockEmpty icon={User} message="Selecteer eerst een patiënt" />;
|
||||
* }
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
Reference in New Issue
Block a user