Adding New Medical Data
Relevant source files
The following files were used as context for generating this wiki page:
This document outlines the process for adding new medical data to the Medical Calculators application. This includes adding entirely new calculators, expanding existing datasets like pediatric medications, and ensuring all user-facing text is properly internationalized. The architecture supports both dedicated data files and data structures embedded within calculator components.
The project structure separates concerns, with data-centric logic residing in src/data/, calculator components in src/calculators/, and internationalization strings in src/i18n/. Following these established patterns is crucial for maintaining code quality and medical accuracy.
Data Architecture Overview
Medical data in the application is primarily managed in two ways: as dedicated data arrays/objects or as internal state and constants within a specific calculator component.
- External Data Files: For large, reusable datasets, data is defined in TypeScript files within the
src/data/directory. ThePediatricDoseCalculatoris a key example, consuming a database of medications fromsrc/data/pediatricMedications.ts. - Embedded Data Structures: For calculators with complex, self-contained logic, data structures, and constants are often defined directly within the component file in
src/calculators/. TheBloodGasAnalysisCalculatorexemplifies this approach with its internalBloodGasDatainterface andNORMAL_VALUESconstants.
The following diagram illustrates the relationship between data sources, internationalization, and the UI components.
This diagram shows how calculator components consume both dedicated data files and internationalization strings.
Sources: src/calculators/PediatricDoseCalculator.tsx:11, src/calculators/BloodGasAnalysisCalculator.tsx:8, 92, src/i18n/translations.ts
Adding a New Calculator
The process for adding a new calculator involves creating new data types, building a React component, and integrating it into the main application page. This workflow is standardized to ensure consistency across the application.
Sources: CONTRIBUTING.md
New Calculator Workflow
The general flow for creating a new calculator is as follows:
This flowchart shows the step-by-step process for adding a new calculator, from type definition to UI integration.
Sources: CONTRIBUTING.md:3-82
Step 1: Define Data Structures
First, define the Inputs and Result TypeScript interfaces for the new calculator in src/types/index.ts.
export interface NewCalculatorInputs {
// Define input parameters
parameter1: number;
parameter2: string;
// ...
}
export interface NewCalculatorResult {
// Define calculation result
result1: number;
result2: string;
// ...
}
Sources: CONTRIBUTING.md:6-18
Alternatively, for complex or highly specific calculators, these interfaces can be defined directly within the calculator's .tsx file, as seen in the BloodGasAnalysisCalculator.
BloodGasAnalysisCalculator Data Interfaces:
| Interface | Purpose |
|---|---|
BloodGasData | Defines the structure for all input values, including pH, pCO2, electrolytes, and advanced parameters like hemoglobin and glucose. |
BloodGasResult | Defines the structure for all calculated outputs, such as anion gap, base excess, and a detailed interpretation object. |
Sources: src/calculators/BloodGasAnalysisCalculator.tsx:6-40, 42-78
Step 2: Create the Calculator Component
Create a new React component file in src/calculators/. This component will manage its own state for inputs and results, contain the calculation logic, and render the UI using shared components like Card, NumberInput, and Button.
// src/calculators/NewCalculator.tsx
'use client';
import React, { useState } from 'react';
import { NewCalculatorInputs, NewCalculatorResult } from '@/types';
// ... other imports
export const NewCalculator: React.FC = () => {
const [inputs, setInputs] = useState<NewCalculatorInputs>({ /* initial values */ });
const [result, setResult] = useState<NewCalculatorResult | null>(null);
const calculate = () => {
// Calculation logic here
};
const reset = () => {
// Reset logic here
};
return (
// JSX for the calculator UI
);
};
Sources: CONTRIBUTING.md:21-65, src/calculators/PediatricDoseCalculator.tsx:13-20
Adding to Existing Data Sets
To enhance existing calculators, you may need to add new data to their source files. The PediatricDoseCalculator is a prime example.
Adding a Pediatric Medication
New medications can be added to the pediatricMedicationsDatabase array located in src/data/pediatricMedications.ts. Each medication is an object that must conform to the PediatricMedication interface.
The structure of a medication object is inferred from its usage in PediatricDoseCalculator.tsx.
PediatricMedication Data Structure:
| Field | Type | Description |
|---|---|---|
name | string | The name of the medication. |
category | string | The therapeutic category (e.g., "Analgesics", "Antibiotics"). |
minAge | number | Minimum age in years for which the medication is indicated. |
minWeight | number | Minimum weight in kg for which the medication is indicated. |
dosingMethods | DosingMethod[] | An array of dosing methods (e.g., by weight, age, BSA). |
warnings | string[] | Important warnings associated with the medication. |
Sources: src/calculators/PediatricDoseCalculator.tsx:11, 48-56, 281
The PediatricDoseCalculator component uses this database to provide autocomplete suggestions and perform dose calculations.
The following diagram shows how the user interaction of searching for a medication filters the database and updates the UI.
This sequence illustrates the real-time filtering of the medication database as the user types.
Sources: src/calculators/PediatricDoseCalculator.tsx:343-366
Internationalization (i18n) of Data
All user-facing strings, including calculator titles, field labels, results, and medication categories, must be added to the translations object in src/i18n/translations.ts. This is essential for supporting multiple languages.
The system defines a Translations interface and a translations object containing keys for each supported language (en, ru, uk).
Translations Structure
export type Language = 'en' | 'ru' | 'uk';
export interface Translations {
// Navigation
home: string;
potassium: string;
// Calculator titles and descriptions
pediatricDoses: string;
pediatricDosesDesc: string;
// Pediatric calculator specific
medicationName: string;
childAge: string;
recommendedDose: string;
// ... more keys
}
export const translations: Record<Language, Translations> = {
en: { /* ... English translations ... */ },
ru: { /* ... Russian translations ... */ },
uk: { /* ... Ukrainian translations ... */ },
};
Sources: src/i18n/translations.ts:1-2, 13-14, 113-115, 330-332
When adding a new calculator or new data fields, you must:
- Add a new key to the
Translationsinterface. - Provide the translation for this key for all supported languages (
en,ru,uk) within thetranslationsobject.
For example, adding a new "Infusion Therapy" calculator would require adding keys like infusionTherapy and infusionTherapyDesc to the interface and all language objects.
Conclusion
Adding new medical data is a structured process that ensures consistency, accuracy, and localization. Developers must determine whether to embed data within a component or create a dedicated data file. Key steps include defining data structures, implementing calculation logic, adding all user-facing text to the internationalization file, and integrating the new data or calculator into the main application. Adherence to these guidelines, especially regarding medical accuracy and source documentation, is paramount.
Sources: CONTRIBUTING.md, PROJECT_SUMMARY.md