| 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-primary text-primary-content shadow-sm px-4 sm:px-6">
|
|---|
| 30 | <div className="navbar-start flex-1 min-w-0">
|
|---|
| 31 | <Link
|
|---|
| 32 | className="btn btn-ghost text-primary-content"
|
|---|
| 33 | aria-label="Trekr home"
|
|---|
| 34 | to="/"
|
|---|
| 35 | >
|
|---|
| 36 | <img src={logo} alt="Trekr" className="h-12 w-auto rounded-2xl" />
|
|---|
| 37 | </Link>
|
|---|
| 38 | </div>
|
|---|
| 39 | <div className="navbar-end flex-none">
|
|---|
| 40 | {isAuthed ? (
|
|---|
| 41 | <div className="flex flex-wrap items-center justify-end gap-2 sm:gap-3">
|
|---|
| 42 | <Link
|
|---|
| 43 | className="btn btn-sm sm:btn-md btn-outline border-primary-content text-primary-content"
|
|---|
| 44 | to="/dashboard"
|
|---|
| 45 | >
|
|---|
| 46 | Dashboard
|
|---|
| 47 | </Link>
|
|---|
| 48 | <button
|
|---|
| 49 | className="btn btn-sm sm:btn-md btn-outline border-primary-content text-primary-content"
|
|---|
| 50 | onClick={onLogout}
|
|---|
| 51 | >
|
|---|
| 52 | Logout
|
|---|
| 53 | </button>
|
|---|
| 54 | </div>
|
|---|
| 55 | ) : (
|
|---|
| 56 | <Link
|
|---|
| 57 | className="btn btn-sm sm:btn-md btn-tertiary text-blue-200! px-6 sm:px-8"
|
|---|
| 58 | to="/login"
|
|---|
| 59 | >
|
|---|
| 60 | Start Tracking
|
|---|
| 61 | </Link>
|
|---|
| 62 | )}
|
|---|
| 63 | </div>
|
|---|
| 64 | </div>
|
|---|
| 65 | );
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | export default NavbarLanding;
|
|---|