Files
gridpilot.gg/apps/website/lib/currentDriver.ts
2025-12-10 15:41:44 +01:00

40 lines
1.3 KiB
TypeScript

'use client';
import { useAuth } from '@/lib/auth/AuthContext';
/**
* Returns the effective driver ID for the current session.
*
* Prefers the authenticated user's primaryDriverId when available,
* otherwise falls back to the demo default used across the alpha site.
*/
export function useEffectiveDriverId(): string {
const { session } = useAuth();
const user = session?.user as
| {
primaryDriverId?: string | null;
}
| undefined;
// In alpha mode, if the user has no bound driver yet, fall back to the
// first seeded driver from the in-memory repository instead of a hardcoded ID.
if (user?.primaryDriverId) {
return user.primaryDriverId;
}
try {
// Lazy-load to avoid importing DI facade at module evaluation time
const { getDriverRepository } = require('./di-container') as typeof import('./di-container');
const repo = getDriverRepository();
// In-memory repository is synchronous for findAll in the demo implementation
const allDrivers = repo.findAllSync?.() as Array<{ id: string }> | undefined;
if (allDrivers && allDrivers.length > 0) {
return allDrivers[0].id;
}
} catch {
// Ignore and fall back to legacy default below
}
// Legacy fallback: preserved only as a last resort for demo
return '';
}