47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
'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 };
|
|
}
|