This document outlines the new architecture using React Router v7 and TanStack React Query for improved state management and routing.
- React 19 - Latest React with improved performance
- TypeScript - Type safety and better DX
- React Router v7 - Modern routing with loader patterns
- TanStack React Query - Server state management
- TailwindCSS 4 - Utility-first CSS framework
- Vite - Fast build tool and dev server
- Go - High-performance backend
- Gin - Web framework
- NATS Go Client - NATS server integration
src/
├── components/ # Reusable UI components
│ ├── ui/ # Base UI components (shadcn/ui)
│ ├── layouts/ # Layout components
│ ├── ConnectionForm.tsx
│ ├── ProtectedRoute.tsx
│ ├── ErrorBoundary.tsx
│ └── DevTools.tsx
├── pages/ # Page components
│ ├── LoginPage.tsx
│ └── DashboardPage.tsx
├── hooks/ # Custom React hooks
│ └── useNATS.ts # NATS-related hooks
├── lib/ # Utility libraries
│ ├── api.ts # API client functions
│ └── utils.ts # General utilities
├── providers/ # Context providers
│ └── QueryProvider.tsx
├── router/ # Router configuration
│ └── index.tsx
├── App.tsx # Root App component
└── main.tsx # Application entry point
All queries use structured query keys for better cache management:
export const queryKeys = {
nats: {
all: ['nats'] as const,
status: () => [...queryKeys.nats.all, 'status'] as const,
info: () => [...queryKeys.nats.all, 'info'] as const,
account: () => [...queryKeys.nats.all, 'account'] as const,
},
};- Checks NATS connection status
- Auto-refreshes every 5 seconds
- Used for route protection
- Fetches NATS server information
- Only runs when connected
- Auto-refreshes every 5 seconds
- Fetches account information
- Only runs when connected
- Auto-refreshes every 5 seconds
- Mutation for connecting to NATS
- Automatically invalidates related queries on success
- Handles connection errors
- Mutation for disconnecting from NATS
- Updates cache immediately
- Handles cleanup
/ (RootLayout)
├── / (LoginPage) - Connection form
└── /dashboard (DashboardPage) - Protected dashboard
Routes are protected using the ProtectedRoute component:
{
path: 'dashboard',
element: (
<ProtectedRoute>
<DashboardPage />
</ProtectedRoute>
),
}- User starts at
/(LoginPage) - On successful connection, redirects to
/dashboard - Dashboard is protected - redirects to
/if not connected - On disconnect, returns to
/
All API calls are type-safe with proper error handling:
// Connection with credentials
const result = await natsApi.connect(credentials);
// Get server info
const info = await natsApi.getInfo();
// Check status
const status = await natsApi.getStatus();- Custom
ApiErrorclass for structured error handling - Automatic retry logic for transient errors
- No retry for 4xx client errors
- React Query error boundaries
-
Authentication Flow
LoginPage → useConnectToNATS → API → Success → Navigate to Dashboard -
Dashboard Data Flow
DashboardPage → useNATSInfo/useAccountInfo → Auto-refresh every 5s -
Disconnection Flow
Dashboard → useDisconnectFromNATS → API → Navigate to Login
- Modern routing patterns with better type safety
- Automatic code splitting for better performance
- Error boundaries for graceful error handling
- Protected routes with clean patterns
- Automatic background refetching keeps data fresh
- Intelligent caching reduces unnecessary API calls
- Optimistic updates for better UX
- Loading and error states handled automatically
- DevTools integration for debugging
- End-to-end TypeScript from API to UI
- Structured error handling with proper types
- IntelliSense support throughout the codebase
-
Start NATS Server
docker-compose up -d nats
-
Start Backend
go run . -
Start Frontend
cd client && pnpm run dev
-
Access Application
- Frontend: http://localhost:5173
- Backend API: http://localhost:5000/api
- React Query DevTools: Available in development
- Real-time updates with automatic polling
- Offline-first with intelligent caching
- Type-safe API calls and responses
- Error boundaries for graceful error handling
- Loading states handled automatically
- Optimistic updates for mutations
- Development tools for debugging
- Query deduplication - Multiple components requesting same data share a single request
- Background refetching - Data stays fresh without user intervention
- Intelligent retries - Automatic retry for network errors, no retry for client errors
- Cache invalidation - Proper cache management for data consistency
- Code splitting - Routes loaded on demand
This architecture provides a solid foundation for building complex NATS management features with excellent developer experience and user performance.