Initial commit - Horux Despachos NL
This commit is contained in:
46
apps/web/lib/hooks/use-nav-gate.ts
Normal file
46
apps/web/lib/hooks/use-nav-gate.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
'use client';
|
||||
|
||||
import { useAuthStore } from '@/stores/auth-store';
|
||||
import { useSubscription } from './use-subscription';
|
||||
import { getSubscriptionState, isGlobalAdminRfc } from '@horux/shared';
|
||||
|
||||
/**
|
||||
* Hrefs siempre disponibles cuando la suscripción está vencida — el user puede
|
||||
* llegar a renovar y ajustar configuración. Todo lo demás se oculta del sidebar.
|
||||
*/
|
||||
const ALWAYS_ALLOWED_HREFS = new Set<string>([
|
||||
'/configuracion',
|
||||
'/configuracion/planes-despacho',
|
||||
'/configuracion/suscripcion',
|
||||
'/mis-empresas',
|
||||
]);
|
||||
|
||||
export function useNavGate() {
|
||||
const { user } = useAuthStore();
|
||||
const { data: subscription } = useSubscription(user?.tenantId);
|
||||
|
||||
const isGlobalAdmin = isGlobalAdminRfc(
|
||||
user?.tenantRfc,
|
||||
user?.role || 'visor',
|
||||
user?.platformRoles,
|
||||
);
|
||||
|
||||
// Admin global nunca queda bloqueado — opera vía impersonación de tenants.
|
||||
// Mientras la query de subscription está pending, NO bloqueamos para evitar
|
||||
// un flicker (sidebar completo → colapso → completo) en cada navegación.
|
||||
const needsRenewal = !isGlobalAdmin
|
||||
&& subscription !== undefined
|
||||
&& getSubscriptionState(subscription).needsRenewal;
|
||||
|
||||
const isAllowed = (href: string): boolean => {
|
||||
if (!needsRenewal) return true;
|
||||
// Permitir prefix-match para que rutas como /configuracion/usuarios/edit sigan
|
||||
// siendo accesibles si están bajo /configuracion (pero NO cualquier otro path).
|
||||
for (const allowed of ALWAYS_ALLOWED_HREFS) {
|
||||
if (href === allowed || href.startsWith(`${allowed}/`)) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return { needsRenewal, isAllowed };
|
||||
}
|
||||
Reference in New Issue
Block a user