Home
Relevant source files
The following files were used as context for generating this wiki page:
The Home component, defined in src/app/page.tsx, serves as the main entry point and central navigation hub for the Medical Calculators web application. It functions as a single-page application controller, dynamically rendering different calculator components based on user interaction. The entire application is built using Next.js 14 with the App Router, React, and TypeScript.
The primary responsibility of the Home component is to manage the application's view state, displaying either a welcome screen with a selection of calculator cards or a specific, active calculator module.
Sources: src/app/page.tsx, README.md:1, 19-22, PROJECT_SUMMARY.md:3-7
Architecture and Core Components
The application's navigation and content display are controlled by a single React state variable within the Home component. This approach simulates a multi-page application experience while remaining on a single page.
Sources: src/app/page.tsx:13-15
State Management
The core of the component's logic is the activeCalculator state, which determines which view is currently visible to the user.
// src/app/page.tsx:11, 14
type CalculatorType = 'home' | 'electrolyte' | 'antibiotic' | 'pediatric' | 'bloodGas' | 'infusionTherapy';
// ...
const [activeCalculator, setActiveCalculator] = useState<CalculatorType>('home');
The CalculatorType enum defines all possible views. The activeCalculator state is initialized to 'home', which displays the main welcome screen. User actions, such as clicking a navigation link or a calculator card, call setActiveCalculator to update this state and trigger a re-render with the new component.
Sources: src/app/page.tsx:11-14
Component Rendering Flow
The main content area uses conditional rendering to display the appropriate component based on the activeCalculator state.
The following diagram illustrates the rendering logic managed by the Home component.
Sources: src/app/page.tsx:115-268
Page Structure
The page is structured into three main parts: a header, the main content area, and a footer. The overall HTML structure, metadata, and font loading are defined in the root layout.
Sources: src/app/page.tsx:17-285, src/app/layout.tsx
Root Layout
The root layout at src/app/layout.tsx sets up the foundational structure for the entire application.
- Metadata: Defines the page title and description.
- Fonts: Loads and applies the
GeistandGeist_Monofonts. - Providers: Wraps all child components with the
LanguageProviderto enable internationalization across the application.
Sources: src/app/layout.tsx:1-33
Header
The header contains the primary navigation elements:
- A main "MDcalc" button that resets the view to the home screen.
- Desktop navigation links for each calculator.
- A
LanguageSelectorcomponent. - A mobile menu for smaller viewports.
Each navigation button has an onClick handler that updates the activeCalculator state.
Sources: src/app/page.tsx:20-112
Main Content
This section is dynamic.
- If
activeCalculator === 'home', it displays a welcome message and a grid of calculator cards. - If
activeCalculatoris set to a specific calculator type, it renders the corresponding component (e.g.,<PediatricDoseCalculator />). Each calculator component is wrapped in adivthat includes a "Back to Calculators" button.
Sources: src/app/page.tsx:115-268
Footer
The footer is a static section at the bottom of the page containing copyright information and a medical disclaimer. The text for these elements is sourced from the internationalization context.
Sources: src/app/page.tsx:271-279, src/i18n/translations.ts:27, 230, 429
Navigation and State Transitions
User navigation is handled by updating the activeCalculator state. This provides a fast, client-side routing experience.
The diagram below shows the sequence of events when a user selects a calculator.
Sources: src/app/page.tsx:14, 178, 230-239
Available Calculators
The Home component can render the following calculator modules:
CalculatorType Value | Rendered Component | UI Text Key |
|---|---|---|
home | Welcome Section & Cards | t.home |
electrolyte | ElectrolyteCorrectionCalculator | t.electrolyteCorrection |
antibiotic | AntibioticProphylaxisCalculator | t.antibioticProphylaxis |
pediatric | PediatricDoseCalculator | t.pediatricDoses |
bloodGas | BloodGasAnalysisCalculator | "Анализ газов крови" |
infusionTherapy | InfusionTherapyCalculator | "Инфузионная терапия" |
Sources: src/app/page.tsx:11, 117-268, src/i18n/translations.ts
Internationalization (i18n)
The application supports multiple languages (en, ru, uk). All display text is managed through a custom useLanguage hook, which provides a translation function t. The text strings are defined in src/i18n/translations.ts. The LanguageProvider in the root layout makes the context available to all components.
// src/app/page.tsx:15
const { t } = useLanguage();
// src/app/page.tsx:121
<h2 className="text-2xl md:text-4xl font-bold text-foreground mb-3 md:mb-4">
{t.welcome}
</h2>
Sources: src/app/page.tsx:15, 102, 121, src/i18n/translations.ts:1-1430, src/app/layout.tsx:28-30
Adding a New Calculator
The Home component is the central integration point for adding new calculators. The process is documented in CONTRIBUTING.md and involves modifying src/app/page.tsx to include the new module.
The general workflow is as follows:
- Create Component: Build the new calculator logic and UI in its own file (e.g.,
src/calculators/NewCalculator.tsx). - Update Type: Add the new calculator's identifier to the
CalculatorTypeenum insrc/app/page.tsx. - Import Component: Import the new calculator component into
src/app/page.tsx. - Add Navigation: Add a new button to the header navigation bar.
- Add Card: Add a new card to the home screen grid.
- Add Render Logic: Add a new
ifblock to the main content area to conditionally render the new calculator.
This flow is illustrated in the diagram below.
Sources: CONTRIBUTING.md:5-96