Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

1.9 KB
Raw Blame
"use client";

import { createContext, useCallback, useContext, useEffect, useState } from "react";
import { APP_CONSTANTS } from "@/lib/constants";

type SidebarContextValue = {
  expanded: boolean;
  toggle: () => void;
  collapse: () => void;
};

const SidebarContext = createContext<SidebarContextValue | null>(null);

// One year, so the preference persists like a typical "remember my setting" cookie.
const COOKIE_MAX_AGE_SECONDS = 60 * 60 * 24 * 365;

export function SidebarProvider({
  children,
  initialExpanded,
}: {
  children: React.ReactNode;
  initialExpanded: boolean;
}) {
  const [expanded, setExpanded] = useState(initialExpanded);

  const toggle = useCallback(() => {
    setExpanded((prev) => !prev);
  }, []);

  const collapse = useCallback(() => {
    setExpanded(false);
  }, []);

  useEffect(() => {
    document.cookie = `${APP_CONSTANTS.SIDEBAR_STORAGE_KEY}=${expanded}; path=/; max-age=${COOKIE_MAX_AGE_SECONDS}; samesite=lax`;
  }, [expanded]);

  useEffect(() => {
    const onKeyDown = (e: KeyboardEvent) => {
      if (e.key?.toLowerCase() !== "b" || (!e.metaKey && !e.ctrlKey)) return;

      // Let editable elements (inputs, Tiptap/ProseMirror) keep Cmd/Ctrl+B
      // for their own shortcuts (e.g. bold) instead of toggling the sidebar.
      const target = e.target as HTMLElement | null;
      if (target?.closest('input, textarea, [contenteditable="true"]')) return;

      e.preventDefault();
      toggle();
    };
    window.addEventListener("keydown", onKeyDown);
    return () => window.removeEventListener("keydown", onKeyDown);
  }, [toggle]);

  return (
    <SidebarContext.Provider value={{ expanded, toggle, collapse }}>
      {children}
    </SidebarContext.Provider>
  );
}

export function useSidebar() {
  const ctx = useContext(SidebarContext);
  if (!ctx) {
    throw new Error("useSidebar must be used within a SidebarProvider");
  }
  return ctx;
}