-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(next): add support for custom collection views #16243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0cb73ca
Initial plan
Copilot b7d93d2
feat(next): add support for custom collection views
Copilot f089c8c
fix(next): fix TypeScript errors in custom collection view helper
Copilot 693be75
docs: update progress
Copilot c7fbb3a
fix(next): address code review feedback
Copilot 88cfdb7
fix(next): ensure folders take precedence over custom collection views
Copilot 55bad88
fix(payload): resolve TypeScript error with collection views type def…
Copilot c890e63
Merge pull request #1 from magicasaservice/copilot/fix-issue-15386
robinscholz 72e1fb7
Merge branch 'main' into main
robinscholz b5c8419
Merge branch 'main' into magicasaservice/main
paulpopus c7a02ca
update test coverage and fix some issues in config
paulpopus 614fc11
added tests
paulpopus 8b24e38
fix linting on test assertion
paulpopus 13274f3
fix builds
paulpopus 8ad157b
updates
paulpopus c8c279d
remove reservedPaths
paulpopus 9af48ee
e2e
paulpopus 75099cd
re-add generic string union
paulpopus 26cf6a6
address some comments
paulpopus 09f0c45
remove the string | ViewTypes pattern that was left over
paulpopus 4f9f1b6
Merge branch 'main' into feat/custom-collection-views
paulpopus d17a720
adjust ispathmatching route for non exact matches
paulpopus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
packages/next/src/views/Root/getCustomCollectionViewByRoute.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import type { AdminViewConfig, SanitizedCollectionConfig } from 'payload' | ||
|
|
||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { getCustomCollectionViewByRoute } from './getCustomCollectionViewByRoute.js' | ||
|
|
||
| type Views = SanitizedCollectionConfig['admin']['components']['views'] | ||
|
|
||
| const gridView: Views = { | ||
| grid: { | ||
| Component: '/components/views/GridView/index.js#GridView', | ||
| exact: true, | ||
| path: '/grid', | ||
| }, | ||
| } | ||
|
|
||
| const gridViewPrefixMatch: Views = { | ||
| grid: { | ||
| Component: '/components/views/GridView/index.js#GridView', | ||
| exact: false, | ||
| path: '/grid', | ||
| }, | ||
| } | ||
|
|
||
| describe('getCustomCollectionViewByRoute', () => { | ||
| describe('route matching with default /admin prefix', () => { | ||
| it('should match a custom view at the correct path', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/grid', | ||
| views: gridView, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBe('grid') | ||
| expect(result.view.payloadComponent).toBeDefined() | ||
| }) | ||
|
|
||
| it('should not match when the path segment does not correspond to any custom view', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/abc123', | ||
| views: gridView, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBeNull() | ||
| expect(result.view.payloadComponent).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('route matching with custom adminRoute prefix', () => { | ||
| it('should match when adminRoute is a non-default prefix', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/cms', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/cms/collections/my-collection/grid', | ||
| views: gridView, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBe('grid') | ||
| expect(result.view.payloadComponent).toBeDefined() | ||
| }) | ||
|
|
||
| it('should match when adminRoute is /', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/collections/my-collection/grid', | ||
| views: gridView, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBe('grid') | ||
| expect(result.view.payloadComponent).toBeDefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('route matching with exact: false (prefix matching)', () => { | ||
| it('should match a sub-path when exact is false', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/grid/detail', | ||
| views: gridViewPrefixMatch, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBe('grid') | ||
| expect(result.view.payloadComponent).toBeDefined() | ||
| }) | ||
|
|
||
| it('should match the exact path when exact is false', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/grid', | ||
| views: gridViewPrefixMatch, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBe('grid') | ||
| expect(result.view.payloadComponent).toBeDefined() | ||
| }) | ||
|
|
||
| it('should not match an unrelated path when exact is false', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/map', | ||
| views: gridViewPrefixMatch, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBeNull() | ||
| expect(result.view.payloadComponent).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('edge cases', () => { | ||
| it('should return no match when views is undefined', () => { | ||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/grid', | ||
| views: undefined, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBeNull() | ||
| expect(result.view.payloadComponent).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should not match built-in "edit" or "list" keys', () => { | ||
| const viewsWithBuiltins: Views = { | ||
| edit: { | ||
| default: { Component: '/components/views/Edit/index.js#EditView' }, | ||
| }, | ||
| list: { | ||
| Component: '/components/views/List/index.js#ListView', | ||
| }, | ||
| } | ||
|
|
||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/edit', | ||
| views: viewsWithBuiltins, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBeNull() | ||
| }) | ||
|
|
||
| it('should not match a custom view that has no path defined', () => { | ||
| const viewsWithNoPath: Views = { | ||
| grid: { | ||
| Component: '/components/views/GridView/index.js#GridView', | ||
| } as unknown as AdminViewConfig, | ||
| } | ||
|
|
||
| const result = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/grid', | ||
| views: viewsWithNoPath, | ||
| }) | ||
|
|
||
| expect(result.viewKey).toBeNull() | ||
| }) | ||
|
|
||
| it('should match the correct view when multiple custom views are defined', () => { | ||
| const multipleViews: Views = { | ||
| grid: { | ||
| Component: '/components/views/GridView/index.js#GridView', | ||
| exact: true, | ||
| path: '/grid', | ||
| }, | ||
| map: { | ||
| Component: '/components/views/MapView/index.js#MapView', | ||
| exact: true, | ||
| path: '/map', | ||
| }, | ||
| } | ||
|
|
||
| const gridResult = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/grid', | ||
| views: multipleViews, | ||
| }) | ||
|
|
||
| expect(gridResult.viewKey).toBe('grid') | ||
|
|
||
| const mapResult = getCustomCollectionViewByRoute({ | ||
| adminRoute: '/admin', | ||
| baseRoute: '/collections/my-collection', | ||
| currentRoute: '/admin/collections/my-collection/map', | ||
| views: multipleViews, | ||
| }) | ||
|
|
||
| expect(mapResult.viewKey).toBe('map') | ||
| }) | ||
| }) | ||
| }) | ||
82 changes: 82 additions & 0 deletions
82
packages/next/src/views/Root/getCustomCollectionViewByRoute.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import type { | ||
| AdminViewConfig, | ||
| AdminViewServerProps, | ||
| PayloadComponent, | ||
| SanitizedCollectionConfig, | ||
| } from 'payload' | ||
|
|
||
| import type { ViewFromConfig } from './getRouteData.js' | ||
|
|
||
| import { isPathMatchingRoute } from './isPathMatchingRoute.js' | ||
|
|
||
| export const getCustomCollectionViewByRoute = ({ | ||
| adminRoute, | ||
| baseRoute, | ||
| currentRoute: currentRouteWithAdmin, | ||
| views, | ||
| }: { | ||
| adminRoute: string | ||
| baseRoute: string | ||
| currentRoute: string | ||
| views: SanitizedCollectionConfig['admin']['components']['views'] | ||
| }): { | ||
| view: ViewFromConfig | ||
| viewKey: null | string | ||
| } => { | ||
| const currentRoute = | ||
| adminRoute === '/' | ||
| ? currentRouteWithAdmin | ||
| : currentRouteWithAdmin.startsWith(adminRoute) | ||
| ? currentRouteWithAdmin.slice(adminRoute.length) | ||
| : currentRouteWithAdmin | ||
|
|
||
| if (views && typeof views === 'object') { | ||
| const foundEntry = Object.entries(views).find(([key, view]) => { | ||
| // Skip the known collection view types: edit and list | ||
| if (key === 'edit' || key === 'list') { | ||
| return false | ||
| } | ||
|
|
||
| // Type guard: custom views should be AdminViewConfig with path and Component | ||
| const isAdminViewConfig = | ||
| typeof view === 'object' && | ||
| view !== null && | ||
| 'path' in view && | ||
| 'Component' in view && | ||
| typeof view.path === 'string' | ||
|
|
||
| if (isAdminViewConfig) { | ||
| const adminView = view as AdminViewConfig | ||
| const viewPath = `${baseRoute}${adminView.path}` | ||
|
|
||
| return isPathMatchingRoute({ | ||
|
paulpopus marked this conversation as resolved.
|
||
| currentRoute, | ||
| exact: adminView.exact, | ||
| path: viewPath, | ||
| sensitive: adminView.sensitive, | ||
| strict: adminView.strict, | ||
| }) | ||
| } | ||
|
|
||
| return false | ||
| }) | ||
|
|
||
| if (foundEntry) { | ||
| const [viewKey, foundViewConfig] = foundEntry | ||
| const adminView = foundViewConfig as AdminViewConfig | ||
| return { | ||
| view: { | ||
| payloadComponent: adminView.Component as PayloadComponent<AdminViewServerProps>, | ||
| }, | ||
| viewKey, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| view: { | ||
| Component: null, | ||
| }, | ||
| viewKey: null, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.