All files / src/common/components/layout Sidebar.tsx

85.2% Statements 190/223
82.05% Branches 32/39
75% Functions 6/8
85.2% Lines 190/223

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 2881x 1x 1x 1x 1x 1x                           1x 1x             1x                       1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   36x 36x 36x 1x 1x 1x 1x 35x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x   34x 34x 34x 1x 36x 36x 36x 252x 252x 252x 252x 252x 252x   252x 252x           252x 252x 252x 1x 1x 1x 1x 1x 1x   1x     1x   252x 252x 252x 36x 36x 24x 24x 24x 24x 24x 24x 24x   24x 24x   36x   252x 36x 36x 36x 36x 72x 72x 72x 72x 72x 72x           72x 72x 72x 36x 36x 36x   36x 36x 36x 36x 36x 36x 36x   36x   1x 36x 36x 36x 36x 36x     36x 36x 36x   36x 36x 36x   36x 36x 36x 36x 36x 36x 36x 36x 36x   36x 36x 36x 36x   36x 3x 3x 3x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x     36x 36x 36x                                                         36x       36x 36x 36x 36x 36x   36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x     36x 36x 36x 36x 36x   36x   1x  
import { AuthContext } from "@common/contexts/AuthContext";
import { useEmailAlias } from "@common/hooks/useEmailAlias";
import { useUnreadCount } from "@common/hooks/useUnreadCount";
import { useLogger } from "@common/utils/logger/useLogger";
import { AnimatePresence, motion } from "framer-motion";
import {
  Bookmark,
  CalendarDays,
  Check,
  Copy,
  Inbox,
  Mail,
  Newspaper,
  Search,
  Settings,
  Tag,
  TrendingUp,
  UserCircle
} from "lucide-react";
import { useContext, useState } from "react";
import { NavLink, useLocation } from "react-router-dom";
 
interface SidebarProps {
  isOpen?: boolean;
  onToggle?: () => void;
}
 
