How to get FileRouteByTo
from beforeLoad context.matches
#3845
-
I have the following use case: My code looks something like:
export const Route = createFileRoute('/teams/$teamId')({
beforeLoad: async c => {
// ...
await redirectIfUnauthorized(
c.context.states.team,
c.matches.map(m => m.???),
);
},
});
export const redirectIfUnauthorized = async (
teamState: TeamState,
routeMatches: (keyof FileRoutesByTo)[],
): Promise<void> => {
if (!teamState.team) {
throw redirect({to: '/teams'});
}
if (
routeMatches.some(r => adminRoutes.includes(r)) &&
teamState.team.role !== Role.Admin
) {
throw redirect({
to: '/teams/$teamId/workspaces',
params: {teamId: `${teamState.team.id}`},
});
}
};
export const adminRoutes: (keyof FileRoutesByTo)[] = [
'/teams/$teamId/x',
'/teams/$teamId/y',
'/teams/$teamId/z',
];
export const Navbar = (): JSX.Element => {
const {teamId} = useParams({from: '/teams/$teamId'});
const {teamState} = useStates();
return (
<NavigationMenu>
<NavigationMenuList>
{createNavbarItems()
.filter(
({route}) =>
!adminRoutes.includes(route) ||
teamState.team.role === Role.Admin,
)
.map(({icon, label, route}) => (
<NavItem
key={route}
icon={icon}
label={label}
route={route}
teamId={teamId}
/>
))}
</NavigationMenuList>
</NavigationMenu>
);
}; I want to have one list of admin routes as the source of truth for both the navbar item rendering and the redirect logic. Currently I have this list typed as What is my best option here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
you should be able to convert paths and ids via |
Beta Was this translation helpful? Give feedback.
you should be able to convert paths and ids via
router.routesById
androuter.routesByPath