From e80d67987e2aab99df7cc2b6f620227808f43e01 Mon Sep 17 00:00:00 2001 From: Jarrod Flesch <30633324+JarrodMFlesch@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:44:09 -0500 Subject: [PATCH] feat(ui): exposes context of the view being rendered on the server (#10620) ### What? Extends visibility into what view is being shown so custom components have context as to where they are being rendered. **This PR does not add React Context.** ### Why? This was needed for the multi-tenant plugin where the selector is in the navigation sidebar and has no way to know if it is being shown inside of a document or the list view. I assume other users may also want their server components to be aware of where a component is rendering before hitting the client. An example would be wanting to redirect on the server instead of on the client, this is how multi-tenant redirects users from "global" enabled collections to the document view. ### How? Adds 2 new variables that are determined by the view being routed to. `viewType` - which view is being rendered, ie `list`, `document`, `version`, `account`, `verify`, `reset` ```ts type ViewTypes = | 'account' | 'dashboard' | 'document' | 'list' | 'reset' | 'verify' | 'version' ``` `documentSubViewType` - which tells you what sub view you are on, ie `api`, `livePreview`, `default`, `versions` ```ts type DocumentSubViewTypes = | 'api' | 'default' | 'livePreview' | 'version' | 'versions' ``` --- packages/next/src/elements/Nav/index.tsx | 25 ++- packages/next/src/templates/Default/index.tsx | 36 ++-- .../views/Document/handleServerFunction.tsx | 2 + packages/next/src/views/Document/index.tsx | 4 +- .../src/views/List/handleServerFunction.tsx | 1 + packages/next/src/views/List/index.tsx | 2 - .../next/src/views/Root/attachViewActions.ts | 47 +++++ .../src/views/Root/getDocumentViewInfo.ts | 39 ++++ .../next/src/views/Root/getViewFromConfig.ts | 166 ++++-------------- packages/next/src/views/Root/index.tsx | 30 ++-- packages/payload/src/admin/views/types.ts | 27 ++- packages/payload/src/config/types.ts | 7 + .../components/GlobalViewRedirect/index.ts | 4 +- .../src/components/TenantSelector/index.tsx | 8 +- packages/plugin-multi-tenant/src/index.ts | 4 - .../src/utilities/getGlobalViewRedirect.ts | 6 +- packages/plugin-multi-tenant/tsconfig.json | 2 +- 17 files changed, 241 insertions(+), 169 deletions(-) create mode 100644 packages/next/src/views/Root/attachViewActions.ts create mode 100644 packages/next/src/views/Root/getDocumentViewInfo.ts diff --git a/packages/next/src/elements/Nav/index.tsx b/packages/next/src/elements/Nav/index.tsx index 7eed9d9b1..a1d09e70e 100644 --- a/packages/next/src/elements/Nav/index.tsx +++ b/packages/next/src/elements/Nav/index.tsx @@ -18,7 +18,18 @@ import { DefaultNavClient } from './index.client.js' export type NavProps = ServerProps export const DefaultNav: React.FC = async (props) => { - const { i18n, locale, params, payload, permissions, searchParams, user, visibleEntities } = props + const { + documentSubViewType, + i18n, + locale, + params, + payload, + permissions, + searchParams, + user, + viewType, + visibleEntities, + } = props if (!payload?.config) { return null @@ -60,6 +71,10 @@ export const DefaultNav: React.FC = async (props) => { const navPreferences = await getNavPrefs({ payload, user }) const LogoutComponent = RenderServerComponent({ + clientProps: { + documentSubViewType, + viewType, + }, Component: logout?.Button, Fallback: Logout, importMap: payload.importMap, @@ -78,6 +93,10 @@ export const DefaultNav: React.FC = async (props) => {