const SidebarContent: React.FC<{
  navLinks: any[];
  bottomLinks: any[];
  showUnreadBadge: boolean;
  unreadCount: number;
  emailLoading: boolean;
  emailAlias: string | null | undefined;
  copied: boolean;
  copyToClipboard: () => void;
  user: any;
  toggleSidebar: () => void;
  isControlled: boolean;
}> = ({
  navLinks,
  bottomLinks,
  showUnreadBadge,
  unreadCount,
  emailLoading,
  emailAlias,
  copied,
  copyToClipboard,
  user,
  toggleSidebar,
  isControlled,
}) => {
    const location = useLocation();
    return (
      <>
        <div className="p-5 border-b border-slate-200/60 dark:border-neutral-800 md:border-b-0">
          <div className="flex items-center space-x-3 mb-5">
            <div className="bg-gradient-to-br from-blue-500 to-blue-600 rounded-lg p-2 shadow-sm">
              <Inbox className="text-white" size={20} />
            </div>
            <h2 className="text-xl font-bold bg-gradient-to-r from-slate-800 to-slate-600 dark:from-slate-100 dark:to-slate-300 bg-clip-text text-transparent">
              Newsletter Hub
            </h2>
          </div>
          {emailLoading ? (
            <div className="text-sm text-slate-500 dark:text-slate-400 animate-pulse py-2.5 px-3.5 bg-slate-50 dark:bg-neutral-900 rounded-lg flex items-center">
              <div className="loading-dots"><span></span><span></span><span></span></div>
              <span className="ml-2">Loading email...</span>
            </div>
          ) : emailAlias ? (
            <div className="w-full text-sm bg-gradient-to-r from-slate-50 to-blue-50/50 dark:from-neutral-900 dark:to-neutral-900/60 border border-slate-200/60 dark:border-neutral-800 rounded-lg p-2.5 flex items-center justify-between transition-all hover:shadow-sm">
              <div className="flex items-center flex-1 min-w-0">
                <Mail className="w-4 h-4 mr-2.5 text-slate-400 dark:text-slate-300 flex-shrink-0" />
                <span className="font-mono text-xs break-all text-slate-700 dark:text-slate-200">
                  {emailAlias}
                </span>
              </div>
              <button
                onClick={copyToClipboard}
                className="btn btn-ghost btn-xs ml-2 p-1.5 rounded-md text-slate-500 dark:text-slate-300 hover:text-blue-600 hover:bg-blue-50 dark:hover:bg-slate-800/60 transition-all flex-shrink-0"
                title={copied ? "Copied!" : "Copy email"}
                disabled={copied}
              >
                {copied ? <Check className="w-4 h-4 text-green-500" /> : <Copy className="w-4 h-4" />}
              </button>
            </div>
          ) : null}
        </div>
        <nav className="flex-1 p-4 space-y-1.5 overflow-y-auto text-slate-700 dark:text-slate-200">
          {navLinks.map((link) => (
            <NavLink
              key={link.to}
              to={link.to}
              data-testid={`sidebar-link-${link.to.replace('/', '')}`}
              end={link.end}
              className={({ isActive }) => {
                // Enhanced logic: highlight Inbox if on a newsletter detail page and navigation state says we came from inbox
                const isNewsletterDetailPage =
                  window.location.pathname.startsWith("/newsletters/") &&
                  link.to === "/inbox" &&
                  (
                    (location.state && (location.state.from === "/inbox" || location.state.fromInbox)) ||
                    (!location.state && window.location.pathname.match(/^\/newsletters\//))
                  );
                return `sidebar-link ${isActive || isNewsletterDetailPage ? "active" : ""}`;
              }}
              onClick={(e) => {
                if (link.to === "/inbox" && window.location.pathname === "/inbox") {
                  e.preventDefault();
                  window.history.replaceState({}, "", "/inbox");
                  window.dispatchEvent(new Event("inbox:clear-filters"));
                  window.dispatchEvent(new Event("inbox:refresh-newsletters"));
                }
                // On mobile, clicking a link should close the sidebar
                if (window.innerWidth < 1024 && isControlled) {
                  toggleSidebar();
                }
              }}
            >
              {link.icon}
              <span className="flex-1">{link.label}</span>
              {link.to === "/inbox" && (
                <AnimatePresence>
                  {showUnreadBadge && (
                    <motion.span
                      initial={{ opacity: 0, scale: 0.5 }}
                      animate={{ opacity: 1, scale: 1 }}
                      exit={{ opacity: 0, scale: 0.5 }}
                      className="ml-auto text-xs font-semibold px-2 py-0.5 rounded-full bg-blue-100 text-blue-700 border border-blue-200/70 shadow-sm dark:bg-blue-900/40 dark:text-blue-200 dark:border-blue-800/50"
                      aria-live="polite"
                      aria-atomic="true"
                    >
                      {unreadCount}
                    </motion.span>
                  )}
                </AnimatePresence>
              )}
            </NavLink>
          ))}
        </nav>
        <div className="p-4 border-t border-slate-200/60 dark:border-neutral-800">
          {bottomLinks.map((link) => (
            <NavLink
              key={link.to}
              to={link.to}
              data-testid={`sidebar-link-${link.to.replace('/', '')}`}
              className={({ isActive }) => `sidebar-link ${isActive ? "active" : ""}`}
              onClick={() => {
                if (window.innerWidth < 1024 && isControlled) {
                  toggleSidebar();
                }
              }}
            >
              {link.icon}
              <span className="flex-1">{link.label}</span>
            </NavLink>
          ))}
          <div className="mt-3 pt-3 border-t border-slate-200/60 dark:border-neutral-800">
            <div className="text-xs text-slate-500 dark:text-slate-400 mb-1">
              Signed in as
            </div>
            <div className="font-medium text-slate-700 dark:text-slate-200 truncate text-sm">
              {user?.email}
            </div>
          </div>
        </div>
      </>
    );
  };
 
const Sidebar: React.FC<SidebarProps> = ({ isOpen, onToggle }) => {
  const log = useLogger("Sidebar");
  const [internalIsOpen, setInternalIsOpen] = useState(false);
  const auth = useContext(AuthContext);
  const user = auth?.user;
  const { emailAlias, loading: emailLoading } = useEmailAlias();
 
  // Use external state if provided (mobile), otherwise use internal state (desktop)
  const isControlled = isOpen !== undefined && onToggle !== undefined;
  const sidebarIsOpen = isControlled ? isOpen : internalIsOpen;
  const toggleSidebar = isControlled ? onToggle : () => setInternalIsOpen(!internalIsOpen);
 
  const [copied, setCopied] = useState(false);
  const { unreadCount, isLoading } = useUnreadCount();
  const showUnreadBadge = !isLoading && unreadCount !== undefined && unreadCount > 0;
 
  const navLinks = [
    { to: "/inbox", icon: <Inbox size={20} />, label: "Inbox" },
    { to: "/queue", icon: <Bookmark size={20} />, label: "Reading Queue" },
    { to: "/daily", icon: <CalendarDays size={20} />, label: "Daily Summary" },
    { to: "/search", icon: <Search size={20} />, label: "Search" },
    { to: "/trending", icon: <TrendingUp size={20} />, label: "Trending Topics" },
    { to: "/tags", icon: <Tag size={20} />, label: "Tags" },
    { to: "/newsletters", icon: <Newspaper size={20} />, label: "Newsletter Groups", end: true },
  ];
 
  const bottomLinks = [
    { to: "/profile", icon: <UserCircle size={20} />, label: "Profile" },
    { to: "/settings", icon: <Settings size={20} />, label: "Settings" },
  ];
 
  const copyToClipboard = async () => {
    if (!emailAlias) return;
    try {
      await navigator.clipboard.writeText(emailAlias);
      setCopied(true);
      setTimeout(() => setCopied(false), 2000);
    } catch (err) {
      log.error(
        "Failed to copy email",
        {
          action: "copy_email",
          metadata: { emailAlias },
        },
        err instanceof Error ? err : new Error(String(err)),
      );
      alert("Failed to copy email to clipboard");
    }
  };
 
  // Mobile sidebar (animated, only rendered if open)
  const mobileSidebar = (
    <AnimatePresence>
      {sidebarIsOpen && (
        <motion.aside
          data-testid="sidebar"
          initial={{ x: '-100%' }}
          animate={{ x: 0 }}
          exit={{ x: '-100%' }}
          transition={{ duration: 0.3, ease: "easeInOut" }}
          className="fixed inset-y-0 left-0 z-50 w-[85vw] max-w-xs bg-white/95 dark:bg-neutral-900/95 backdrop-blur-md border-r border-slate-200/60 dark:border-neutral-800 flex flex-col shadow-xl lg:hidden"
          aria-label="Main navigation"
        >
          <div className="h-14 flex items-center px-4 border-b border-slate-200/60 flex-shrink-0">
            {/* Hamburger menu button (if needed) */}
            {/* You may need to add the actual hamburger button here if not present elsewhere */}
          </div>
          <SidebarContent
            navLinks={navLinks}
            bottomLinks={bottomLinks}
            showUnreadBadge={showUnreadBadge}
            unreadCount={unreadCount}
            emailLoading={emailLoading}
            emailAlias={emailAlias}
            copied={copied}
            copyToClipboard={copyToClipboard}
            user={user}
            toggleSidebar={toggleSidebar}
            isControlled={isControlled}
          />
        </motion.aside>
      )}
    </AnimatePresence>
  );
 
  // Desktop sidebar (always visible, no animation)
  const desktopSidebar = (
    <aside
      data-testid="sidebar"
      className="static inset-y-0 left-0 z-40 w-72 bg-white/95 dark:bg-neutral-900/95 backdrop-blur-md border-r border-slate-200/60 dark:border-neutral-800 lg:border-r-0 flex flex-col shadow-none hidden lg:flex"
      aria-label="Main navigation"
    >
      <SidebarContent
        navLinks={navLinks}
        bottomLinks={bottomLinks}
        showUnreadBadge={showUnreadBadge}
        unreadCount={unreadCount}
        emailLoading={emailLoading}
        emailAlias={emailAlias}
        copied={copied}
        copyToClipboard={copyToClipboard}
        user={user}
        toggleSidebar={toggleSidebar}
        isControlled={isControlled}
      />
    </aside>
  );
 
  return (
    <>
      {mobileSidebar}
      {desktopSidebar}
    </>
  );
};
 
export default Sidebar;