It is currently very difficult to build custom edit and list views or inject custom components into these views because these views and components are not explicitly typed. Instances of these components were not fully type safe as well, i.e. when rendering them via `RenderServerComponent`, there was little to no type-checking in most cases. There is now a 1:1 type match for all views and view components and they now receive type-checking at render time. The following types have been newly added and/or improved: List View: - `ListViewClientProps` - `ListViewServerProps` - `BeforeListClientProps` - `BeforeListServerProps` - `BeforeListTableClientProps` - `BeforeListTableServerProps` - `AfterListClientProps` - `AfterListServerProps` - `AfterListTableClientProps` - `AfterListTableServerProps` - `ListViewSlotSharedClientProps` Document View: - `DocumentViewClientProps` - `DocumentViewServerProps` - `SaveButtonClientProps` - `SaveButtonServerProps` - `SaveDraftButtonClientProps` - `SaveDraftButtonServerProps` - `PublishButtonClientProps` - `PublishButtonServerProps` - `PreviewButtonClientProps` - `PreviewButtonServerProps` Root View: - `AdminViewClientProps` - `AdminViewServerProps` General: - `ViewDescriptionClientProps` - `ViewDescriptionServerProps` A few other changes were made in a non-breaking way: - `Column` is now exported from `payload` - `ListPreferences` is now exported from `payload` - `ListViewSlots` is now exported from `payload` - `ListViewClientProps` is now exported from `payload` - `AdminViewProps` is now an alias of `AdminViewServerProps` (listed above) - `ClientSideEditViewProps` is now an alias of `DocumentViewClientProps` (listed above) - `ServerSideEditViewProps` is now an alias of `DocumentViewServerProps` (listed above) - `ListComponentClientProps` is now an alias of `ListViewClientProps` (listed above) - `ListComponentServerProps` is now an alias of `ListViewServerProps` (listed above) - `CustomSaveButton` is now marked as deprecated because this is only relevant to the config (see correct type above) - `CustomSaveDraftButton` is now marked as deprecated because this is only relevant to the config (see correct type above) - `CustomPublishButton` is now marked as deprecated because this is only relevant to the config (see correct type above) - `CustomPreviewButton` is now marked as deprecated because this is only relevant to the config (see correct type above) This PR _does not_ apply these changes to _root_ components, i.e. `afterNavLinks`. Those will come in a future PR. Related: #10987.
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import type { AdminViewServerProps } from 'payload'
|
|
|
|
import { Button } from '@payloadcms/ui'
|
|
import LinkImport from 'next/link.js'
|
|
import { notFound, redirect } from 'next/navigation.js'
|
|
import React from 'react'
|
|
|
|
import { customNestedViewTitle, customViewPath } from '../../../shared.js'
|
|
import { settingsGlobalSlug } from '../../../slugs.js'
|
|
|
|
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
|
|
|
|
export async function CustomProtectedView({ initPageResult }: AdminViewServerProps) {
|
|
const {
|
|
req: {
|
|
payload: {
|
|
config: {
|
|
routes: { admin: adminRoute },
|
|
},
|
|
},
|
|
user,
|
|
},
|
|
req,
|
|
} = initPageResult
|
|
|
|
const settings = await req.payload.findGlobal({
|
|
slug: settingsGlobalSlug,
|
|
})
|
|
|
|
if (!settings?.canAccessProtected) {
|
|
if (user) {
|
|
redirect(`${adminRoute}/unauthorized`)
|
|
} else {
|
|
notFound()
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div
|
|
style={{
|
|
marginTop: 'calc(var(--base) * 2)',
|
|
paddingLeft: 'var(--gutter-h)',
|
|
paddingRight: 'var(--gutter-h)',
|
|
}}
|
|
>
|
|
<h1 id="custom-view-title">{customNestedViewTitle}</h1>
|
|
<p>This custom view was added through the Payload config:</p>
|
|
<ul>
|
|
<li>
|
|
<code>components.views[key].Component</code>
|
|
</li>
|
|
</ul>
|
|
<div className="custom-view__controls">
|
|
<Button buttonStyle="secondary" el="link" Link={Link} to={`${adminRoute}`}>
|
|
Go to Dashboard
|
|
</Button>
|
|
|
|
<Button
|
|
buttonStyle="secondary"
|
|
el="link"
|
|
Link={Link}
|
|
to={`${adminRoute}/${customViewPath}`}
|
|
>
|
|
Go to Custom View
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|