-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathnavbar.tsx
66 lines (60 loc) · 2.28 KB
/
navbar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useEffect, useState, useContext } from 'react';
import { signInWithRedirect, GoogleAuthProvider, signOut, User } from 'firebase/auth';
import { AuthContext } from '@/lib/firebase';
import { Link } from 'react-router-dom';
import { handleAuthStateChange } from '@/lib/MovieService';
import { FaSearch } from 'react-icons/fa';
import firebaseLogo from '@/assets/firebase_logo.svg';
export default function Navbar() {
const [user, setUser] = useState<User | null>(null);
const auth = useContext(AuthContext);
useEffect(() => {
const unsubscribe = handleAuthStateChange(auth, setUser);
return () => unsubscribe();
}, [auth]);
async function handleSignIn() {
const provider = new GoogleAuthProvider();
await signInWithRedirect(auth, provider);
}
async function handleSignOut() {
await signOut(auth);
}
return (
<nav className="bg-black p-4">
<div className="container mx-auto flex justify-between items-center">
<div className="flex items-center space-x-4">
<Link to="/" className="flex items-center">
<img src={firebaseLogo} alt="Firebase Logo" width={30} height={30} className="mr-2" />
<span className=" text-white text-lg font-bold hidden md:block">FriendlyMovies</span>
</Link>
<Link to="/vectorsearch" className="text-gray-200 hover:text-white">
Vector Search
</Link>
</div>
<Link to="/advancedsearch" className="flex items-center text-gray-200 hover:text-white mx-auto">
<FaSearch className="mr-2" />
Advanced Search
</Link>
<div className="flex items-center space-x-4">
{user && (
<Link to="/myprofile" className="text-yellow-500 hover:text-yellow-400">
My Profile
</Link>
)}
{user ? (
<>
<span className="text-gray-200 mr-4">Hello, {user.displayName}</span>
<button onClick={handleSignOut} className="text-gray-200 hover:text-white">
Sign Out
</button>
</>
) : (
<button onClick={handleSignIn} className="text-gray-200 hover:text-white">
Sign In with Google
</button>
)}
</div>
</div>
</nav>
);
}