51 lines
1.6 KiB
TypeScript
51 lines
1.6 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();
|
|
|
|
interface DriverRepositoryWithSyncFindAll {
|
|
findAllSync?: () => Array<{ id: string }>;
|
|
}
|
|
|
|
// In alpha/demo mode the in-memory repository exposes a synchronous finder;
|
|
// access it via a safe dynamic lookup to keep typing compatible with the port.
|
|
const repoWithSync = repo as DriverRepositoryWithSyncFindAll;
|
|
const allDrivers = repoWithSync.findAllSync?.();
|
|
if (Array.isArray(allDrivers) && allDrivers.length > 0) {
|
|
const firstDriver = allDrivers[0];
|
|
if (firstDriver) {
|
|
return firstDriver.id;
|
|
}
|
|
}
|
|
} catch {
|
|
// Ignore and fall back to legacy default below
|
|
}
|
|
|
|
// Legacy fallback: preserved only as a last resort for demo
|
|
return '';
|
|
} |