-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathgetCustomCollectionViewByRoute.ts
More file actions
78 lines (68 loc) · 1.93 KB
/
getCustomCollectionViewByRoute.ts
File metadata and controls
78 lines (68 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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.replace(adminRoute, '')
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({
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,
}
}