📌 Components Architecture & File Structure
This document explains how the component system works, file relationships, and architectural patterns in the PassItOn Admin Dashboard.
📊 Component Architecture Overview
📌 Directory Structure & Relationships
components/
├── auth/ # Authentication & authorization
├── dashboard/ # Dashboard-specific business logic
├── providers/ # React context & state management
└── ui/ # Base UI primitives (shadcn/ui)
Data Flow:
providers/ → auth/ → dashboard/ → ui/
🔐 Authentication Layer
🔐 AuthGuard (auth/auth-guard.tsx)
Purpose: Route protection and role-based access control
Connected Files:
lib/auth/permissions.ts- Role validation logichooks/use-organization.ts- Organization contexttypes/roles.types.ts- Role type definitions
How it works:
- Receives user from Clerk authentication
- Validates role against required permissions in
permissions.ts - Redirects unauthorized users to appropriate pages
- Wraps protected components/pages
Usage Pattern:
// In page.tsx files
<AuthGuard requiredRole="super_admin">
<AdminComponent />
</AuthGuard>📌 Provider Layer (State Management)
📌 ClientProvider (providers/client-provider.tsx)
Purpose: Client-side application context wrapper
Dependencies:
@clerk/nextjs- Authentication state@tanstack/react-query- Server state managementcomponents/ui/Toaster.tsx- Global notifications
File Relationships:
- Wraps entire app in
app/layout.tsx - Provides auth context to all child components
- Manages global UI state (toasts, loading states)
📌 SupabaseProvider (providers/supabase-provider.tsx)
Purpose: Database client context for all components
Connected Files:
lib/supabase/supabase-client.ts- Browser clientlib/supabase/supabase-server.ts- Server client- All components that need database access
Architecture Pattern:
- Creates Supabase client with Clerk session
- Provides authenticated database access
- Manages real-time subscriptions
- Handles connection state
📌 Dashboard Component Layer
📌 DashboardHeader (dashboard/dashboard-header.tsx)
Purpose: Main navigation and user context display
File Dependencies:
hooks/use-organization.ts- Current organization datalib/auth/permissions.ts- Role-based menu itemscomponents/ui/- UI primitives (Button, DropdownMenu)
Connected Components:
Sidebar.tsx- Works together for navigationAuthGuard.tsx- Receives user role for menu rendering
Data Flow:
- Gets current user from Clerk
- Fetches organization from
use-organizationhook - Renders navigation based on role permissions
- Communicates with sidebar for mobile menu state
📌 Sidebar (dashboard/sidebar.tsx)
Purpose: Main navigation menu with role-based items
Architecture Connections:
lib/auth/permissions.ts- Menu item visibility rulesapp/(dashboard)/*/page.tsx- Navigation targetshooks/use-organization.ts- Organization context
Role-Based Rendering:
- Super Admin: Links to
app/(dashboard)/admin/*pages - Owner/Editor: Links to
app/(dashboard)/dashboard/*pages
📌 WidgetCustomizer (dashboard/widget-customizer.tsx)
Purpose: Widget configuration interface
File Relationships:
hooks/use-widget.ts- Widget state managementtypes/widget.types.ts- Widget configuration typesapp/api/widgets/route.ts- Save configuration APIWidgetPreview.tsx- Real-time preview display
Data Architecture:
- Fetches current widget config via
use-widgethook - Updates local state for real-time preview
- Saves to database via
/api/widgetsendpoint - Syncs with preview component for live updates
📌 WidgetPreview (dashboard/widget-preview.tsx)
Purpose: Live widget rendering with current configuration
Dependencies:
WidgetCustomizer.tsx- Receives configuration changesapp/widget/[slug]/page.tsx- Embedded widget renderinghooks/use-widget.ts- Shared widget state
Rendering Flow:
- Receives config from customizer in real-time
- Applies styling and theme changes
- Simulates actual widget appearance
- Updates when configuration changes
📌 OrganizationSettings (dashboard/organization-settings.tsx)
Purpose: Organization management interface
Connected Systems:
hooks/use-organization.ts- Organization datahooks/use-stripe-connect.ts- Payment setupapp/api/organizations/[orgId]/route.ts- Update APIlib/stripe/connect.ts- Stripe integration
Integration Points:
- Manages organization profile data
- Handles Stripe Connect onboarding
- Team member invitation system
- Subscription management
📌 UI Component Layer
📌 Base Components (ui/)
Purpose: Reusable UI primitives built on Radix UI
Architecture Pattern:
- Built with
@radix-ui/*for accessibility - Styled with Tailwind CSS
- Consistent API patterns across components
- Used by all higher-level components
Key Relationships:
Button.tsx- Used in all interactive componentsCard.tsx- Layout wrapper for dashboard sectionsToast.tsx+use-toast.tsx- Global notification systemInput.tsx,Select.tsx- Form components in settings
📌 Hook Integration Patterns
📌 State Management Hooks
File Locations: hooks/
Architecture Connections:
use-organization.ts→ Multiple dashboard componentsuse-widget.ts→ Widget customizer/preview componentsuse-stripe-connect.ts→ Organization settingsuse-notifications.ts→ Dashboard header, notification components
Data Flow Pattern:
- Hooks fetch data from Supabase via providers
- Components consume hook data via React context
- State updates trigger re-renders across connected components
- API mutations update both local and server state
🔌 API Integration Architecture
🔌 Component → API → Database Flow
Pattern:
Component → Hook → API Route → Supabase → Database
↓ ↓ ↓ ↓ ↓
UI State → Local → Server → Client → PostgreSQL
Example: Widget Configuration
WidgetCustomizerupdates local stateuse-widgethook manages state and API callsapp/api/widgets/route.tsvalidates and saves- Supabase client updates database
- Real-time updates reflect across all components
🔐 Authentication Flow Integration
🔒 Component Security Architecture
Flow:
Page Request → Middleware → AuthGuard → Component → API
↓ ↓ ↓ ↓ ↓
Route Check → Auth Check → Role Check → Render → Data Access
File Integration:
middleware.ts- Route-level protectionAuthGuard.tsx- Component-level protectionlib/auth/permissions.ts- Role validationapp/api/*/route.ts- API-level authorization
📌 Multi-Tenant Architecture
📌 Organization Context Flow
Components → Organization Relationship:
SupabaseProviderestablishes user sessionuse-organizationhook determines current organizationAuthGuardvalidates user's role in organization- Dashboard components filter data by organization ID
- API routes enforce organization-scoped data access
Database Relationships:
- Users belong to Organizations (foreign key)
- Widgets belong to Organizations
- All data is organization-scoped via RLS policies
📌 Development Patterns
📌 Adding New Components
Architecture Requirements:
- Location: Choose appropriate layer (auth/dashboard/ui/providers)
- Dependencies: Import from lower layers only (ui → dashboard → auth → providers)
- State: Use appropriate hooks for data management
- Types: Define in
types/directory with proper relationships - API: Create corresponding API routes if needed
📌 Component Communication Patterns
Parent-Child: Props and callbacks Sibling: Shared hooks and context Global: Zustand store and React context Server: API routes with optimistic updates
This architecture ensures maintainable, scalable component relationships with clear separation of concerns.