Skip to content

Commit

Permalink
Reintroduce featureFlags object to avoid passing secret envs to the f…
Browse files Browse the repository at this point in the history
…rontend
  • Loading branch information
jantuomi committed Feb 12, 2024
1 parent 8fdddb4 commit 987ace9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
9 changes: 2 additions & 7 deletions src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getExpense,
updateExpense,
} from '@/lib/api'
import { env } from '@/lib/env'
import { getRuntimeFeatureFlags } from '@/lib/featureFlags'
import { expenseFormSchema } from '@/lib/schemas'
import { Metadata } from 'next'
import { notFound, redirect } from 'next/navigation'
Expand Down Expand Up @@ -40,11 +40,6 @@ export default async function EditExpensePage({
redirect(`/groups/${groupId}`)
}

async function getEnvOnServer() {
'use server'
return env;
}

return (
<Suspense>
<ExpenseForm
Expand All @@ -53,7 +48,7 @@ export default async function EditExpensePage({
categories={categories}
onSubmit={updateExpenseAction}
onDelete={deleteExpenseAction}
env={await getEnvOnServer()}
runtimeFeatureFlags={await getRuntimeFeatureFlags()}
/>
</Suspense>
)
Expand Down
9 changes: 2 additions & 7 deletions src/app/groups/[groupId]/expenses/create/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cached } from '@/app/cached-functions'
import { ExpenseForm } from '@/components/expense-form'
import { createExpense, getCategories } from '@/lib/api'
import { env } from '@/lib/env'
import { getRuntimeFeatureFlags } from '@/lib/featureFlags'
import { expenseFormSchema } from '@/lib/schemas'
import { Metadata } from 'next'
import { notFound, redirect } from 'next/navigation'
Expand All @@ -27,18 +27,13 @@ export default async function ExpensePage({
redirect(`/groups/${groupId}`)
}

async function getEnvOnServer() {
'use server'
return env;
}

return (
<Suspense>
<ExpenseForm
group={group}
categories={categories}
onSubmit={createExpenseAction}
env={await getEnvOnServer()}
runtimeFeatureFlags={await getRuntimeFeatureFlags()}
/>
</Suspense>
)
Expand Down
9 changes: 5 additions & 4 deletions src/components/expense-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ import { useForm } from 'react-hook-form'
import { match } from 'ts-pattern'
import { extractCategoryFromTitle } from './expense-form-actions'
import type { env } from '@/lib/env'
import { RuntimeFeatureFlags } from '@/lib/featureFlags'

export type Props = {
group: NonNullable<Awaited<ReturnType<typeof getGroup>>>
expense?: NonNullable<Awaited<ReturnType<typeof getExpense>>>
categories: NonNullable<Awaited<ReturnType<typeof getCategories>>>
onSubmit: (values: ExpenseFormValues) => Promise<void>
onDelete?: () => Promise<void>
env: typeof env
runtimeFeatureFlags: RuntimeFeatureFlags
}

export function ExpenseForm({
Expand All @@ -61,7 +62,7 @@ export function ExpenseForm({
categories,
onSubmit,
onDelete,
env,
runtimeFeatureFlags,
}: Props) {
const isCreate = expense === undefined
const searchParams = useSearchParams()
Expand Down Expand Up @@ -163,7 +164,7 @@ export function ExpenseForm({
{...field}
onBlur={async () => {
field.onBlur() // avoid skipping other blur event listeners since we overwrite `field`
if (env.NEXT_PUBLIC_ENABLE_CATEGORY_EXTRACT) {
if (runtimeFeatureFlags.enableCategoryExtract) {
setCategoryLoading(true)
const { categoryId } = await extractCategoryFromTitle(
field.value,
Expand Down Expand Up @@ -543,7 +544,7 @@ export function ExpenseForm({
</CardContent>
</Card>

{env.NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS && (
{runtimeFeatureFlags.enableExpenseDocuments && (
<Card className="mt-4">
<CardHeader>
<CardTitle className="flex justify-between">
Expand Down
13 changes: 13 additions & 0 deletions src/lib/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use server"

import { env } from "./env"

export async function getRuntimeFeatureFlags() {
return {
enableExpenseDocuments: env.NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS,
enableReceiptExtract: env.NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT,
enableCategoryExtract: env.NEXT_PUBLIC_ENABLE_CATEGORY_EXTRACT,
};
}

export type RuntimeFeatureFlags = Awaited<ReturnType<typeof getRuntimeFeatureFlags>>;

0 comments on commit 987ace9

Please sign in to comment.