feat: update layout and components for improved user experience, add theme toggle and main navigation

This commit is contained in:
2025-09-06 21:44:52 +02:00
parent 8b82a5e48e
commit 5344e0d6a8
7 changed files with 208 additions and 113 deletions

View File

@@ -16,8 +16,8 @@ const geistMono = Geist_Mono({
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
title: "Thoughts",
description: "A social network for sharing thoughts",
};
export default function RootLayout({

View File

@@ -1,15 +1,13 @@
// app/page.tsx
import { cookies } from "next/headers";
import { getFeed, getMe, getUserProfile, Me, Thought } from "@/lib/api";
import { ThoughtCard } from "@/components/thought-card";
import { getFeed, getMe, getUserProfile, Me, User } from "@/lib/api";
import { PostThoughtForm } from "@/components/post-thought-form";
import { Button } from "@/components/ui/button";
import Link from "next/link";
import { PopularTags } from "@/components/popular-tags";
import { ThoughtThread } from "@/components/thought-thread";
import { buildThoughtThreads } from "@/lib/utils";
import { TopFriends } from "@/components/top-friends";
// This is now an async Server Component
export default async function Home() {
const token = (await cookies()).get("auth_token")?.value ?? null;
@@ -21,31 +19,42 @@ export default async function Home() {
}
async function FeedPage({ token }: { token: string }) {
const feedData = await getFeed(token);
const me = (await getMe(token).catch(() => null)) as Me | null;
const [feedData, me] = await Promise.all([
getFeed(token),
getMe(token).catch(() => null) as Promise<Me | null>,
]);
const authors = [...new Set(feedData.thoughts.map((t) => t.authorUsername))];
const userProfiles = await Promise.all(
authors.map((username) => getUserProfile(username, token).catch(() => null))
);
const authorDetails = new Map(
const authorDetails = new Map<string, { avatarUrl?: string | null }>(
userProfiles
.filter(Boolean)
.map((user) => [user!.username, { avatarUrl: user!.avatarUrl }])
.filter((u): u is User => !!u)
.map((user) => [user.username, { avatarUrl: user.avatarUrl }])
);
const allThoughts = feedData.thoughts;
const { topLevelThoughts, repliesByParentId } =
buildThoughtThreads(allThoughts);
const { topLevelThoughts, repliesByParentId } = buildThoughtThreads(
feedData.thoughts
);
return (
<div className="container mx-auto max-w-4xl p-4 sm:p-6 grid grid-cols-1 md:grid-cols-3 gap-8">
<main className="md:col-span-2 space-y-6">
<header className="my-6">
<div className="container mx-auto max-w-6xl p-4 sm:p-6">
<div className="grid grid-cols-1 lg:grid-cols-4 gap-8">
<aside className="hidden lg:block lg:col-span-1">
<div className="sticky top-20 space-y-6">
<h2 className="text-lg font-semibold">Filters & Sorting</h2>
<p className="text-sm text-muted-foreground">Coming soon...</p>
</div>
</aside>
<main className="col-span-1 lg:col-span-2 space-y-6">
<header className="mb-6">
<h1 className="text-3xl font-bold">Your Feed</h1>
</header>
<PostThoughtForm />
<main className="space-y-6">
<div className="space-y-6">
{topLevelThoughts.map((thought) => (
<ThoughtThread
key={thought.id}
@@ -57,15 +66,20 @@ async function FeedPage({ token }: { token: string }) {
))}
{topLevelThoughts.length === 0 && (
<p className="text-center text-muted-foreground pt-8">
Your feed is empty. Follow some users to see their thoughts here!
Your feed is empty. Follow some users to see their thoughts!
</p>
)}
</div>
</main>
</main>
<aside className="md:col-span-1 space-y-6 pt-20">
<aside className="hidden lg:block lg:col-span-1">
<div className="sticky top-20 space-y-6">
{me?.topFriends && <TopFriends usernames={me.topFriends} />}
<PopularTags />
</div>
</aside>
</div>
</div>
);
}

View File

@@ -62,33 +62,23 @@ export default async function ProfilePage({ params }: ProfilePageProps) {
}}
/>
<main className="container mx-auto max-w-3xl p-4 -mt-16 grid grid-cols-1 md:grid-cols-3 gap-8">
<aside className="md:col-span-1 space-y-6 pt-24">
<TopFriends usernames={user.topFriends} />
</aside>
<div className="md:col-span-2 mt-8 md:mt-0 space-y-4">
<main className="container mx-auto max-w-6xl p-4 -mt-16 grid grid-cols-1 lg:grid-cols-4 gap-8">
{/* Left Sidebar (Profile Card & Top Friends) */}
<aside className="col-span-1 lg:col-span-1 space-y-6">
<div className="sticky top-20 space-y-6">
<Card className="p-6 bg-card/80 backdrop-blur-lg">
<div className="flex justify-between items-start">
<div className="flex items-end gap-4">
<div className="w-24 h-24 rounded-full border-4 border-background shrink-0">
<UserAvatar src={user.avatarUrl} alt={user.displayName} />
</div>
<div>
<h1 className="text-2xl font-bold">
{user.displayName || user.username}
</h1>
<p className="text-sm text-muted-foreground">
@{user.username}
</p>
</div>
</div>
{/* Action Button */}
<div>
{isOwnProfile ? (
<Button asChild variant="outline">
<Button asChild variant="outline" size="sm">
<Link href="/settings/profile">
<Settings className="mr-2 h-4 w-4" />
Settings
<Settings className="mr-2 h-4 w-4" /> Edit
</Link>
</Button>
) : token ? (
@@ -100,15 +90,31 @@ export default async function ProfilePage({ params }: ProfilePageProps) {
</div>
</div>
<p className="mt-4 whitespace-pre-wrap">{user.bio}</p>
<div className="mt-4">
<h1 className="text-2xl font-bold">
{user.displayName || user.username}
</h1>
<p className="text-sm text-muted-foreground">
@{user.username}
</p>
</div>
<p className="mt-4 text-sm whitespace-pre-wrap">{user.bio}</p>
<div className="flex items-center gap-2 mt-4 text-sm text-muted-foreground">
<Calendar className="h-4 w-4" />
<span>Joined {new Date(user.joinedAt).toLocaleDateString()}</span>
<span>
Joined {new Date(user.joinedAt).toLocaleDateString()}
</span>
</div>
</Card>
{/* Thoughts Feed */}
<div className="mt-8 space-y-4">
<TopFriends usernames={user.topFriends} />
</div>
</aside>
{/* Main Content (Thoughts Feed) */}
<div className="col-span-1 lg:col-span-3 space-y-4">
{topLevelThoughts.map((thought) => (
<ThoughtThread
key={thought.id}
@@ -119,13 +125,13 @@ export default async function ProfilePage({ params }: ProfilePageProps) {
/>
))}
{topLevelThoughts.length === 0 && (
<p className="text-center text-muted-foreground pt-8">
Your feed is empty. Follow some users to see their thoughts
here!
<Card className="flex items-center justify-center h-48">
<p className="text-center text-muted-foreground">
This user hasn&apos;t posted any public thoughts yet.
</p>
</Card>
)}
</div>
</div>
</main>
</div>
);

View File

@@ -1,37 +1,39 @@
// components/header.tsx
"use client";
import { useAuth } from "@/hooks/use-auth";
import Link from "next/link";
import { Button } from "./ui/button";
import { UserNav } from "./user-nav";
import { MainNav } from "./main-nav";
import { ThemeToggle } from "./theme-toggle";
import { Wind } from "lucide-react";
export function Header() {
const { token } = useAuth();
return (
<header className="sticky top-0 z-50 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
<div className="container flex h-14 items-center">
<div className="mr-4 flex">
<Link href="/" className="mr-6 flex items-center space-x-2">
<span className="font-bold">Thoughts</span>
<div className="w-full flex h-14 items-center px-2">
<div className="flex gap-2">
<Link href="/" className="flex items-center gap-1">
<span className="hidden font-bold sm:inline-block">Thoughts</span>
</Link>
<MainNav />
</div>
<div className="flex flex-1 items-center justify-end space-x-4">
<nav className="flex items-center space-x-2">
<div className="flex flex-1 items-center justify-end space-x-2">
<ThemeToggle />
{token ? (
<UserNav />
) : (
<>
<Button asChild variant="ghost">
<Button asChild variant="ghost" size="sm">
<Link href="/login">Login</Link>
</Button>
<Button asChild>
<Button asChild size="sm">
<Link href="/register">Register</Link>
</Button>
</>
)}
</nav>
</div>
</div>
</header>

View File

@@ -0,0 +1,22 @@
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { cn } from "@/lib/utils";
export function MainNav() {
const pathname = usePathname();
return (
<nav className="hidden md:flex items-center space-x-6 text-sm font-medium">
<Link
href="/"
className={cn(
"transition-colors hover:text-foreground/80",
pathname === "/" ? "text-foreground" : "text-foreground/60"
)}
>
Feed
</Link>
</nav>
);
}

View File

@@ -0,0 +1,11 @@
"use client";
import * as React from "react";
import {
ThemeProvider as NextThemesProvider,
ThemeProviderProps,
} from "next-themes";
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
}

View File

@@ -0,0 +1,40 @@
"use client";
import * as React from "react";
import { Moon, Sun } from "lucide-react";
import { useTheme } from "next-themes";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
export function ThemeToggle() {
const { setTheme } = useTheme();
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon">
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
<span className="sr-only">Toggle theme</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => setTheme("light")}>
Light
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("dark")}>
Dark
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("system")}>
System
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}