| 1 | import React, { useEffect, useState } from "react";
|
|---|
| 2 | import { Link, useNavigate } from "react-router-dom";
|
|---|
| 3 | import logo from "../../../assets/logo.png";
|
|---|
| 4 |
|
|---|
| 5 | const NavbarLanding = () => {
|
|---|
| 6 | const navigate = useNavigate();
|
|---|
| 7 | const [isAuthed, setIsAuthed] = useState(() =>
|
|---|
| 8 | Boolean(localStorage.getItem("authToken")),
|
|---|
| 9 | );
|
|---|
| 10 |
|
|---|
| 11 | useEffect(() => {
|
|---|
| 12 | const onStorage = (e) => {
|
|---|
| 13 | if (e.key === "authToken") {
|
|---|
| 14 | setIsAuthed(Boolean(e.newValue));
|
|---|
| 15 | }
|
|---|
| 16 | };
|
|---|
| 17 | window.addEventListener("storage", onStorage);
|
|---|
| 18 | return () => window.removeEventListener("storage", onStorage);
|
|---|
| 19 | }, []);
|
|---|
| 20 |
|
|---|
| 21 | const onLogout = () => {
|
|---|
| 22 | localStorage.removeItem("authToken");
|
|---|
| 23 | localStorage.removeItem("authUser");
|
|---|
| 24 | setIsAuthed(false);
|
|---|
| 25 | navigate("/", { replace: true });
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | return (
|
|---|
| 29 | <div className="navbar bg-neutral shadow-sm">
|
|---|
| 30 | <div className="navbar-start">
|
|---|
| 31 | <Link className="btn btn-ghost" aria-label="Trekr home" to="/">
|
|---|
| 32 | <img src={logo} alt="Trekr" className="h-12 w-auto rounded-2xl" />
|
|---|
| 33 | </Link>
|
|---|
| 34 | </div>
|
|---|
| 35 | <div className="navbar-end">
|
|---|
| 36 | {isAuthed ? (
|
|---|
| 37 | <div className="flex items-center gap-2 mr-6">
|
|---|
| 38 | <Link className="btn btn-primary" to="/dashboard">
|
|---|
| 39 | Dashboard
|
|---|
| 40 | </Link>
|
|---|
| 41 | <button className="btn btn-outline" onClick={onLogout}>
|
|---|
| 42 | Logout
|
|---|
| 43 | </button>
|
|---|
| 44 | </div>
|
|---|
| 45 | ) : (
|
|---|
| 46 | <Link
|
|---|
| 47 | className="btn bg-black text-green-200 border-black hover:bg-black/90 hover:border-black px-8 mr-6"
|
|---|
| 48 | to="/login"
|
|---|
| 49 | >
|
|---|
| 50 | Start Tracking
|
|---|
| 51 | </Link>
|
|---|
| 52 | )}
|
|---|
| 53 | </div>
|
|---|
| 54 | </div>
|
|---|
| 55 | );
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 | export default NavbarLanding;
|
|---|