2024-04-08 08:26:01 -04:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2024-01-30 09:13:17 -05:00
|
|
|
import { NavLink, Route, Switch, useRouteMatch } from 'react-router-dom';
|
|
|
|
import TransitionRouter from '@/TransitionRouter';
|
|
|
|
import PermissionRoute from '@/components/elements/PermissionRoute';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import { NotFound } from '@/components/elements/ScreenBlock';
|
|
|
|
import { useLocation } from 'react-router';
|
2024-02-24 15:52:53 -05:00
|
|
|
import { useStoreState } from 'easy-peasy';
|
2024-03-09 12:36:16 -05:00
|
|
|
import { ServerContext } from '@/state/server';
|
2024-01-30 09:13:17 -05:00
|
|
|
|
|
|
|
import routes from '@/routers/routes';
|
|
|
|
import blueprintRoutes from './routes';
|
|
|
|
|
2024-04-08 08:26:01 -04:00
|
|
|
const blueprintExtensions = [...new Set(blueprintRoutes.server.map((route) => route.identifier))];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the route egg IDs for each extension with server routes.
|
|
|
|
*/
|
|
|
|
const useExtensionEggs = () => {
|
|
|
|
const [extensionEggs, setExtensionEggs] = useState<{ [x: string]: string[] }>(
|
|
|
|
blueprintExtensions.reduce((prev, current) => ({ ...prev, [current]: ['-1'] }), {})
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const newEggs: { [x: string]: string[] } = {};
|
|
|
|
for (const id of blueprintExtensions) {
|
|
|
|
const resp = await fetch(`/api/client/extensions/blueprint/eggs?${new URLSearchParams({ id })}`);
|
|
|
|
newEggs[id] = (await resp.json()) as string[];
|
|
|
|
}
|
|
|
|
setExtensionEggs(newEggs);
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return extensionEggs;
|
|
|
|
};
|
|
|
|
|
2024-01-30 09:13:17 -05:00
|
|
|
export const NavigationLinks = () => {
|
2024-02-24 15:52:53 -05:00
|
|
|
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
|
2024-03-11 13:22:33 -04:00
|
|
|
const serverEgg = ServerContext.useStoreState((state) => state.server.data?.BlueprintFramework.eggId);
|
2024-01-30 09:13:17 -05:00
|
|
|
const match = useRouteMatch<{ id: string }>();
|
|
|
|
const to = (value: string, url = false) => {
|
|
|
|
if (value === '/') {
|
|
|
|
return url ? match.url : match.path;
|
|
|
|
}
|
|
|
|
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
|
|
|
|
};
|
2024-04-08 08:26:01 -04:00
|
|
|
const extensionEggs = useExtensionEggs();
|
2024-01-21 16:01:48 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-01-30 09:13:17 -05:00
|
|
|
{/* Pterodactyl routes */}
|
|
|
|
{routes.server
|
|
|
|
.filter((route) => !!route.name)
|
|
|
|
.map((route) =>
|
|
|
|
route.permission ? (
|
|
|
|
<Can key={route.path} action={route.permission} matchAny>
|
|
|
|
<NavLink to={to(route.path, true)} exact={route.exact}>
|
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
|
|
|
</Can>
|
|
|
|
) : (
|
|
|
|
<NavLink key={route.path} to={to(route.path, true)} exact={route.exact}>
|
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
|
|
|
)
|
2024-04-08 08:26:01 -04:00
|
|
|
)}
|
2024-01-30 09:13:17 -05:00
|
|
|
|
|
|
|
{/* Blueprint routes */}
|
2024-04-08 08:26:01 -04:00
|
|
|
{blueprintRoutes.server.length > 0 &&
|
|
|
|
blueprintRoutes.server
|
|
|
|
.filter((route) => !!route.name)
|
|
|
|
.filter((route) => (route.adminOnly ? rootAdmin : true))
|
|
|
|
.filter((route) =>
|
|
|
|
extensionEggs[route.identifier].includes('-1')
|
|
|
|
? true
|
|
|
|
: extensionEggs[route.identifier].find((id) => id === serverEgg?.toString())
|
|
|
|
)
|
|
|
|
.map((route) =>
|
|
|
|
route.permission ? (
|
|
|
|
<Can key={route.path} action={route.permission} matchAny>
|
|
|
|
<NavLink to={to(route.path, true)} exact={route.exact}>
|
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
|
|
|
</Can>
|
|
|
|
) : (
|
|
|
|
<NavLink key={route.path} to={to(route.path, true)} exact={route.exact}>
|
2024-01-30 09:13:17 -05:00
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
2024-04-08 08:26:01 -04:00
|
|
|
)
|
|
|
|
)}
|
2024-01-21 16:01:48 -05:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2024-01-30 09:13:17 -05:00
|
|
|
|
|
|
|
export const NavigationRouter = () => {
|
2024-02-24 15:52:53 -05:00
|
|
|
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
|
2024-03-11 13:22:33 -04:00
|
|
|
const serverEgg = ServerContext.useStoreState((state) => state.server.data?.BlueprintFramework.eggId);
|
2024-01-30 09:13:17 -05:00
|
|
|
const match = useRouteMatch<{ id: string }>();
|
|
|
|
const to = (value: string, url = false) => {
|
|
|
|
if (value === '/') {
|
|
|
|
return url ? match.url : match.path;
|
|
|
|
}
|
|
|
|
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
|
|
|
|
};
|
2024-04-08 08:26:01 -04:00
|
|
|
const extensionEggs = useExtensionEggs();
|
2024-01-30 09:13:17 -05:00
|
|
|
|
|
|
|
const location = useLocation();
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TransitionRouter>
|
|
|
|
<Switch location={location}>
|
2024-02-13 09:04:23 -05:00
|
|
|
{/* Pterodactyl routes */}
|
2024-01-30 09:13:17 -05:00
|
|
|
{routes.server.map(({ path, permission, component: Component }) => (
|
|
|
|
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
|
|
|
|
<Spinner.Suspense>
|
|
|
|
<Component />
|
|
|
|
</Spinner.Suspense>
|
|
|
|
</PermissionRoute>
|
|
|
|
))}
|
|
|
|
|
2024-02-13 09:04:23 -05:00
|
|
|
{/* Blueprint routes */}
|
2024-04-08 08:26:01 -04:00
|
|
|
{blueprintRoutes.server.length > 0 &&
|
|
|
|
blueprintRoutes.server
|
|
|
|
.filter((route) => (route.adminOnly ? rootAdmin : true))
|
|
|
|
.filter((route) =>
|
|
|
|
extensionEggs[route.identifier].includes('-1')
|
|
|
|
? true
|
|
|
|
: extensionEggs[route.identifier].find((id) => id === serverEgg?.toString())
|
|
|
|
)
|
|
|
|
.map(({ path, permission, component: Component }) => (
|
|
|
|
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
|
|
|
|
<Spinner.Suspense>
|
|
|
|
<Component />
|
|
|
|
</Spinner.Suspense>
|
|
|
|
</PermissionRoute>
|
|
|
|
))}
|
2024-01-30 09:13:17 -05:00
|
|
|
|
|
|
|
<Route path={'*'} component={NotFound} />
|
|
|
|
</Switch>
|
|
|
|
</TransitionRouter>
|
|
|
|
</>
|
|
|
|
);
|
2024-04-08 08:26:01 -04:00
|
|
|
};
|