Skip to main content

Coding Conventions

This page covers the coding standards and conventions used throughout the HostMetrics codebase. All contributors should follow these rules to maintain consistency.

File Naming

TypeConventionExample
ComponentsPascalCase.tsxStatCard.tsx, TripTable.tsx
HooksuseCamelCase.tsuseBookings.ts, useKPIs.ts
UtilitiescamelCase.tsdateUtils.ts, formatters.ts
Database modulescamelCase.ts or kebab-case.tspartners.ts, import-service.ts
Test filesName.test.tsx or name.test.tsStatCard.test.tsx, useKPIs.test.ts

Import Path Alias

All imports use the @/ alias which maps to src/:

Client vs Server Components

Next.js App Router defaults to server components. Add the 'use client' directive only for interactive components that need browser APIs, hooks, or event handlers:
Server components (no directive needed) are used for pages that only fetch and render data.

Conditional Class Merging

Use the cn() utility from @/lib/utils for conditional Tailwind classes. This wraps clsx and tailwind-merge:

TypeScript Strict Mode

The project uses TypeScript in strict mode. All new code must:
  • Define explicit types for function parameters and return values
  • Avoid any — use unknown with type narrowing when the type is truly unknown
  • Define interfaces/types in src/lib/db/schema/ for database entities
  • Use generics where appropriate for reusable utilities

Component Composition

Prefer composition over prop drilling. Pass children or render props instead of threading data through many layers:

State Management

HostMetrics does not use a global state library. State is managed with:
  • React hooks (useState, useReducer) for local component state
  • React Context (useAuth, etc.) for cross-cutting concerns like auth
  • Custom hooks (useKPIs, useBookings, etc.) for data fetching and business logic
  • Supabase Realtime for live data updates where needed

Database Access Rules

All database operations live in src/lib/db/. Every query must filter by user_id to enforce row-level data isolation:
Never call Supabase directly from components — always go through a src/lib/db/ module or a hook in src/hooks/.

Code Organization Summary

WhatWhere
Page routessrc/app/(dashboard)/ or src/app/(auth)/
React componentssrc/components/{feature}/
Base UI primitivessrc/components/ui/
Custom hookssrc/hooks/
Database operationssrc/lib/db/
Schema / typessrc/lib/db/schema/
Utility functionssrc/lib/
Testssrc/__tests__/

Error Handling

Database and API errors should be caught and surfaced with meaningful messages:
Hooks should expose an error state so components can render appropriate error UI.