mirror of
https://github.com/xtr-dev/payload-feature-flags.git
synced 2025-12-12 20:03:25 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f49a445e5a | ||
|
|
263c355806 | ||
|
|
33b39e3ced | ||
|
|
a1943c23a6 | ||
|
|
e0e0046d21 | ||
|
|
adffe3aaa1 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "payload-feature-flags",
|
"name": "payload-feature-flags",
|
||||||
"version": "0.0.19",
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "payload-feature-flags",
|
"name": "payload-feature-flags",
|
||||||
"version": "0.0.19",
|
"version": "1.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-feature-flags",
|
"name": "@xtr-dev/payload-feature-flags",
|
||||||
"version": "0.0.20",
|
"version": "0.0.12",
|
||||||
"description": "Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts",
|
"description": "Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -7,5 +7,4 @@ export {
|
|||||||
useRolloutCheck,
|
useRolloutCheck,
|
||||||
withFeatureFlag,
|
withFeatureFlag,
|
||||||
type FeatureFlag,
|
type FeatureFlag,
|
||||||
type FeatureFlagOptions,
|
|
||||||
} from '../hooks/client.js'
|
} from '../hooks/client.js'
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import React, { useCallback, useEffect, useState, useRef } from 'react'
|
import React, { useCallback, useEffect, useState } from 'react'
|
||||||
|
import { useConfig } from '@payloadcms/ui'
|
||||||
|
|
||||||
export interface FeatureFlag {
|
export interface FeatureFlag {
|
||||||
name: string
|
name: string
|
||||||
@@ -13,78 +14,34 @@ export interface FeatureFlag {
|
|||||||
metadata?: any
|
metadata?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FeatureFlagOptions {
|
|
||||||
serverURL?: string
|
|
||||||
apiPath?: string
|
|
||||||
collectionSlug?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to get config from options or defaults
|
|
||||||
function getConfig(options?: FeatureFlagOptions) {
|
|
||||||
// Check if serverURL is explicitly provided
|
|
||||||
if (options?.serverURL) {
|
|
||||||
return {
|
|
||||||
serverURL: options.serverURL,
|
|
||||||
apiPath: options.apiPath || '/api',
|
|
||||||
collectionSlug: options.collectionSlug || 'feature-flags'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// In browser environment, use window.location.origin
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
return {
|
|
||||||
serverURL: window.location.origin,
|
|
||||||
apiPath: options?.apiPath || '/api',
|
|
||||||
collectionSlug: options?.collectionSlug || 'feature-flags'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// During SSR or in non-browser environments, use relative URL
|
|
||||||
// This will work for same-origin requests
|
|
||||||
return {
|
|
||||||
serverURL: '',
|
|
||||||
apiPath: options?.apiPath || '/api',
|
|
||||||
collectionSlug: options?.collectionSlug || 'feature-flags'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to fetch all active feature flags from the API
|
* Hook to fetch all active feature flags from the API
|
||||||
*/
|
*/
|
||||||
export function useFeatureFlags(
|
export function useFeatureFlags(
|
||||||
initialFlags: Partial<FeatureFlag>[],
|
initialFlags: Partial<FeatureFlag>[]
|
||||||
options?: FeatureFlagOptions
|
|
||||||
): {
|
): {
|
||||||
flags: Partial<FeatureFlag>[]
|
flags: Partial<FeatureFlag>[]
|
||||||
loading: boolean
|
loading: boolean
|
||||||
error: string | null
|
error: string | null
|
||||||
refetch: () => Promise<void>
|
refetch: () => Promise<void>
|
||||||
} {
|
} {
|
||||||
const { serverURL, apiPath, collectionSlug } = getConfig(options)
|
const { config } = useConfig()
|
||||||
const [flags, setFlags] = useState<Partial<FeatureFlag>[]>(initialFlags)
|
const [flags, setFlags] = useState<Partial<FeatureFlag>[]>(initialFlags)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
|
||||||
// Use ref to store initialFlags to avoid re-creating fetchFlags on every render
|
|
||||||
const initialFlagsRef = useRef(initialFlags)
|
|
||||||
|
|
||||||
// Update ref when initialFlags changes (but won't trigger re-fetch)
|
|
||||||
useEffect(() => {
|
|
||||||
initialFlagsRef.current = initialFlags
|
|
||||||
}, [initialFlags])
|
|
||||||
|
|
||||||
const fetchFlags = useCallback(async () => {
|
const fetchFlags = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setError(null)
|
setError(null)
|
||||||
|
|
||||||
// Use Payload's native collection API
|
// Use Payload's native collection API
|
||||||
const names = initialFlagsRef.current.map(f => f.name).filter(Boolean)
|
const names = initialFlags.map(f => f.name).filter(Boolean)
|
||||||
const query = names.length > 0
|
const query = names.length > 0
|
||||||
? `?where[name][in]=${names.join(',')}&limit=1000`
|
? `?where[name][in]=${names.join(',')}&limit=1000`
|
||||||
: '?limit=1000'
|
: '?limit=1000'
|
||||||
|
|
||||||
const response = await fetch(`${serverURL}${apiPath}/${collectionSlug}${query}`)
|
const response = await fetch(`${config.serverURL}${config.routes.api}/feature-flags${query}`)
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to fetch feature flags: ${response.statusText}`)
|
throw new Error(`Failed to fetch feature flags: ${response.statusText}`)
|
||||||
@@ -107,7 +64,7 @@ export function useFeatureFlags(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort flags based on the order of names in initialFlags
|
// Sort flags based on the order of names in initialFlags
|
||||||
const sortedFlags = initialFlagsRef.current.map(initialFlag => {
|
const sortedFlags = initialFlags.map(initialFlag => {
|
||||||
const fetchedFlag = fetchedFlagsMap.get(initialFlag.name!)
|
const fetchedFlag = fetchedFlagsMap.get(initialFlag.name!)
|
||||||
// Use fetched flag if available, otherwise keep the initial flag
|
// Use fetched flag if available, otherwise keep the initial flag
|
||||||
return fetchedFlag || initialFlag
|
return fetchedFlag || initialFlag
|
||||||
@@ -120,7 +77,7 @@ export function useFeatureFlags(
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}, [serverURL, apiPath, collectionSlug]) // Remove initialFlags from dependencies
|
}, [config.serverURL, config.routes.api, initialFlags])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void fetchFlags()
|
void fetchFlags()
|
||||||
@@ -132,16 +89,13 @@ export function useFeatureFlags(
|
|||||||
/**
|
/**
|
||||||
* Hook to check if a specific feature flag is enabled
|
* Hook to check if a specific feature flag is enabled
|
||||||
*/
|
*/
|
||||||
export function useFeatureFlag(
|
export function useFeatureFlag(flagName: string): {
|
||||||
flagName: string,
|
|
||||||
options?: FeatureFlagOptions
|
|
||||||
): {
|
|
||||||
isEnabled: boolean
|
isEnabled: boolean
|
||||||
flag: Partial<FeatureFlag> | null
|
flag: Partial<FeatureFlag> | null
|
||||||
loading: boolean
|
loading: boolean
|
||||||
error: string | null
|
error: string | null
|
||||||
} {
|
} {
|
||||||
const { flags, loading, error } = useFeatureFlags([{ name: flagName }], options)
|
const { flags, loading, error } = useFeatureFlags([{ name: flagName }])
|
||||||
|
|
||||||
const flag = flags.find(f => f.name === flagName) || null
|
const flag = flags.find(f => f.name === flagName) || null
|
||||||
const isEnabled = flag?.enabled || false
|
const isEnabled = flag?.enabled || false
|
||||||
@@ -152,16 +106,13 @@ export function useFeatureFlag(
|
|||||||
/**
|
/**
|
||||||
* Hook to fetch a specific feature flag from the API
|
* Hook to fetch a specific feature flag from the API
|
||||||
*/
|
*/
|
||||||
export function useSpecificFeatureFlag(
|
export function useSpecificFeatureFlag(flagName: string): {
|
||||||
flagName: string,
|
|
||||||
options?: FeatureFlagOptions
|
|
||||||
): {
|
|
||||||
flag: FeatureFlag | null
|
flag: FeatureFlag | null
|
||||||
loading: boolean
|
loading: boolean
|
||||||
error: string | null
|
error: string | null
|
||||||
refetch: () => Promise<void>
|
refetch: () => Promise<void>
|
||||||
} {
|
} {
|
||||||
const { serverURL, apiPath, collectionSlug } = getConfig(options)
|
const { config } = useConfig()
|
||||||
const [flag, setFlag] = useState<FeatureFlag | null>(null)
|
const [flag, setFlag] = useState<FeatureFlag | null>(null)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
@@ -173,7 +124,7 @@ export function useSpecificFeatureFlag(
|
|||||||
|
|
||||||
// Use Payload's native collection API with query filter
|
// Use Payload's native collection API with query filter
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${serverURL}${apiPath}/${collectionSlug}?where[name][equals]=${flagName}&limit=1`
|
`${config.serverURL}${config.routes.api}/feature-flags?where[name][equals]=${flagName}&limit=1`
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -202,7 +153,7 @@ export function useSpecificFeatureFlag(
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}, [serverURL, apiPath, collectionSlug, flagName])
|
}, [config.serverURL, config.routes.api, flagName])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void fetchFlag()
|
void fetchFlag()
|
||||||
@@ -216,15 +167,14 @@ export function useSpecificFeatureFlag(
|
|||||||
*/
|
*/
|
||||||
export function useVariantSelection(
|
export function useVariantSelection(
|
||||||
flagName: string,
|
flagName: string,
|
||||||
userId: string,
|
userId: string
|
||||||
options?: FeatureFlagOptions
|
|
||||||
): {
|
): {
|
||||||
variant: string | null
|
variant: string | null
|
||||||
flag: FeatureFlag | null
|
flag: FeatureFlag | null
|
||||||
loading: boolean
|
loading: boolean
|
||||||
error: string | null
|
error: string | null
|
||||||
} {
|
} {
|
||||||
const { flag, loading, error } = useSpecificFeatureFlag(flagName, options)
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName)
|
||||||
|
|
||||||
const variant = flag?.enabled && flag.variants
|
const variant = flag?.enabled && flag.variants
|
||||||
? selectVariantForUser(userId, flag.variants)
|
? selectVariantForUser(userId, flag.variants)
|
||||||
@@ -238,15 +188,14 @@ export function useVariantSelection(
|
|||||||
*/
|
*/
|
||||||
export function useRolloutCheck(
|
export function useRolloutCheck(
|
||||||
flagName: string,
|
flagName: string,
|
||||||
userId: string,
|
userId: string
|
||||||
options?: FeatureFlagOptions
|
|
||||||
): {
|
): {
|
||||||
isInRollout: boolean
|
isInRollout: boolean
|
||||||
flag: FeatureFlag | null
|
flag: FeatureFlag | null
|
||||||
loading: boolean
|
loading: boolean
|
||||||
error: string | null
|
error: string | null
|
||||||
} {
|
} {
|
||||||
const { flag, loading, error } = useSpecificFeatureFlag(flagName, options)
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName)
|
||||||
|
|
||||||
const isInRollout = flag?.enabled
|
const isInRollout = flag?.enabled
|
||||||
? checkUserInRollout(userId, flag.rolloutPercentage || 100)
|
? checkUserInRollout(userId, flag.rolloutPercentage || 100)
|
||||||
@@ -304,14 +253,13 @@ function checkUserInRollout(userId: string, percentage: number): boolean {
|
|||||||
*/
|
*/
|
||||||
export function withFeatureFlag<P extends Record<string, any>>(
|
export function withFeatureFlag<P extends Record<string, any>>(
|
||||||
flagName: string,
|
flagName: string,
|
||||||
FallbackComponent?: React.ComponentType<P>,
|
FallbackComponent?: React.ComponentType<P>
|
||||||
options?: FeatureFlagOptions
|
|
||||||
) {
|
) {
|
||||||
return function FeatureFlagWrapper(
|
return function FeatureFlagWrapper(
|
||||||
WrappedComponent: React.ComponentType<P>
|
WrappedComponent: React.ComponentType<P>
|
||||||
): React.ComponentType<P> {
|
): React.ComponentType<P> {
|
||||||
return function WithFeatureFlagComponent(props: P): React.ReactElement | null {
|
return function WithFeatureFlagComponent(props: P): React.ReactElement | null {
|
||||||
const { isEnabled, loading } = useFeatureFlag(flagName, options)
|
const { isEnabled, loading } = useFeatureFlag(flagName)
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return null // or a loading spinner
|
return null // or a loading spinner
|
||||||
|
|||||||
15
src/index.ts
15
src/index.ts
@@ -34,11 +34,6 @@ export type PayloadFeatureFlagsConfig = {
|
|||||||
* Override collection configuration
|
* Override collection configuration
|
||||||
*/
|
*/
|
||||||
collectionOverrides?: CollectionOverrides
|
collectionOverrides?: CollectionOverrides
|
||||||
/**
|
|
||||||
* Enable custom list view for feature flags
|
|
||||||
* @default false
|
|
||||||
*/
|
|
||||||
enableCustomListView?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const payloadFeatureFlags =
|
export const payloadFeatureFlags =
|
||||||
@@ -49,7 +44,6 @@ export const payloadFeatureFlags =
|
|||||||
defaultValue = false,
|
defaultValue = false,
|
||||||
enableRollouts = true,
|
enableRollouts = true,
|
||||||
enableVariants = true,
|
enableVariants = true,
|
||||||
enableCustomListView = false,
|
|
||||||
collectionOverrides,
|
collectionOverrides,
|
||||||
} = pluginOptions
|
} = pluginOptions
|
||||||
|
|
||||||
@@ -169,15 +163,6 @@ export const payloadFeatureFlags =
|
|||||||
useAsTitle: 'name',
|
useAsTitle: 'name',
|
||||||
group: 'Configuration',
|
group: 'Configuration',
|
||||||
description: 'Manage feature flags for your application',
|
description: 'Manage feature flags for your application',
|
||||||
components: enableCustomListView ? {
|
|
||||||
...collectionOverrides?.admin?.components,
|
|
||||||
views: {
|
|
||||||
...collectionOverrides?.admin?.components?.views,
|
|
||||||
list: {
|
|
||||||
Component: '@xtr-dev/payload-feature-flags/views#FeatureFlagsView'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} : collectionOverrides?.admin?.components || {},
|
|
||||||
...(collectionOverrides?.admin || {}),
|
...(collectionOverrides?.admin || {}),
|
||||||
},
|
},
|
||||||
fields,
|
fields,
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
'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,
|
||||||
@@ -238,7 +236,7 @@ const FeatureFlagsClientComponent = ({
|
|||||||
color: styles.text,
|
color: styles.text,
|
||||||
margin: '0 0 0.5rem 0'
|
margin: '0 0 0.5rem 0'
|
||||||
}}>
|
}}>
|
||||||
Feature Flags
|
Feature Flags Dashboard
|
||||||
</h1>
|
</h1>
|
||||||
<p style={{
|
<p style={{
|
||||||
color: styles.textMuted,
|
color: styles.textMuted,
|
||||||
@@ -666,4 +664,4 @@ const FeatureFlagsClientComponent = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const FeatureFlagsClient = memo(FeatureFlagsClientComponent)
|
export const FeatureFlagsClient = memo(FeatureFlagsClientComponent)
|
||||||
export default FeatureFlagsClient
|
export default FeatureFlagsClient
|
||||||
@@ -1,13 +1,19 @@
|
|||||||
import React from 'react'
|
import type { AdminViewServerProps } from 'payload'
|
||||||
import type { ListViewServerProps } from 'payload'
|
import { DefaultTemplate } from '@payloadcms/next/templates'
|
||||||
|
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, collectionSlug: string): Promise<FeatureFlag[]> {
|
async function fetchInitialFlags(payload: any, searchParams?: Record<string, any>): 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: 1000,
|
limit,
|
||||||
|
page,
|
||||||
sort: 'name',
|
sort: 'name',
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -18,83 +24,131 @@ async function fetchInitialFlags(payload: any, collectionSlug: string): Promise<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function FeatureFlagsView(props: ListViewServerProps) {
|
export default async function FeatureFlagsView({
|
||||||
const { collectionConfig, user, permissions, payload } = props
|
initPageResult,
|
||||||
|
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 (
|
||||||
<div style={{
|
<DefaultTemplate
|
||||||
padding: '2rem',
|
i18n={initPageResult.req.i18n}
|
||||||
textAlign: 'center',
|
locale={initPageResult.locale}
|
||||||
color: 'var(--theme-error-500)',
|
params={params}
|
||||||
backgroundColor: 'var(--theme-error-50)',
|
payload={initPageResult.req.payload}
|
||||||
border: '1px solid var(--theme-error-200)',
|
permissions={initPageResult.permissions}
|
||||||
borderRadius: '0.5rem',
|
searchParams={searchParams}
|
||||||
margin: '2rem 0'
|
user={undefined}
|
||||||
}}>
|
visibleEntities={initPageResult.visibleEntities}
|
||||||
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-error-600)' }}>
|
>
|
||||||
Authentication Required
|
<Gutter>
|
||||||
</h2>
|
<div style={{
|
||||||
<p style={{ marginBottom: '1rem' }}>
|
padding: '2rem',
|
||||||
You must be logged in to view the Feature Flags Dashboard.
|
textAlign: 'center',
|
||||||
</p>
|
color: 'var(--theme-error-500)',
|
||||||
<a
|
backgroundColor: 'var(--theme-error-50)',
|
||||||
href="/admin/login"
|
border: '1px solid var(--theme-error-200)',
|
||||||
style={{
|
borderRadius: '0.5rem',
|
||||||
display: 'inline-block',
|
margin: '2rem 0'
|
||||||
padding: '0.75rem 1.5rem',
|
}}>
|
||||||
backgroundColor: 'var(--theme-error-500)',
|
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-error-600)' }}>
|
||||||
color: 'white',
|
Authentication Required
|
||||||
textDecoration: 'none',
|
</h2>
|
||||||
borderRadius: '0.375rem',
|
<p style={{ marginBottom: '1rem' }}>
|
||||||
fontWeight: '500'
|
You must be logged in to view the Feature Flags Dashboard.
|
||||||
}}
|
</p>
|
||||||
>
|
<a
|
||||||
Go to Login
|
href="/admin/login"
|
||||||
</a>
|
style={{
|
||||||
</div>
|
display: 'inline-block',
|
||||||
|
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 the collection
|
// Security check: User must have permissions to access feature-flags collection
|
||||||
const canReadFeatureFlags = permissions?.collections?.[collectionConfig.slug]?.read
|
const collectionSlug = searchParams?.collectionSlug as string || 'feature-flags'
|
||||||
|
const canReadFeatureFlags = permissions?.collections?.[collectionSlug]?.read
|
||||||
if (!canReadFeatureFlags) {
|
if (!canReadFeatureFlags) {
|
||||||
return (
|
return (
|
||||||
<div style={{
|
<DefaultTemplate
|
||||||
padding: '2rem',
|
i18n={initPageResult.req.i18n}
|
||||||
textAlign: 'center',
|
locale={initPageResult.locale}
|
||||||
color: 'var(--theme-warning-600)',
|
params={params}
|
||||||
backgroundColor: 'var(--theme-warning-50)',
|
payload={initPageResult.req.payload}
|
||||||
border: '1px solid var(--theme-warning-200)',
|
permissions={initPageResult.permissions}
|
||||||
borderRadius: '0.5rem',
|
searchParams={searchParams}
|
||||||
margin: '2rem 0'
|
user={initPageResult.req.user || undefined}
|
||||||
}}>
|
visibleEntities={initPageResult.visibleEntities}
|
||||||
<h2 style={{ marginBottom: '1rem', color: 'var(--theme-warning-700)' }}>
|
>
|
||||||
Access Denied
|
<Gutter>
|
||||||
</h2>
|
<div style={{
|
||||||
<p style={{ marginBottom: '1rem' }}>
|
padding: '2rem',
|
||||||
You don't have permission to access the Feature Flags Dashboard.
|
textAlign: 'center',
|
||||||
</p>
|
color: 'var(--theme-warning-600)',
|
||||||
<p style={{ fontSize: '0.875rem', color: 'var(--theme-warning-600)' }}>
|
backgroundColor: 'var(--theme-warning-50)',
|
||||||
Contact your administrator to request access to the {collectionConfig.slug} collection.
|
border: '1px solid var(--theme-warning-200)',
|
||||||
</p>
|
borderRadius: '0.5rem',
|
||||||
</div>
|
margin: '2rem 0'
|
||||||
|
}}>
|
||||||
|
<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(payload, collectionConfig.slug)
|
const initialFlags = await fetchInitialFlags(initPageResult.req.payload, searchParams)
|
||||||
|
|
||||||
// Check if user can update feature flags
|
// Check if user can update feature flags (use already defined collection slug)
|
||||||
const canUpdateFeatureFlags = permissions?.collections?.[collectionConfig.slug]?.update || false
|
const canUpdateFeatureFlags = permissions?.collections?.[collectionSlug]?.update || false
|
||||||
|
|
||||||
|
// Use DefaultTemplate with proper props structure from initPageResult
|
||||||
return (
|
return (
|
||||||
<FeatureFlagsClient
|
<DefaultTemplate
|
||||||
initialFlags={initialFlags}
|
i18n={initPageResult.req.i18n}
|
||||||
canUpdate={canUpdateFeatureFlags}
|
locale={initPageResult.locale}
|
||||||
maxFlags={100}
|
params={params}
|
||||||
collectionSlug={collectionConfig.slug}
|
payload={initPageResult.req.payload}
|
||||||
/>
|
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