Main Application Page
Relevant source files
The following files were used as context for generating this wiki page:The Main Application Page, implemented in src/app/page.tsx, serves as the primary user interface and central navigation hub for the Medical Calculators application. It functions as a single-page application (SPA) where users can select and interact with various medical calculators without full page reloads. The page is responsible for rendering the main layout, including the header, footer, and a dynamic content area that displays either a home screen with calculator options or a specific, selected calculator.
Built with Next.js and React, the page utilizes a state-driven approach to manage which calculator is currently active. It dynamically imports and renders calculator components, making the system modular and extensible. The entire UI is styled with Tailwind CSS, featuring a responsive, dark-themed design. Internationalization is supported through a custom React Context, allowing UI text to be displayed in multiple languages.
Sources: src/app/page.tsx, src/app/layout.tsx, README.md
Architecture and Component Structure
The application's structure is defined by the Next.js App Router. The root layout (layout.tsx) wraps the main page component (page.tsx), providing a consistent structure, font loading, and global context providers.
Sources: src/app/layout.tsx:1-40, src/app/page.tsx:1-375
Component Hierarchy
The RootLayout component sets up the HTML shell, loads the Geist fonts, and wraps all children with the LanguageProvider for internationalization. The Home component from page.tsx is the main child, which in turn renders the header, main content area, and footer.
The following diagram illustrates the high-level component hierarchy:
Core Components
| Component | File | Description |
|---|---|---|
RootLayout | src/app/layout.tsx | The root layout for the entire application. It defines the <html> and <body> tags, applies global styles, loads fonts, and wraps the application in the LanguageProvider. |
Home | src/app/page.tsx | The main page component. It manages the active calculator state and renders the UI, including the header, footer, and the currently selected calculator. |
LanguageProvider | src/app/layout.tsx | A React Context provider that makes language translations available to all child components. |
LanguageSelector | src/app/page.tsx | A component within the header that allows the user to switch the application's language. |
MobileMenu | src/app/page.tsx | A slide-in menu for mobile navigation, providing access to all calculators. |
| Calculator Components | src/calculators/ | Individual calculator components (e.g., PediatricDoseCalculator, BloodGasAnalysisCalculator) that are dynamically rendered by the Home component. |
Sources: src/app/layout.tsx:5,24-37, src/app/page.tsx:4-11,13-15, src/calculators/PediatricDoseCalculator.tsx
State Management and Navigation
Navigation within the application is controlled by a single React state variable, activeCalculator, within the Home component. This design avoids traditional routing and keeps the user within a single page.
Sources: src/app/page.tsx:13-15,142-329
activeCalculator State
The activeCalculator state determines which view is rendered in the main content area. It is managed using the useState hook.
// src/app/page.tsx:12-13
type CalculatorType = 'home' | 'electrolyte' | 'antibiotic' | 'pediatric' | 'bloodGas' | 'infusionTherapy';
export default function Home() {
const [activeCalculator, setActiveCalculator] = useState<CalculatorType>('home');
// ...
}
The CalculatorType defines all possible states for the main view. The initial state is 'home', which displays a grid of available calculator cards.
Sources: src/app/page.tsx:12-15
Navigation Flow
When a user clicks on a navigation button in the header, a calculator card on the home screen, or an item in the mobile menu, an onClick event handler calls setActiveCalculator to update the state. This triggers a re-render, and the conditional rendering logic in the main element displays the corresponding component.
This diagram shows the user interaction flow for selecting a calculator:
UI and Layout
The UI is divided into a fixed header, a dynamic main content area, and a fixed footer. The layout is responsive, adapting to desktop and mobile screens.
Sources: src/app/page.tsx:18-373, src/app/globals.css
Header
The header contains the application title, which also functions as a "home" button, primary navigation links, and a language selector. On mobile devices, the navigation links are collapsed into a MobileMenu component, accessible via a hamburger icon.
Sources: src/app/page.tsx:20-140
Main Content
The <main> element's content is determined by the activeCalculator state.
- Home View (
activeCalculator === 'home'): Displays a welcome message and a grid of "cards". Each card represents a calculator and acts as a button to switch to that calculator's view. There is also a "Coming Soon" card for planned features.
Sources: src/app/page.tsx:144-263 - Calculator View (
activeCalculator !== 'home'): When a calculator is selected, this view is rendered. It includes a "Back to Calculators" button and the dynamically imported calculator component itself. Each calculator section has a title and description sourced from the i18ntranslations.tsfile.
Sources: src/app/page.tsx:265-329
The following table lists the calculators available from the main page:
CalculatorType State | Display Name (EN) | Component Rendered |
|---|---|---|
electrolyte | Electrolyte Correction | ElectrolyteCorrectionCalculator |
antibiotic | Wound Antibiotic Prophylaxis | AntibioticProphylaxisCalculator |
pediatric | Pediatric Drug Doses | PediatricDoseCalculator |
bloodGas | Анализ газов крови | BloodGasAnalysisCalculator |
infusionTherapy | Инфузионная терапия | InfusionTherapyCalculator |
Sources: src/app/page.tsx:4-9, 265-329, src/i18n/translations.ts:345, 351, 353
Footer
The footer is a simple banner at the bottom of the page containing copyright information and a medical disclaimer. The text is also managed via the i18n system.
Sources: src/app/page.tsx:364-372, src/i18n/translations.ts:47, 444-445
Styling and Theming
The application uses a dark theme defined with CSS custom properties in src/app/globals.css. Tailwind CSS is used for the utility-first styling approach.
The following CSS variables define the core color palette:
| Variable | Value | Description |
|---|---|---|
--background | #1a202c | Main page background color. |
--foreground | #ffffff | Primary text color. |
--card | #2d3748 | Background color for card elements. |
--border | #4a5568 | Color for borders and dividers. |
--input | #2d3748 | Background color for input fields. |
--primary | #63b3ed | Primary accent color for buttons and links. |
--secondary | #4a5568 | Secondary color for UI elements. |
--accent | #2c5282 | Accent color, often used for hover states or secondary backgrounds. |
--muted-foreground | #a0aec0 | Text color for muted or secondary information. |
Sources: src/app/globals.css:4-18
The root layout also loads the Geist and Geist_Mono fonts to be used throughout the application.
Sources: src/app/layout.tsx:6-15
Internationalization (i18n)
The application supports multiple languages (English, Russian, Ukrainian) through a custom i18n implementation.
LanguageProvider: This context provider, wrapped around the application insrc/app/layout.tsx, manages the current language and provides the translation function.useLanguageHook: Components likepage.tsxuse this hook to get the translation functiont.translations.ts: This file contains a large object with translation keys for all UI text in the supported languages.
Example of using the t function in page.tsx:
// src/app/page.tsx:15, 148
const { t } = useLanguage();
// ...
<h2 className="text-2xl md:text-4xl font-bold text-foreground mb-3 md:mb-4">
{t.welcome}
</h2>
Here, t.welcome retrieves the "Welcome to MDcalc" string in the currently selected language from the translations object.
Sources: src/app/layout.tsx:5, 34-36, src/app/page.tsx:15, 148, src/i18n/translations.ts:1-1250
Extensibility
The architecture of page.tsx is designed for easy extension. Adding a new calculator involves a few simple steps, as outlined in CONTRIBUTING.md.
The process for adding a new calculator to the main page is as follows:
- Create the Calculator Component: A new
.tsxfile is created insrc/calculators/. - Update
CalculatorType: The new calculator's identifier is added to theCalculatorTypeunion insrc/app/page.tsx. - Import the New Component: The new calculator component is imported at the top of
src/app/page.tsx. - Add Navigation: A new button is added to the desktop navigation, mobile menu, and a new card is added to the home screen grid. The
onClickhandler for these elements should set theactiveCalculatorstate to the new type. - Add Conditional Rendering: A new
ifblock is added to the main content area to render the new calculator component when its corresponding state is active.
This flow is visualized below: