mirror of
https://github.com/xtr-dev/payload-feature-flags.git
synced 2025-12-10 02:43:25 +00:00
Update custom list view to use proper Payload ListView pattern
- Changed from AdminViewServerProps to ListViewServerProps - Updated component to use collectionConfig instead of collection - Simplified view structure to work directly with Payload's List View - Added ListViewClientProps import to client component 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
import React from 'react'
|
||||||
|
import type { ListViewClientProps } from 'payload'
|
||||||
import { useState, useEffect, useCallback, useMemo, memo } from 'react'
|
import { useState, useEffect, useCallback, useMemo, memo } from 'react'
|
||||||
import {
|
import {
|
||||||
useConfig,
|
useConfig,
|
||||||
|
|||||||
@@ -1,19 +1,13 @@
|
|||||||
import type { AdminViewServerProps } from 'payload'
|
import React from 'react'
|
||||||
import { DefaultTemplate } from '@payloadcms/next/templates'
|
import type { ListViewServerProps } from 'payload'
|
||||||
import { Gutter } from '@payloadcms/ui'
|
|
||||||
import FeatureFlagsClient from './FeatureFlagsClient.js'
|
import FeatureFlagsClient from './FeatureFlagsClient.js'
|
||||||
import type { FeatureFlag } from '../types/index.js'
|
import type { FeatureFlag } from '../types/index.js'
|
||||||
|
|
||||||
async function fetchInitialFlags(payload: any, searchParams?: Record<string, any>): Promise<FeatureFlag[]> {
|
async function fetchInitialFlags(payload: any, collectionSlug: string): Promise<FeatureFlag[]> {
|
||||||
try {
|
try {
|
||||||
const limit = Math.min(1000, parseInt(searchParams?.limit as string) || 100)
|
|
||||||
const page = Math.max(1, parseInt(searchParams?.page as string) || 1)
|
|
||||||
const collectionSlug = searchParams?.collectionSlug as string || 'feature-flags'
|
|
||||||
|
|
||||||
const result = await payload.find({
|
const result = await payload.find({
|
||||||
collection: collectionSlug,
|
collection: collectionSlug,
|
||||||
limit,
|
limit: 1000,
|
||||||
page,
|
|
||||||
sort: 'name',
|
sort: 'name',
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -24,131 +18,83 @@ async function fetchInitialFlags(payload: any, searchParams?: Record<string, any
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function FeatureFlagsView({
|
export default async function FeatureFlagsView(props: ListViewServerProps) {
|
||||||
initPageResult,
|
const { collectionConfig, user, permissions, payload } = props
|
||||||
params,
|
|
||||||
searchParams,
|
|
||||||
}: AdminViewServerProps) {
|
|
||||||
const {
|
|
||||||
req: { user },
|
|
||||||
permissions,
|
|
||||||
} = initPageResult
|
|
||||||
|
|
||||||
// Security check: User must be logged in
|
// Security check: User must be logged in
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return (
|
return (
|
||||||
<DefaultTemplate
|
<div style={{
|
||||||
i18n={initPageResult.req.i18n}
|
padding: '2rem',
|
||||||
locale={initPageResult.locale}
|
textAlign: 'center',
|
||||||
params={params}
|
color: 'var(--theme-error-500)',
|
||||||
payload={initPageResult.req.payload}
|
backgroundColor: 'var(--theme-error-50)',
|
||||||
permissions={initPageResult.permissions}
|
border: '1px solid var(--theme-error-200)',
|
||||||
searchParams={searchParams}
|
borderRadius: '0.5rem',
|
||||||
user={undefined}
|
margin: '2rem 0'
|
||||||
visibleEntities={initPageResult.visibleEntities}
|
}}>
|
||||||
>
|
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-error-600)' }}>
|
||||||
<Gutter>
|
Authentication Required
|
||||||
<div style={{
|
</h2>
|
||||||
padding: '2rem',
|
<p style={{ marginBottom: '1rem' }}>
|
||||||
textAlign: 'center',
|
You must be logged in to view the Feature Flags Dashboard.
|
||||||
color: 'var(--theme-error-500)',
|
</p>
|
||||||
backgroundColor: 'var(--theme-error-50)',
|
<a
|
||||||
border: '1px solid var(--theme-error-200)',
|
href="/admin/login"
|
||||||
borderRadius: '0.5rem',
|
style={{
|
||||||
margin: '2rem 0'
|
display: 'inline-block',
|
||||||
}}>
|
padding: '0.75rem 1.5rem',
|
||||||
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-error-600)' }}>
|
backgroundColor: 'var(--theme-error-500)',
|
||||||
Authentication Required
|
color: 'white',
|
||||||
</h2>
|
textDecoration: 'none',
|
||||||
<p style={{ marginBottom: '1rem' }}>
|
borderRadius: '0.375rem',
|
||||||
You must be logged in to view the Feature Flags Dashboard.
|
fontWeight: '500'
|
||||||
</p>
|
}}
|
||||||
<a
|
>
|
||||||
href="/admin/login"
|
Go to Login
|
||||||
style={{
|
</a>
|
||||||
display: 'inline-block',
|
</div>
|
||||||
padding: '0.75rem 1.5rem',
|
|
||||||
backgroundColor: 'var(--theme-error-500)',
|
|
||||||
color: 'white',
|
|
||||||
textDecoration: 'none',
|
|
||||||
borderRadius: '0.375rem',
|
|
||||||
fontWeight: '500'
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Go to Login
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</Gutter>
|
|
||||||
</DefaultTemplate>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Security check: User must have permissions to access feature-flags collection
|
// Security check: User must have permissions to access the collection
|
||||||
const collectionSlug = searchParams?.collectionSlug as string || 'feature-flags'
|
const canReadFeatureFlags = permissions?.collections?.[collectionConfig.slug]?.read
|
||||||
const canReadFeatureFlags = permissions?.collections?.[collectionSlug]?.read
|
|
||||||
if (!canReadFeatureFlags) {
|
if (!canReadFeatureFlags) {
|
||||||
return (
|
return (
|
||||||
<DefaultTemplate
|
<div style={{
|
||||||
i18n={initPageResult.req.i18n}
|
padding: '2rem',
|
||||||
locale={initPageResult.locale}
|
textAlign: 'center',
|
||||||
params={params}
|
color: 'var(--theme-warning-600)',
|
||||||
payload={initPageResult.req.payload}
|
backgroundColor: 'var(--theme-warning-50)',
|
||||||
permissions={initPageResult.permissions}
|
border: '1px solid var(--theme-warning-200)',
|
||||||
searchParams={searchParams}
|
borderRadius: '0.5rem',
|
||||||
user={initPageResult.req.user || undefined}
|
margin: '2rem 0'
|
||||||
visibleEntities={initPageResult.visibleEntities}
|
}}>
|
||||||
>
|
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-warning-700)' }}>
|
||||||
<Gutter>
|
Access Denied
|
||||||
<div style={{
|
</h2>
|
||||||
padding: '2rem',
|
<p style={{ marginBottom: '1rem' }}>
|
||||||
textAlign: 'center',
|
You don't have permission to access the Feature Flags Dashboard.
|
||||||
color: 'var(--theme-warning-600)',
|
</p>
|
||||||
backgroundColor: 'var(--theme-warning-50)',
|
<p style={{ fontSize: '0.875rem', color: 'var(--theme-warning-600)' }}>
|
||||||
border: '1px solid var(--theme-warning-200)',
|
Contact your administrator to request access to the {collectionConfig.slug} collection.
|
||||||
borderRadius: '0.5rem',
|
</p>
|
||||||
margin: '2rem 0'
|
</div>
|
||||||
}}>
|
|
||||||
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-warning-700)' }}>
|
|
||||||
Access Denied
|
|
||||||
</h2>
|
|
||||||
<p style={{ marginBottom: '1rem' }}>
|
|
||||||
You don't have permission to access the Feature Flags Dashboard.
|
|
||||||
</p>
|
|
||||||
<p style={{ fontSize: '0.875rem', color: 'var(--theme-warning-600)' }}>
|
|
||||||
Contact your administrator to request access to the feature-flags collection.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</Gutter>
|
|
||||||
</DefaultTemplate>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch initial data server-side (only if user has access)
|
// Fetch initial data server-side (only if user has access)
|
||||||
const initialFlags = await fetchInitialFlags(initPageResult.req.payload, searchParams)
|
const initialFlags = await fetchInitialFlags(payload, collectionConfig.slug)
|
||||||
|
|
||||||
// Check if user can update feature flags (use already defined collection slug)
|
// Check if user can update feature flags
|
||||||
const canUpdateFeatureFlags = permissions?.collections?.[collectionSlug]?.update || false
|
const canUpdateFeatureFlags = permissions?.collections?.[collectionConfig.slug]?.update || false
|
||||||
|
|
||||||
// Use DefaultTemplate with proper props structure from initPageResult
|
|
||||||
return (
|
return (
|
||||||
<DefaultTemplate
|
<FeatureFlagsClient
|
||||||
i18n={initPageResult.req.i18n}
|
initialFlags={initialFlags}
|
||||||
locale={initPageResult.locale}
|
canUpdate={canUpdateFeatureFlags}
|
||||||
params={params}
|
maxFlags={100}
|
||||||
payload={initPageResult.req.payload}
|
collectionSlug={collectionConfig.slug}
|
||||||
permissions={initPageResult.permissions}
|
/>
|
||||||
searchParams={searchParams}
|
|
||||||
user={initPageResult.req.user || undefined}
|
|
||||||
visibleEntities={initPageResult.visibleEntities}
|
|
||||||
>
|
|
||||||
<Gutter>
|
|
||||||
<FeatureFlagsClient
|
|
||||||
initialFlags={initialFlags}
|
|
||||||
canUpdate={canUpdateFeatureFlags}
|
|
||||||
maxFlags={parseInt(searchParams?.maxFlags as string) || 100}
|
|
||||||
collectionSlug={collectionSlug}
|
|
||||||
/>
|
|
||||||
</Gutter>
|
|
||||||
</DefaultTemplate>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user