All files / src/components/session SessionSidebar.tsx

0% Statements 0/150
0% Branches 0/1
0% Functions 0/1
0% Lines 0/150

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                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { useEffect, useRef, useState } from "react";
import type { SessionRecord } from "../../types";
import { EmptyState } from "../shared/EmptyState";
 
function TrashIcon() {
  return (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <polyline points="3 6 5 6 21 6" />
      <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
      <line x1="10" y1="11" x2="10" y2="17" />
      <line x1="14" y1="11" x2="14" y2="17" />
    </svg>
  );
}
 
function MoreIcon() {
  return (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor">
      <circle cx="5" cy="12" r="2" />
      <circle cx="12" cy="12" r="2" />
      <circle cx="19" cy="12" r="2" />
    </svg>
  );
}
 
function formatDate(dateStr: string): string {
  const date = new Date(dateStr);
  const now = new Date();
  const diffMs = now.getTime() - date.getTime();
  const diffMin = Math.floor(diffMs / 60000);
  const diffHr = Math.floor(diffMs / 3600000);
  const diffDay = Math.floor(diffMs / 86400000);
 
  if (diffMin < 1) return "Just now";
  if (diffMin < 60) return `${diffMin}m ago`;
  if (diffHr < 24) return `${diffHr}h ago`;
  if (diffDay < 7) return `${diffDay}d ago`;
  return date.toLocaleDateString(undefined, { month: "short", day: "numeric" });
}
 
export function SessionSidebar({
  sessions,
  isLoading,
  activeSessionId,
  workspaceRoot,
  onNewSession,
  onResumeSession,
  onDeleteSession,
  onToggle,
  isOpen,
}: {
  sessions: SessionRecord[];
  isLoading: boolean;
  activeSessionId: string | null;
  workspaceRoot: string | undefined;
  onNewSession: () => void;
  onResumeSession: (sessionId: string) => void;
  onDeleteSession: (session: SessionRecord) => void;
  onToggle: () => void;
  isOpen: boolean;
}) {
  const [openMenuSessionId, setOpenMenuSessionId] = useState<string | null>(null);
  const containerRef = useRef<HTMLDivElement>(null);
 
  useEffect(() => {
    const handlePointerDown = (event: MouseEvent) => {
      if (
        containerRef.current &&
        event.target instanceof Node &&
        !containerRef.current.contains(event.target)
      ) {
        setOpenMenuSessionId(null);
      }
    };
    const handleKeyDown = (event: KeyboardEvent) => {
      if (event.key === "Escape") {
        setOpenMenuSessionId(null);
      }
    };
    document.addEventListener("mousedown", handlePointerDown);
    document.addEventListener("keydown", handleKeyDown);
    return () => {
      document.removeEventListener("mousedown", handlePointerDown);
      document.removeEventListener("keydown", handleKeyDown);
    };
  }, []);
 
  if (!isOpen) {
    return (
      <div className="sidebar__collapsed">
        <button
          type="button"
          className="sidebar__toggle"
          onClick={onToggle}
          title="Show sessions"
        >
          &#9654;
        </button>
        <button
          type="button"
          className="btn btn--primary btn--icon"
          onClick={onNewSession}
          title="New session"
        >
          +
        </button>
      </div>
    );
  }
 
  return (
    <>
      <div className="sidebar__header">
        <span className="sidebar__title">Session History</span>
        <div className="sidebar__header-actions">
          <button type="button" className="btn btn--primary btn--sm" onClick={onNewSession}>
            + New
          </button>
          <button
            type="button"
            className="sidebar__toggle"
            onClick={onToggle}
            title="Hide sessions"
          >
            &#9664;
          </button>
        </div>
      </div>
 
      {workspaceRoot ? (
        <div className="sidebar__workspace">
          <span className="sidebar__workspace-path" title={workspaceRoot}>
            {workspaceRoot}
          </span>
        </div>
      ) : null}
 
      <div className="sidebar__list" ref={containerRef}>
        {isLoading ? (
          <>
            <div className="skeleton skeleton--card" />
            <div className="skeleton skeleton--card" />
            <div className="skeleton skeleton--card" />
          </>
        ) : sessions.length === 0 ? (
          <EmptyState
            title="No sessions"
            description="Start a new session to begin"
          />
        ) : (
          sessions.map((session) => {
            const menuOpen = openMenuSessionId === session.session_id;
            return (
              <div
                key={session.session_id}
                className={`session-card ${activeSessionId === session.session_id ? "session-card--active" : ""}${menuOpen ? " session-card--menu-open" : ""}`}
              >
                <button
                  type="button"
                  className="session-card__main"
                  onClick={() => {
                    setOpenMenuSessionId(null);
                    onResumeSession(session.session_id);
                  }}
                >
                  <span className="session-card__title">
                    {session.title || "Untitled session"}
                  </span>
                  <div className="session-card__meta">
                    <time className="session-card__time">{formatDate(session.updated_at)}</time>
                    <span className="session-card__model">{session.model}</span>
                  </div>
                </button>
 
                <button
                  type="button"
                  className="session-card__menu-trigger"
                  aria-label={`Open actions for ${session.title || "Untitled session"}`}
                  aria-expanded={menuOpen}
                  onClick={(event) => {
                    event.stopPropagation();
                    setOpenMenuSessionId((current) =>
                      current === session.session_id ? null : session.session_id,
                    );
                  }}
                >
                  <MoreIcon />
                </button>
 
                {menuOpen ? (
                  <div className="session-card__menu" role="menu">
                    <button
                      type="button"
                      className="session-card__menu-item session-card__menu-item--danger"
                      role="menuitem"
                      onClick={() => {
                        setOpenMenuSessionId(null);
                        onDeleteSession(session);
                      }}
                    >
                      <TrashIcon />
                      Delete session
                    </button>
                  </div>
                ) : null}
              </div>
            );
          })
        )}
      </div>
    </>
  );
}