Add a custom admin view over the technologies collection so staff can model prerequisites as a left-to-right tree instead of a flat list. - React Flow v12 canvas with category bands, OR-prerequisite edges, a lite side-panel editor, and a quick-create dialog. - Propagating layout: a technology with prerequisites lands one column right of its parents at their average row; uncategorized roots fill a catalog grid keyed by each tech's rank among all uncategorized technologies, so promoting one into the tree leaves an empty slot instead of re-shuffling the grid. - Stability contract: connecting or removing edges never moves a card (the connected node is pinned to its current spot), and a "Save positions" control commits every on-screen node so the arrangement survives reload; "Auto-arrange" clears manual positions and restores the ordered layout. - Progression-path edge colors and per-source lanes keep overlapping edges readable. Wire the view and its nav entry into payload.config.ts and add dark React Flow / scrollbar styling.
151 lines
4.7 KiB
TypeScript
151 lines
4.7 KiB
TypeScript
import { NavHamburger, NavWrapper } from "@payloadcms/next/client";
|
|
import { Logout } from "@payloadcms/ui";
|
|
import { RenderServerComponent } from "@payloadcms/ui/elements/RenderServerComponent";
|
|
import { EntityType, groupNavItems, type EntityToGroup } from "@payloadcms/ui/shared";
|
|
import { PREFERENCE_KEYS } from "payload/shared";
|
|
import type { PayloadRequest, SanitizedPermissions, ServerProps } from "payload";
|
|
import React from "react";
|
|
|
|
import { TechTreeNavClient, type TechTreeNavGroup, type TechTreeNavEntity } from "./TechTreeNavClient";
|
|
|
|
const baseClass = "nav";
|
|
|
|
type NavProps = ServerProps & { req?: PayloadRequest };
|
|
|
|
/**
|
|
* Replaces the default admin Nav so a "Tech Tree" entry can be injected into
|
|
* the Intelligence group (the group containing the technologies collection).
|
|
* Everything else mirrors Payload's DefaultNav: groups from visible entities,
|
|
* nav preferences, before/after components, and the logout control. The
|
|
* Tech Tree entry rides along with the technologies collection's visibility,
|
|
* so users who cannot see Technologies never see the tree entry either.
|
|
*/
|
|
export const TechTreeNav = async ({
|
|
i18n,
|
|
locale,
|
|
params,
|
|
payload,
|
|
permissions,
|
|
req,
|
|
searchParams,
|
|
user,
|
|
visibleEntities,
|
|
}: NavProps) => {
|
|
if (!payload?.config) {
|
|
return null;
|
|
}
|
|
|
|
const {
|
|
admin: {
|
|
components: { afterNav, afterNavLinks, beforeNav, beforeNavLinks, logout },
|
|
},
|
|
collections,
|
|
globals,
|
|
} = payload.config;
|
|
|
|
const visibleCollections = visibleEntities?.collections ?? [];
|
|
const visibleGlobals = visibleEntities?.globals ?? [];
|
|
|
|
const groups: TechTreeNavGroup[] = groupNavItems(
|
|
[
|
|
...visibleCollections.map((slug) => {
|
|
const collection = collections.find((c) => c.slug === slug)!;
|
|
return { type: EntityType.collection, entity: collection } satisfies EntityToGroup;
|
|
}),
|
|
...visibleGlobals.map((slug) => {
|
|
const global = globals.find((g) => g.slug === slug)!;
|
|
return { type: EntityType.global, entity: global } satisfies EntityToGroup;
|
|
}),
|
|
],
|
|
permissions ?? ({} as SanitizedPermissions),
|
|
i18n,
|
|
).map((group): TechTreeNavGroup => {
|
|
const techIndex = group.entities.findIndex(
|
|
(entity) => entity.type === EntityType.collection && entity.slug === "technologies",
|
|
);
|
|
if (techIndex === -1) return group;
|
|
const entities: TechTreeNavEntity[] = [...group.entities];
|
|
entities.splice(techIndex + 1, 0, {
|
|
slug: "tech-tree",
|
|
label: "Tech Tree",
|
|
type: "custom",
|
|
href: "/tech-tree",
|
|
});
|
|
return { ...group, entities };
|
|
});
|
|
|
|
let navPreferences: { groups?: { [key: string]: { open: boolean } } } | null = null;
|
|
if (req?.user?.collection) {
|
|
const prefs = await req.payload.find({
|
|
collection: "payload-preferences",
|
|
depth: 0,
|
|
limit: 1,
|
|
pagination: false,
|
|
req,
|
|
where: {
|
|
and: [
|
|
{ key: { equals: PREFERENCE_KEYS.NAV } },
|
|
{ "user.relationTo": { equals: req.user.collection } },
|
|
{ "user.value": { equals: req?.user?.id } },
|
|
],
|
|
},
|
|
});
|
|
navPreferences = (prefs?.docs?.[0]?.value as typeof navPreferences) ?? null;
|
|
}
|
|
|
|
const serverProps = { i18n, locale, params, payload, permissions, searchParams, user };
|
|
const clientProps = { documentSubViewType: undefined, viewType: undefined };
|
|
|
|
const LogoutComponent = RenderServerComponent({
|
|
clientProps,
|
|
Component: logout?.Button,
|
|
Fallback: Logout,
|
|
importMap: payload.importMap,
|
|
serverProps,
|
|
});
|
|
|
|
const RenderedBeforeNav = RenderServerComponent({
|
|
clientProps,
|
|
Component: beforeNav,
|
|
importMap: payload.importMap,
|
|
serverProps,
|
|
});
|
|
const RenderedBeforeNavLinks = RenderServerComponent({
|
|
clientProps,
|
|
Component: beforeNavLinks,
|
|
importMap: payload.importMap,
|
|
serverProps,
|
|
});
|
|
const RenderedAfterNavLinks = RenderServerComponent({
|
|
clientProps,
|
|
Component: afterNavLinks,
|
|
importMap: payload.importMap,
|
|
serverProps,
|
|
});
|
|
const RenderedAfterNav = RenderServerComponent({
|
|
clientProps,
|
|
Component: afterNav,
|
|
importMap: payload.importMap,
|
|
serverProps,
|
|
});
|
|
|
|
return (
|
|
<NavWrapper baseClass={baseClass}>
|
|
{RenderedBeforeNav}
|
|
<nav className={`${baseClass}__wrap`}>
|
|
{RenderedBeforeNavLinks}
|
|
<TechTreeNavClient groups={groups} navPreferences={navPreferences} />
|
|
{RenderedAfterNavLinks}
|
|
<div className={`${baseClass}__controls`}>{LogoutComponent}</div>
|
|
</nav>
|
|
{RenderedAfterNav}
|
|
<div className={`${baseClass}__header`}>
|
|
<div className={`${baseClass}__header-content`}>
|
|
<NavHamburger baseClass={baseClass} />
|
|
</div>
|
|
</div>
|
|
</NavWrapper>
|
|
);
|
|
};
|
|
|
|
export default TechTreeNav;
|