mirror of
https://github.com/xtr-dev/payload-feature-flags.git
synced 2025-12-10 19:03:25 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be24beeaa5 | ||
| 05952e3e72 | |||
|
|
e6f56535ca | ||
| eefe9bdaf3 | |||
| 49c10c7091 | |||
|
|
fe3da4fe52 | ||
| e82bf9c6d4 | |||
| 460d627d92 | |||
|
|
14a5acd222 | ||
| d3b8a8446e | |||
| 7dc17bc80a | |||
| b642b653d0 | |||
|
|
1db434e701 | ||
| 7fd6194712 | |||
| 259599ddcc | |||
|
|
7f54d9a79f | ||
| 9bb5f4ecc8 | |||
| 4f802c8cc9 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "payload-feature-flags",
|
"name": "payload-feature-flags",
|
||||||
"version": "1.0.0",
|
"version": "0.0.19",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "payload-feature-flags",
|
"name": "payload-feature-flags",
|
||||||
"version": "1.0.0",
|
"version": "0.0.19",
|
||||||
"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.12",
|
"version": "0.0.20",
|
||||||
"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,4 +7,5 @@ export {
|
|||||||
useRolloutCheck,
|
useRolloutCheck,
|
||||||
withFeatureFlag,
|
withFeatureFlag,
|
||||||
type FeatureFlag,
|
type FeatureFlag,
|
||||||
|
type FeatureFlagOptions,
|
||||||
} from '../hooks/client.js'
|
} from '../hooks/client.js'
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import React, { useCallback, useEffect, useState } from 'react'
|
import React, { useCallback, useEffect, useState, useRef } from 'react'
|
||||||
import { useConfig } from '@payloadcms/ui'
|
|
||||||
|
|
||||||
export interface FeatureFlag {
|
export interface FeatureFlag {
|
||||||
name: string
|
name: string
|
||||||
@@ -14,34 +13,78 @@ 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 { config } = useConfig()
|
const { serverURL, apiPath, collectionSlug } = getConfig(options)
|
||||||
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 = initialFlags.map(f => f.name).filter(Boolean)
|
const names = initialFlagsRef.current.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(`${config.serverURL}${config.routes.api}/feature-flags${query}`)
|
const response = await fetch(`${serverURL}${apiPath}/${collectionSlug}${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}`)
|
||||||
@@ -64,7 +107,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 = initialFlags.map(initialFlag => {
|
const sortedFlags = initialFlagsRef.current.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
|
||||||
@@ -77,7 +120,7 @@ export function useFeatureFlags(
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}, [config.serverURL, config.routes.api, initialFlags])
|
}, [serverURL, apiPath, collectionSlug]) // Remove initialFlags from dependencies
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void fetchFlags()
|
void fetchFlags()
|
||||||
@@ -89,13 +132,16 @@ 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(flagName: string): {
|
export function useFeatureFlag(
|
||||||
|
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 }])
|
const { flags, loading, error } = useFeatureFlags([{ name: flagName }], options)
|
||||||
|
|
||||||
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
|
||||||
@@ -106,13 +152,16 @@ export function useFeatureFlag(flagName: string): {
|
|||||||
/**
|
/**
|
||||||
* Hook to fetch a specific feature flag from the API
|
* Hook to fetch a specific feature flag from the API
|
||||||
*/
|
*/
|
||||||
export function useSpecificFeatureFlag(flagName: string): {
|
export function useSpecificFeatureFlag(
|
||||||
|
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 { config } = useConfig()
|
const { serverURL, apiPath, collectionSlug } = getConfig(options)
|
||||||
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)
|
||||||
@@ -124,7 +173,7 @@ export function useSpecificFeatureFlag(flagName: string): {
|
|||||||
|
|
||||||
// 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(
|
||||||
`${config.serverURL}${config.routes.api}/feature-flags?where[name][equals]=${flagName}&limit=1`
|
`${serverURL}${apiPath}/${collectionSlug}?where[name][equals]=${flagName}&limit=1`
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -153,7 +202,7 @@ export function useSpecificFeatureFlag(flagName: string): {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}, [config.serverURL, config.routes.api, flagName])
|
}, [serverURL, apiPath, collectionSlug, flagName])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void fetchFlag()
|
void fetchFlag()
|
||||||
@@ -167,14 +216,15 @@ export function useSpecificFeatureFlag(flagName: string): {
|
|||||||
*/
|
*/
|
||||||
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)
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName, options)
|
||||||
|
|
||||||
const variant = flag?.enabled && flag.variants
|
const variant = flag?.enabled && flag.variants
|
||||||
? selectVariantForUser(userId, flag.variants)
|
? selectVariantForUser(userId, flag.variants)
|
||||||
@@ -188,14 +238,15 @@ 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)
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName, options)
|
||||||
|
|
||||||
const isInRollout = flag?.enabled
|
const isInRollout = flag?.enabled
|
||||||
? checkUserInRollout(userId, flag.rolloutPercentage || 100)
|
? checkUserInRollout(userId, flag.rolloutPercentage || 100)
|
||||||
@@ -253,13 +304,14 @@ 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)
|
const { isEnabled, loading } = useFeatureFlag(flagName, options)
|
||||||
|
|
||||||
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,6 +34,11 @@ 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 =
|
||||||
@@ -44,6 +49,7 @@ export const payloadFeatureFlags =
|
|||||||
defaultValue = false,
|
defaultValue = false,
|
||||||
enableRollouts = true,
|
enableRollouts = true,
|
||||||
enableVariants = true,
|
enableVariants = true,
|
||||||
|
enableCustomListView = false,
|
||||||
collectionOverrides,
|
collectionOverrides,
|
||||||
} = pluginOptions
|
} = pluginOptions
|
||||||
|
|
||||||
@@ -163,6 +169,15 @@ 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,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,
|
||||||
@@ -236,7 +238,7 @@ const FeatureFlagsClientComponent = ({
|
|||||||
color: styles.text,
|
color: styles.text,
|
||||||
margin: '0 0 0.5rem 0'
|
margin: '0 0 0.5rem 0'
|
||||||
}}>
|
}}>
|
||||||
Feature Flags Dashboard
|
Feature Flags
|
||||||
</h1>
|
</h1>
|
||||||
<p style={{
|
<p style={{
|
||||||
color: styles.textMuted,
|
color: styles.textMuted,
|
||||||
@@ -664,4 +666,4 @@ const FeatureFlagsClientComponent = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const FeatureFlagsClient = memo(FeatureFlagsClientComponent)
|
export const FeatureFlagsClient = memo(FeatureFlagsClientComponent)
|
||||||
export default FeatureFlagsClient
|
export default FeatureFlagsClient
|
||||||
|
|||||||
@@ -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