Skip to content

Commit 8b1379b

Browse files
committed
feat: allow controlling interface with environment variable
1 parent c5effe6 commit 8b1379b

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

app/(app)/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { ENABLE_STATISTICS_PAGE } from "@/lib/features";
12
import { redirect } from "next/navigation";
23

34
export default function App() {
5+
if (!ENABLE_STATISTICS_PAGE) {
6+
redirect("/challenges");
7+
}
8+
49
redirect("/statistics");
510
}

app/(app)/statistics/page.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
2+
import { ENABLE_STATISTICS_PAGE } from "@/lib/features";
3+
import { AlertCircle } from "lucide-react";
14
import type { Metadata } from "next";
25
import { Suspense } from "react";
36
import Board from "./_components/board";
@@ -10,6 +13,19 @@ export const metadata: Metadata = {
1013
};
1114

1215
export default function StatisticsPage() {
16+
if (!ENABLE_STATISTICS_PAGE) {
17+
return (
18+
<Alert>
19+
<AlertCircle />
20+
<AlertTitle>系統管理員停用了「統計資料」頁面。</AlertTitle>
21+
<AlertDescription>
22+
如果您需要統計資料,請聯絡系統管理員開啟。<br />
23+
如果您是系統管理員:請將環境變數中 NEXT_PUBLIC_FEATURE_STATISTICS_PAGE 的否定值改為 true。
24+
</AlertDescription>
25+
</Alert>
26+
);
27+
}
28+
1329
return (
1430
<div>
1531
<Board />

app/login/page.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Logo } from "@/components/logo";
2+
import { ENABLE_SOCIAL_PLATFORM } from "@/lib/features";
23
import type { Metadata } from "next";
34
import Link from "next/link";
45
import { Suspense } from "react";
@@ -59,13 +60,17 @@ export default async function LoginPage() {
5960
<UpstreamStatus />
6061
</Suspense>
6162

62-
<Separator />
63+
{ENABLE_SOCIAL_PLATFORM && (
64+
<>
65+
<Separator />
6366

64-
<GithubLink />
67+
<GithubLink />
6568

66-
<Separator />
69+
<Separator />
6770

68-
<DiscordLink />
71+
<DiscordLink />
72+
</>
73+
)}
6974
</div>
7075
</div>
7176
</div>

components/app-navbar.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AppAvatar from "@/components/avatar";
22
import { Logo } from "@/components/logo";
33
import { Button } from "@/components/ui/button";
44
import useUser from "@/hooks/use-user";
5+
import { ENABLE_SOCIAL_PLATFORM, ENABLE_STATISTICS_PAGE } from "@/lib/features";
56
import { cn } from "@/lib/utils";
67
import { BarChart3, BookOpen, ChevronDown, Menu, MessageSquare, Swords, X } from "lucide-react";
78
import Link from "next/link";
@@ -171,11 +172,13 @@ interface ExternalNavItem extends BaseNavItem {
171172
type NavItem = InternalNavItem | ExternalNavItem;
172173

173174
const navItems: NavItem[] = [
174-
{
175-
icon: <BarChart3 className="h-full w-full" />,
176-
label: "統計資料",
177-
pathPrefix: "/statistics",
178-
},
175+
ENABLE_STATISTICS_PAGE
176+
? {
177+
icon: <BarChart3 className="h-full w-full" />,
178+
label: "統計資料",
179+
pathPrefix: "/statistics",
180+
}
181+
: undefined,
179182
{
180183
icon: <Swords className="h-full w-full" />,
181184
label: "挑戰題目",
@@ -186,12 +189,14 @@ const navItems: NavItem[] = [
186189
label: "補充資料",
187190
pathPrefix: "/materials",
188191
},
189-
{
190-
icon: <MessageSquare className="h-full w-full" />,
191-
label: "意見分享",
192-
externalLink: "https://community.dbplay.app/discord",
193-
},
194-
];
192+
ENABLE_SOCIAL_PLATFORM
193+
? {
194+
icon: <MessageSquare className="h-full w-full" />,
195+
label: "意見分享",
196+
externalLink: "https://community.dbplay.app/discord",
197+
}
198+
: undefined,
199+
].filter((item) => item !== undefined);
195200

196201
function getActiveNavItemLabel(path: string): string | null {
197202
for (const item of navItems) {

lib/features.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const ENABLE_STATISTICS_PAGE = requireExplicitlyDisabled(process.env.NEXT_PUBLIC_FEATURE_STATISTICS_PAGE);
2+
export const ENABLE_SOCIAL_PLATFORM = requireExplicitlyDisabled(process.env.NEXT_PUBLIC_FEATURE_SOCIAL_PLATFORM);
3+
4+
/**
5+
* Return false if the feature is explicitly disabled; true otherwise.
6+
*/
7+
function requireExplicitlyDisabled(feature: string | undefined) {
8+
return !(feature === "false" || feature === "0");
9+
}

0 commit comments

Comments
 (0)