Skip to content

Commit 4118874

Browse files
committed
feat: support feature flags
1 parent 7f77a5d commit 4118874

File tree

6 files changed

+211
-77
lines changed

6 files changed

+211
-77
lines changed

apps/app/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
NEXT_PUBLIC_APP_URL=https://${VERCEL_URL}
2+
3+
FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY=

apps/app/.env.local-example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
NEXT_PUBLIC_API_URL=http://api.devfaq.localhost:3002
22
NEXT_PUBLIC_APP_URL=http://app.devfaq.localhost:3000
3+
4+
FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY=

apps/app/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"@tanstack/react-query-devtools": "4.20.4",
2121
"client-only": "0.0.1",
2222
"easymde": "2.18.0",
23+
"flagsmith": "3.15.1",
24+
"flagsmith-nodejs": "2.5.0",
2325
"next": "13.1.1",
2426
"next-mdx-remote": "4.2.0",
2527
"openapi-typescript-fetch": "1.1.3",
@@ -29,6 +31,7 @@
2931
"react-focus-lock": "2.9.2",
3032
"rehype-prism-plus": "1.5.0",
3133
"remark": "14.0.2",
34+
"server-only": "0.0.1",
3235
"strip-markdown": "5.0.0",
3336
"tailwind-merge": "1.8.1"
3437
},

apps/app/src/app/(main-layout)/questions/p/[questionId]/page.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SingleQuestion } from "../../../../../components/SingleQuestion";
55
import { serializeQuestionToMarkdown } from "../../../../../lib/question";
66
import { getQuestionAnswers, getQuestionById } from "../../../../../services/questions.service";
77
import { Params } from "../../../../../types";
8+
import { isFlagEnabled } from "../../../../../services/flagsmith.service";
89

910
export default async function SingleQuestionPage({ params }: { params: Params<"questionId"> }) {
1011
const questionId = Number.parseInt(params.questionId);
@@ -13,14 +14,20 @@ export default async function SingleQuestionPage({ params }: { params: Params<"q
1314
return redirect("/");
1415
}
1516

16-
const [questionData, answersData] = await Promise.all([
17+
const [questionData, answersData, questionAnswersEnabled] = await Promise.all([
1718
getQuestionById({
1819
id: questionId,
1920
}),
2021
getQuestionAnswers({ id: questionId }),
22+
isFlagEnabled("question_answers"),
2123
]);
2224

2325
const question = await serializeQuestionToMarkdown(questionData.data.data);
26+
27+
if (!questionAnswersEnabled) {
28+
return <SingleQuestion question={question} />;
29+
}
30+
2431
const answers = await Promise.all(
2532
answersData.data.data.map(async ({ content, ...rest }) => {
2633
const mdxContent = await serializeSource(content);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "server-only";
2+
3+
import Flagsmith from "flagsmith-nodejs";
4+
import { BaseFlag } from "flagsmith-nodejs/build/sdk/models";
5+
6+
type Flag = "question_answers";
7+
const defaultFlags: Record<Flag, BaseFlag> = {
8+
question_answers: { enabled: true, value: undefined, isDefault: true },
9+
};
10+
11+
const getFlagsmith = async () => {
12+
if (!process.env.FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY) {
13+
console.warn(`FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY not provided.`);
14+
return null;
15+
}
16+
17+
const flagsmith = new Flagsmith({
18+
environmentKey: process.env.FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY,
19+
});
20+
const flags = await flagsmith.getEnvironmentFlags();
21+
return flags;
22+
};
23+
24+
const getFlag = async <F extends Flag>(flag: F) => {
25+
const flagsmith = await getFlagsmith();
26+
if (!flagsmith) {
27+
return defaultFlags[flag];
28+
}
29+
return flagsmith.getFlag(flag);
30+
};
31+
32+
export const isFlagEnabled = async <F extends Flag>(flag: F) => {
33+
return (await getFlag(flag)).enabled;
34+
};

0 commit comments

Comments
 (0)