Refactor env check.

This commit is contained in:
Mike Cao 2022-10-27 16:42:57 -07:00
parent ba63991a92
commit fc9584eb44
14 changed files with 30 additions and 76 deletions

View File

@ -17,7 +17,7 @@ import styles from './Header.module.css';
export default function Header() {
const { user } = useUser();
const { pathname } = useRouter();
const { updatesDisabled } = useConfig();
const { updatesDisabled, adminDisabled } = useConfig();
const isSharePage = pathname.includes('/share/');
const allowUpdate = user?.isAdmin && !updatesDisabled && !isSharePage;
@ -38,7 +38,7 @@ export default function Header() {
<Link href="/realtime">
<FormattedMessage id="label.realtime" defaultMessage="Realtime" />
</Link>
{!process.env.isAdminDisabled && (
{!adminDisabled && (
<Link href="/settings">
<FormattedMessage id="label.settings" defaultMessage="Settings" />
</Link>

View File

@ -8,10 +8,12 @@ import User from 'assets/user.svg';
import styles from './UserButton.module.css';
import { AUTH_TOKEN } from 'lib/constants';
import useUser from 'hooks/useUser';
import useConfig from 'hooks/useConfig';
export default function UserButton() {
const { user } = useUser();
const router = useRouter();
const { adminDisabled } = useConfig();
const menuOptions = [
{
@ -28,7 +30,7 @@ export default function UserButton() {
{
label: <FormattedMessage id="label.profile" defaultMessage="Profile" />,
value: 'profile',
hidden: process.env.isAdminDisabled,
hidden: adminDisabled,
},
{ label: <FormattedMessage id="label.logout" defaultMessage="Logout" />, value: 'logout' },
];

View File

@ -36,7 +36,6 @@ module.exports = {
env: {
currentVersion: pkg.version,
isProduction: process.env.NODE_ENV === 'production',
isAdminDisabled: process.env.DISABLE_ADMIN,
},
basePath: process.env.BASE_PATH,
output: 'standalone',

View File

@ -2,6 +2,7 @@ import Head from 'next/head';
import { useRouter } from 'next/router';
import { IntlProvider } from 'react-intl';
import useLocale from 'hooks/useLocale';
import useConfig from 'hooks/useConfig';
import 'styles/variables.css';
import 'styles/bootstrap-grid.css';
import 'styles/index.css';
@ -23,6 +24,11 @@ const Intl = ({ children }) => {
export default function App({ Component, pageProps }) {
const { basePath } = useRouter();
const { dir } = useLocale();
const { uiDisabled } = useConfig();
if (uiDisabled) {
return null;
}
return (
<Intl>

View File

@ -7,6 +7,9 @@ export default async (req, res) => {
trackerScriptName: process.env.TRACKER_SCRIPT_NAME,
updatesDisabled: !!process.env.DISABLE_UPDATES,
telemetryDisabled: !!process.env.DISABLE_TELEMETRY,
loginDisabled: !!process.env.DISABLE_LOGIN,
adminDisabled: !!process.env.DISABLE_ADMIN,
uiDisabled: !!process.env.DISABLE_UI,
});
}

View File

@ -4,11 +4,11 @@ import TestConsole from 'components/pages/TestConsole';
import useRequireLogin from 'hooks/useRequireLogin';
import useUser from 'hooks/useUser';
export default function ConsolePage({ enabled, pageDisabled }) {
export default function ConsolePage({ pageDisabled }) {
const { loading } = useRequireLogin();
const { user } = useUser();
if (pageDisabled || loading || !enabled || !user?.isAdmin) {
if (pageDisabled || loading || !user?.isAdmin) {
return null;
}
@ -22,8 +22,7 @@ export default function ConsolePage({ enabled, pageDisabled }) {
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
enabled: !!process.env.ENABLE_TEST_CONSOLE,
pageDisabled: !process.env.ENABLE_TEST_CONSOLE,
},
};
}

View File

@ -5,7 +5,7 @@ import useRequireLogin from 'hooks/useRequireLogin';
import { useRouter } from 'next/router';
import useUser from 'hooks/useUser';
export default function DashboardPage({ pageDisabled }) {
export default function DashboardPage() {
const {
query: { id },
isReady,
@ -14,7 +14,7 @@ export default function DashboardPage({ pageDisabled }) {
const { loading } = useRequireLogin();
const user = useUser();
if (pageDisabled || !user || !isReady || loading) {
if (!user || !isReady || loading) {
return null;
}
@ -26,11 +26,3 @@ export default function DashboardPage({ pageDisabled }) {
</Layout>
);
}
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
},
};
}

View File

@ -17,8 +17,7 @@ export default function LoginPage({ pageDisabled }) {
export async function getServerSideProps() {
return {
props: {
pageDisabled:
!!process.env.DISABLE_LOGIN || !!process.env.DISABLE_UI || !!process.env.isAdminDisabled,
pageDisabled: !!(process.env.DISABLE_LOGIN || process.env.DISABLE_ADMIN),
},
};
}

View File

@ -3,10 +3,10 @@ import Layout from 'components/layout/Layout';
import RealtimeDashboard from 'components/pages/RealtimeDashboard';
import useRequireLogin from 'hooks/useRequireLogin';
export default function RealtimePage({ pageDisabled }) {
export default function RealtimePage() {
const { loading } = useRequireLogin();
if (pageDisabled || loading) {
if (loading) {
return null;
}
@ -16,11 +16,3 @@ export default function RealtimePage({ pageDisabled }) {
</Layout>
);
}
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
},
};
}

View File

@ -1,11 +1,3 @@
import Index from './index';
export default Index;
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
},
};
}

View File

@ -2,11 +2,13 @@ import React from 'react';
import Layout from 'components/layout/Layout';
import Settings from 'components/pages/Settings';
import useRequireLogin from 'hooks/useRequireLogin';
import useConfig from 'hooks/useConfig';
export default function SettingsPage({ pageDisabled }) {
export default function SettingsPage() {
const { loading } = useRequireLogin();
const { adminDisabled } = useConfig();
if (pageDisabled || loading) {
if (adminDisabled || loading) {
return null;
}
@ -16,11 +18,3 @@ export default function SettingsPage({ pageDisabled }) {
</Layout>
);
}
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI || !!process.env.isAdminDisabled,
},
};
}

View File

@ -1,11 +1,3 @@
import Index from './index';
export default Index;
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
},
};
}

View File

@ -4,13 +4,13 @@ import Layout from 'components/layout/Layout';
import WebsiteDetails from 'components/pages/WebsiteDetails';
import useShareToken from 'hooks/useShareToken';
export default function SharePage({ pageDisabled }) {
export default function SharePage() {
const router = useRouter();
const { id } = router.query;
const shareId = id?.[0];
const shareToken = useShareToken(shareId);
if (pageDisabled || !shareToken) {
if (!shareToken) {
return null;
}
@ -20,11 +20,3 @@ export default function SharePage({ pageDisabled }) {
</Layout>
);
}
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
},
};
}

View File

@ -4,12 +4,12 @@ import Layout from 'components/layout/Layout';
import WebsiteDetails from 'components/pages/WebsiteDetails';
import useRequireLogin from 'hooks/useRequireLogin';
export default function DetailsPage({ pageDisabled }) {
export default function DetailsPage() {
const { loading } = useRequireLogin();
const router = useRouter();
const { id } = router.query;
if (pageDisabled || !id || loading) {
if (!id || loading) {
return null;
}
@ -21,11 +21,3 @@ export default function DetailsPage({ pageDisabled }) {
</Layout>
);
}
export async function getServerSideProps() {
return {
props: {
pageDisabled: !!process.env.DISABLE_UI,
},
};
}