From 10d03da196b15ab4d8b0b290f5d40be90cc13adf Mon Sep 17 00:00:00 2001 From: sirin Date: Sun, 6 Oct 2024 14:07:46 +0700 Subject: [PATCH] refactor: extract client component from navbar --- src/components/navigationBar/nav.tsx | 134 +------------------- src/components/navigationBar/profileBar.tsx | 100 +++++++++++++++ src/components/navigationBar/serchBar.tsx | 35 +++++ 3 files changed, 139 insertions(+), 130 deletions(-) create mode 100644 src/components/navigationBar/profileBar.tsx create mode 100644 src/components/navigationBar/serchBar.tsx diff --git a/src/components/navigationBar/nav.tsx b/src/components/navigationBar/nav.tsx index 940e917..9fc31a7 100644 --- a/src/components/navigationBar/nav.tsx +++ b/src/components/navigationBar/nav.tsx @@ -1,5 +1,3 @@ -"use client"; - import * as React from "react"; import Link from "next/link"; import Image from "next/image"; @@ -7,8 +5,6 @@ import Image from "next/image"; import { cn } from "@/lib/utils"; import { Separator } from "@/components/ui/separator"; import { ThemeToggle } from "@/components/theme-toggle"; -import { Button } from "@/components/ui/button"; -import { useRouter } from "next/navigation"; import { NavigationMenu, NavigationMenuContent, @@ -16,36 +12,10 @@ import { NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger, - navigationMenuTriggerStyle, } from "@/components/ui/navigation-menu"; -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuLabel, - DropdownMenuSeparator, - DropdownMenuTrigger, -} from "@/components/ui/dropdown-menu"; -import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; -import { Skeleton } from "@/components/ui/skeleton"; -import { Search, Bell, Heart, Wallet } from "lucide-react"; +import { SearchBar } from "./serchBar"; +import { ProfileBar } from "./profileBar"; -import { LogoutButton } from "@/components/auth/logoutButton"; - -import useSession from "@/lib/supabase/useSession"; - -const landings = [ - { - id: 1, - title: "Landing 01", - route: "/project-management", - }, - { - id: 2, - title: "Landing 02", - route: "/crm-landing", - }, -]; const ListItem = React.forwardRef, React.ComponentPropsWithoutRef<"a">>( ({ className, title, children, ...props }, ref) => { return ( @@ -69,83 +39,7 @@ const ListItem = React.forwardRef, React.ComponentPropsWit ); ListItem.displayName = "ListItem"; -const unAuthenticatedComponents = () => { - return ( -
- - - - - - -
- ); -}; - -const authenticatedComponents = () => { - let notifications = 100; - const displayValue = notifications >= 100 ? "..." : notifications; - return ( -
- - {" "} -
- - - {displayValue} - -
- - - - - - - - - My Account - - Settings - Support - - - - - - -
- ); -}; - export function NavigationBar() { - const { session, loading } = useSession(); - const user = session?.user; - const [sessionLoaded, setSessionLoaded] = React.useState(false); - const [searchActive, setSearchActive] = React.useState(false); - const router = useRouter(); - - React.useEffect(() => { - if (!loading) { - setSessionLoaded(true); - } - }, [loading]); - - const handleKeyDown = async (k: React.KeyboardEvent) => { - if (k.key === "Enter") { - const query = (k.target as HTMLInputElement).value.trim(); - if (query) { - router.push(`/find?query=${encodeURIComponent(query)}`); - } - } - }; - const businessComponents = [ { title: "Businesses", @@ -234,17 +128,7 @@ export function NavigationBar() { - setSearchActive(!searchActive)} className="cursor-pointer" /> - {/* search bar's input */} - + @@ -252,17 +136,7 @@ export function NavigationBar() {
- {sessionLoaded ? ( - user ? ( - authenticatedComponents() - ) : ( - unAuthenticatedComponents() - ) - ) : ( -
- -
- )} +
diff --git a/src/components/navigationBar/profileBar.tsx b/src/components/navigationBar/profileBar.tsx new file mode 100644 index 0000000..2f6943c --- /dev/null +++ b/src/components/navigationBar/profileBar.tsx @@ -0,0 +1,100 @@ +"use client"; + +import { useState, useEffect } from "react"; +import Link from "next/link"; +import { Button } from "@/components/ui/button"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuLabel, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu"; +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; +import { Skeleton } from "@/components/ui/skeleton"; +import { Bell, Heart, Wallet } from "lucide-react"; +import { LogoutButton } from "@/components/auth/logoutButton"; +import useSession from "@/lib/supabase/useSession"; + +const UnAuthenticatedComponents = () => { + return ( +
+ + + + + + +
+ ); +}; + +const AuthenticatedComponents = () => { + let notifications = 100; + const displayValue = notifications >= 100 ? "..." : notifications; + return ( +
+ +
+ + + {displayValue} + +
+ + + + + + + + + My Account + + Settings + Support + + + + + + +
+ ); +}; + +export function ProfileBar() { + const { session } = useSession(); + const user = session?.user; + const [sessionLoaded, setSessionLoaded] = useState(false); + + useEffect(() => { + if (!session) { + setSessionLoaded(true); + } + }, [session]); + + return ( + <> + {sessionLoaded ? ( + user ? ( + + ) : ( + + ) + ) : ( +
+ +
+ )} + + ); +} diff --git a/src/components/navigationBar/serchBar.tsx b/src/components/navigationBar/serchBar.tsx new file mode 100644 index 0000000..026e52b --- /dev/null +++ b/src/components/navigationBar/serchBar.tsx @@ -0,0 +1,35 @@ +"use client"; + +import * as React from "react"; +import { useRouter } from "next/navigation"; +import { Search } from "lucide-react"; +import { cn } from "@/lib/utils"; + +export function SearchBar() { + const [searchActive, setSearchActive] = React.useState(false); + const router = useRouter(); + + const handleKeyDown = async (e: React.KeyboardEvent) => { + if (e.key === "Enter") { + const query = (e.target as HTMLInputElement).value.trim(); + if (query) { + router.push(`/find?query=${encodeURIComponent(query)}`); + } + } + }; + + return ( +
+ setSearchActive(!searchActive)} className="cursor-pointer" /> + +
+ ); +}