Skip to content

Commit

Permalink
fix: Client side feature flag check
Browse files Browse the repository at this point in the history
  • Loading branch information
hampfh committed Nov 9, 2024
1 parent 1f9e98b commit 4b8f21b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app/student/events/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async function EventDetailsPage({
}: {
params: { id: string }
}) {
if (!feature("EVENT_PAGE")) {
if (!(await feature("EVENT_PAGE"))) {
return notFound()
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/student/events/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fetchEvents } from "@/components/shared/hooks/api/useEvents"
import { notFound } from "next/navigation"

export default async function StudentEventPage() {
if (!feature("EVENT_PAGE")) {
if (!(await feature("EVENT_PAGE"))) {
return notFound()
}
const events = await fetchEvents()
Expand Down
12 changes: 8 additions & 4 deletions src/components/shared/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import featureFlags, { FEATURE_FLAGS } from "@/feature_flags"
import { decrypt, FlagOverridesType } from "@vercel/flags"
import { cookies } from "next/headers"

export async function feature(feature: keyof typeof featureFlags) {
export async function features() {
const overrideCookie = cookies().get("vercel-flag-overrides")?.value
const overrides = overrideCookie
? await decrypt<FlagOverridesType>(overrideCookie)
: {}
return {
...FEATURE_FLAGS,
...overrides
}
}

const result = overrides?.[feature] ?? FEATURE_FLAGS[feature] ?? false
console.log("RESULT", result)
return result
export async function feature(feature: keyof typeof featureFlags) {
return (await features())[feature] ?? false
}
16 changes: 16 additions & 0 deletions src/components/shared/hooks/useFeature.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { features } from "@/components/shared/feature"
import FEATURE_FLAGS from "@/feature_flags"
import { useQuery } from "@tanstack/react-query"

export function useFeature(feature?: keyof typeof FEATURE_FLAGS) {
const { data, isLoading } = useQuery({
queryKey: ["feature-flags"],
queryFn: async () => await features()
})

return {
isLoading,
enabled: feature ? data?.[feature] : null,
flags: data
}
}

0 comments on commit 4b8f21b

Please sign in to comment.