2024-03-12 10:52:13 +00:00
|
|
|
import { UserPayload } from '@/types/user';
|
2024-03-28 00:34:41 +00:00
|
|
|
import { UserPermissions } from '@/types/permissions';
|
2024-03-28 07:17:33 +00:00
|
|
|
import { hasPermission } from './helpers/permissions';
|
2024-03-12 10:52:13 +00:00
|
|
|
|
|
|
|
export type UserOnly = boolean | keyof UserPayload;
|
|
|
|
|
2024-03-28 00:34:41 +00:00
|
|
|
export type Subroute = {
|
2024-03-12 10:52:13 +00:00
|
|
|
url: string,
|
|
|
|
name: string,
|
2024-03-28 00:34:41 +00:00
|
|
|
userOnly?: UserOnly,
|
|
|
|
permissions?: (UserPermissions | UserPermissions[])[],
|
|
|
|
routes?: Omit<Subroute, 'routes'>[]
|
2024-03-12 10:52:13 +00:00
|
|
|
};
|
|
|
|
|
2024-03-28 00:34:41 +00:00
|
|
|
export type Route = {
|
2024-03-12 10:52:13 +00:00
|
|
|
url: string,
|
|
|
|
name: string,
|
|
|
|
title: string,
|
|
|
|
userOnly?: UserOnly,
|
2024-03-28 00:34:41 +00:00
|
|
|
permissions?: (UserPermissions | UserPermissions[])[],
|
2024-03-12 10:52:13 +00:00
|
|
|
routes: Subroute[]
|
|
|
|
};
|
|
|
|
|
|
|
|
export const MAIN_ROUTES: Route = {
|
|
|
|
url: '/',
|
|
|
|
name: "Actaeon",
|
|
|
|
title: 'Actaeon',
|
|
|
|
routes: [{
|
|
|
|
url: '/dashboard',
|
|
|
|
name: 'Overview'
|
2024-03-25 01:47:52 +00:00
|
|
|
}, {
|
|
|
|
url: '/arcade',
|
|
|
|
name: 'Arcades'
|
2024-03-28 00:34:41 +00:00
|
|
|
}, {
|
|
|
|
url: '/admin',
|
|
|
|
name: 'Admin',
|
|
|
|
permissions: [[UserPermissions.USERMOD, UserPermissions.SYSADMIN]],
|
|
|
|
routes: [{
|
|
|
|
url: '/admin/users',
|
|
|
|
name: 'Users',
|
|
|
|
permissions: [UserPermissions.USERMOD]
|
|
|
|
}, {
|
|
|
|
url: '/admin/system-config',
|
|
|
|
name: 'System Config',
|
|
|
|
permissions: [UserPermissions.SYSADMIN]
|
|
|
|
}]
|
2024-03-12 10:52:13 +00:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ROUTES: Route[] = [{
|
|
|
|
url: '/chuni',
|
|
|
|
name: 'Chunithm',
|
|
|
|
title: 'Chunithm',
|
|
|
|
userOnly: 'chuni',
|
|
|
|
routes: [{
|
|
|
|
url: '/chuni/dashboard',
|
|
|
|
name: 'Dashboard',
|
|
|
|
userOnly: 'chuni'
|
|
|
|
}, {
|
|
|
|
url: '/chuni/music',
|
|
|
|
name: 'Music List'
|
|
|
|
}, {
|
|
|
|
url: '/chuni/playlog',
|
|
|
|
name: 'Playlog',
|
|
|
|
userOnly: 'chuni'
|
|
|
|
}, {
|
|
|
|
url: '/chuni/userbox',
|
|
|
|
name: 'Userbox',
|
|
|
|
userOnly: 'chuni'
|
|
|
|
}]
|
|
|
|
}, MAIN_ROUTES];
|
2024-03-28 07:17:33 +00:00
|
|
|
|
|
|
|
export const filterRoute = (user: UserPayload | null | undefined, { userOnly, permissions }: { userOnly?: UserOnly, permissions?: (UserPermissions | UserPermissions[])[]; }) => {
|
|
|
|
if (typeof userOnly === 'string' && !user?.[userOnly])
|
|
|
|
return false;
|
|
|
|
if (typeof userOnly === 'boolean' && !user)
|
|
|
|
return false;
|
|
|
|
if (permissions?.length && !hasPermission(user?.permissions, ...permissions))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getValidHomepageRoutes = (user: UserPayload) => {
|
|
|
|
const filter = filterRoute.bind(null, user);
|
|
|
|
return [MAIN_ROUTES, ...ROUTES.slice(0, -1)].filter(filter)
|
|
|
|
.map(({ name, routes }) => ({
|
|
|
|
name,
|
|
|
|
routes: routes.filter(filter)
|
|
|
|
.flatMap(r => [r, ...(r.routes?.filter(filter)
|
|
|
|
?.map(d => ({ ...d, name: `${r.name}┃${d.name}` })) ?? [])])
|
|
|
|
.filter(r => !r.url.startsWith('/admin'))
|
|
|
|
.map(({ name, url }) => ({ name, url }))
|
|
|
|
}));
|
|
|
|
};
|