Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Revert server side nav (#849)

Sean Boult1e04619b9e98
diff --git a/apps/web/src/app/layout.tsx b/apps/web/src/app/layout.tsx
index f00fdc8a..dd89a5e9 100644
--- a/apps/web/src/app/layout.tsx
+++ b/apps/web/src/app/layout.tsx
@@ -6,10 +6,8 @@ import { Inter } from 'next/font/google';
import type { Metadata } from 'next';
import { Toaster } from '@repo/ui/components/toaster';
import { Providers } from './providers';
-import { getServerAuthSession } from '@repo/auth/server';
import { buildMetaForDefault } from './metadata';
import { Navigation } from '~/components/navigation';
-import { getAllFlags } from '~/utils/feature-flags';
const inter = Inter({ subsets: ['latin'] });
@@ -17,10 +15,7 @@ export async function generateMetadata(): Promise<Metadata> {
return buildMetaForDefault({});
}
-export default async function RootLayout({ children }: { children: React.ReactNode }) {
- const flags = await getAllFlags();
- const session = await getServerAuthSession();
-
+export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html suppressHydrationWarning lang="en">
<head>
@@ -36,7 +31,7 @@ export default async function RootLayout({ children }: { children: React.ReactNo
</head>
<body className={`${inter.className} flex flex-col`}>
<Providers>
- <Navigation session={session} flags={flags} />
+ <Navigation />
<main className="flex-1">{children}</main>
<Toaster />
</Providers>
diff --git a/apps/web/src/components/navigation.tsx b/apps/web/src/components/navigation.tsx
index a951832a..0f70843b 100644
--- a/apps/web/src/components/navigation.tsx
+++ b/apps/web/src/components/navigation.tsx
@@ -1,7 +1,7 @@
'use client';
-import { signIn, signOut } from '@repo/auth/react';
-import type { Session } from '@repo/auth/server';
+import { signIn, signOut, useSession } from '@repo/auth/react';
+import { type RoleTypes } from '@repo/db/types';
import { Button } from '@repo/ui/components/button';
import {
DropdownMenu,
@@ -11,16 +11,15 @@ import {
DropdownMenuTrigger,
} from '@repo/ui/components/dropdown-menu';
import { Loader2, LogIn, Moon, Play, Settings, Settings2, Sun, User } from '@repo/ui/icons';
-import type { EdgeConfigItems } from '@vercel/edge-config';
import clsx from 'clsx';
import { useTheme } from 'next-themes';
import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation';
-import { useEffect, useState } from 'react';
+import { useContext, useEffect, useState } from 'react';
import { useFullscreenSettingsStore } from '~/app/[locale]/challenge/_components/fullscreen';
+import { FeatureFlagContext } from '~/app/feature-flag-provider';
import { isAdminOrModerator } from '~/utils/auth-guards';
-// TODO: move this to a util as it might be used in other places
export function getAdminUrl() {
// reference for vercel.com
if (process.env.VERCEL_URL) {
@@ -31,15 +30,18 @@ export function getAdminUrl() {
return `http://localhost:3001`;
}
-export function Navigation({
- session,
- flags,
-}: {
- session: Session | null;
- flags: EdgeConfigItems;
-}) {
+const roleTypes: typeof RoleTypes = {
+ USER: 'USER',
+ ADMIN: 'ADMIN',
+ MODERATOR: 'MODERATOR',
+ CREATOR: 'CREATOR',
+};
+
+export function Navigation() {
const { fssettings } = useFullscreenSettingsStore();
const pathname = usePathname();
+ const { data } = useSession();
+ const featureFlags = useContext(FeatureFlagContext);
return (
<header className="z-0 w-full">
@@ -80,7 +82,7 @@ export function Navigation({
hero
</span>
</Link>
- {flags?.exploreButton === true && (
+ {featureFlags?.exploreButton ? (
<Link href="/explore" className="ml-4">
<div
className={clsx('hover:text-foreground text-foreground/80 transition-colors', {
@@ -90,8 +92,8 @@ export function Navigation({
Explore
</div>
</Link>
- )}
- {flags?.tracksButton === true && (
+ ) : null}
+ {featureFlags?.tracksButton ? (
<Link href="/tracks" className="ml-4">
<div
className={clsx('hover:text-foreground text-foreground/80 transition-colors', {
@@ -101,12 +103,12 @@ export function Navigation({
Tracks
</div>
</Link>
- )}
+ ) : null}
</div>
<div className="flex">
<div className="flex items-center justify-end gap-2">
<ThemeButton />
- {flags?.loginButton === true && <LoginButton session={session} />}
+ {featureFlags?.loginButton ? <LoginButton /> : null}
</div>
</div>
</div>
@@ -143,21 +145,24 @@ function ThemeButton() {
);
}
-function LoginButton({ session }: { session: Session | null }) {
- const [isSigningIn, setSigningIn] = useState(false);
+function LoginButton() {
+ const [loading, setLoading] = useState(false);
+ const { data: session, status } = useSession();
const router = useRouter();
+
const isAdminOrMod = isAdminOrModerator(session);
- // We start the flow client side, then a full page redirect will happen
+ // NOTE: 1. loading == true -> 2. signIn() -> 3. session status == 'loading' (loading == false)
const handleSignIn = async () => {
try {
- setSigningIn(true);
+ setLoading(true);
+ // page reloads after sign in, so no need to setLoading(false), othersiwe ugly visual glitch
await signIn('github', { redirect: false });
} catch (error) {
- console.error(error);
+ // only set loading to false if there was an error and page didn't reload after sign in
+ setLoading(false);
}
};
-
const handleSignOut = async () => {
await signOut({ redirect: false });
router.refresh();
@@ -189,22 +194,22 @@ function LoginButton({ session }: { session: Session | null }) {
<span>Settings</span>
</DropdownMenuItem>
</Link>
- {Boolean(isAdminOrMod) && (
+ {isAdminOrMod ? (
<a className="block" href={getAdminUrl()}>
<DropdownMenuItem className="focus:bg-accent rounded-lg p-2 duration-300 focus:outline-none dark:hover:bg-neutral-700/50">
<Settings className="mr-2 h-4 w-4" />
<span>Admin</span>
</DropdownMenuItem>
</a>
- )}
- {Boolean(isAdminOrMod) && (
+ ) : null}
+ {isAdminOrMod ? (
<a className="block" href="/challenge-playground">
<DropdownMenuItem className="focus:bg-accent rounded-lg p-2 duration-300 focus:outline-none dark:hover:bg-neutral-700/50">
<Play className="mr-2 h-4 w-4" />
<span>Challenge Playground</span>
</DropdownMenuItem>
</a>
- )}
+ ) : null}
<DropdownMenuSeparator />
<Button
className="h-8 w-full justify-start rounded-b-lg rounded-t-sm bg-opacity-50 px-2 text-red-500 hover:bg-red-500/20 hover:text-red-500"
@@ -217,11 +222,11 @@ function LoginButton({ session }: { session: Session | null }) {
</DropdownMenu>
) : (
<Button
- disabled={isSigningIn}
className="focus:bg-accent w-20 rounded-lg bg-transparent p-2 text-black duration-300 hover:bg-gray-200 focus:outline-none dark:text-white hover:dark:bg-gray-800"
+ disabled={loading || status === 'loading'}
onClick={handleSignIn}
>
- {isSigningIn ? (
+ {loading || status === 'loading' ? (
<Loader2 className="h-5 w-5 animate-spin" />
) : (
<div className="flex items-center space-x-2">
diff --git a/tooling/config-eslint/utils/rules.js b/tooling/config-eslint/utils/rules.js
index eb12ac79..95ebb4e6 100644
--- a/tooling/config-eslint/utils/rules.js
+++ b/tooling/config-eslint/utils/rules.js
@@ -41,6 +41,7 @@ module.exports = {
'no-control-regex': 'off',
'no-return-await': 'off',
'@typescript-eslint/require-await': 'off',
+
// Best practice/readability
// Some of these might be opinions.
// You can ignore if you'd like.