All files / components/workspace sidebar.tsx

0% Statements 0/34
0% Branches 0/20
0% Functions 0/13
0% Lines 0/34

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
"use client";
 
import type { ReactNode } from "react";
import { useState } from "react";
import { useTheme } from "next-themes";
 
import { Badge } from "@/components/ui/badge";
import { Card } from "@/components/ui/card";
import { Text } from "@/components/ui/text";
import { Button } from "@/components/ui/button";
import { SIDEBAR_COPY } from "@/constants/app";
import {
  THREAD_TIMESTAMP_FORMAT,
  THREAD_TIMESTAMP_LOCALE,
} from "@/constants/date-time";
import { cn } from "@/utils/class-name";
import type { ChatThread } from "@/types/thread";
import { Theme } from "@/constants/theme";
import { ArrowPathIcon } from "@/components/icons/ArrowPath";
import { MoonIcon } from "@/components/icons/Moon";
import { SunIcon } from "@/components/icons/Sun";
 
function formatTimestamp(value: string) {
  if (!value) {
    return "";
  }
 
  try {
    return new Intl.DateTimeFormat(
      THREAD_TIMESTAMP_LOCALE,
      THREAD_TIMESTAMP_FORMAT,
    ).format(new Date(value));
  } catch {
    return value;
  }
}
 
type ThreadSidebarProps = {
  activeThread: ChatThread;
  allThreads: ChatThread[];
  disabled?: boolean;
  accountPanel: ReactNode;
  providerPanel: ReactNode;
  onSwitchThread: (id: string) => void;
  onCreateThread: () => void;
  onDeleteThread: (id: string) => void;
  onResetChat: () => void;
};
 
export function ThreadSidebar({
  activeThread,
  allThreads,
  disabled = false,
  accountPanel,
  providerPanel,
  onSwitchThread,
  onCreateThread,
  onDeleteThread,
  onResetChat,
}: ThreadSidebarProps) {
  const [confirmReset, setConfirmReset] = useState(false);
  const { resolvedTheme, setTheme } = useTheme();
 
  function handleChangeTheme() {
    setTheme(resolvedTheme === Theme.DARK ? Theme.LIGHT : Theme.DARK);
  }
 
  function handleResetClick() {
    if (confirmReset) {
      onResetChat();
      setConfirmReset(false);
    } else {
      setConfirmReset(true);
    }
  }
 
  return (
    <aside className="flex w-full flex-col rounded-[1.75rem] border border-white/9 bg-panel text-white backdrop-blur-[28px] shadow-[0_16px_60px_rgba(7,12,32,0.32)] lg:max-w-sm">
      <div className="border-b border-white/8 p-5">
        <div className="mb-3 flex justify-start gap-2">
          {confirmReset ? (
            <div className="flex items-center gap-1.5">
              <button
                type="button"
                aria-label="Cancel reset"
                onClick={() => setConfirmReset(false)}
                className="h-8 px-2.5 rounded-lg text-xs font-medium transition cursor-pointer bg-white/8 text-white/50 hover:bg-white/14 hover:text-white/90 light:bg-slate-200 light:text-slate-600 light:hover:bg-slate-300 light:hover:text-slate-900"
              >
                Cancel
              </button>
              <button
                type="button"
                aria-label="Confirm reset chat"
                onClick={handleResetClick}
                className="h-8 px-2.5 rounded-lg text-xs font-medium transition cursor-pointer bg-white/8 text-rose-300/80 hover:bg-rose-500/15 hover:text-rose-300 light:bg-slate-200 light:text-rose-500 light:hover:bg-rose-100 light:hover:text-rose-600"
              >
                Confirm reset
              </button>
            </div>
          ) : (
          <button
            type="button"
            aria-label="Reset chat"
            onClick={handleResetClick}
            disabled={disabled}
            className="grid h-8 w-8 aspect-square shrink-0 place-items-center rounded-lg transition cursor-pointer disabled:cursor-not-allowed disabled:opacity-50 bg-white/8 text-white/50 hover:bg-white/14 hover:text-white/90 light:bg-slate-200 light:text-slate-600 light:hover:bg-slate-300 light:hover:text-slate-900"
          >
            <ArrowPathIcon className="h-4 w-4" />
          </button>
          )}
          <button
            type="button"
            aria-label="Toggle theme"
            onClick={handleChangeTheme}
            className="grid h-8 w-8 aspect-square shrink-0 place-items-center rounded-lg transition cursor-pointer bg-white/8 text-white/50 hover:bg-white/14 hover:text-white/90 light:bg-slate-200 light:text-slate-600 light:hover:bg-slate-300 light:hover:text-slate-900"
          >
            {resolvedTheme === Theme.DARK ? (
              <MoonIcon className="h-4 w-4" />
            ) : (
              <SunIcon className="h-4 w-4" />
            )}
          </button>
        </div>
        <div className="space-y-1">
          <Text as="p" variant="eyebrow">
            {SIDEBAR_COPY.eyebrow}
          </Text>
          <Text as="h1" variant="title">
            {SIDEBAR_COPY.title}
          </Text>
          <Text variant="captionStrong">
            {SIDEBAR_COPY.description}
          </Text>
        </div>
      </div>
 
      <div className="border-b border-white/8 p-5">{accountPanel}</div>
 
      <div className="border-b border-white/8 p-5">{providerPanel}</div>
 
      <div className="min-h-0 flex-1 overflow-y-auto p-3">
        <div className="mb-4 px-2">
          <Button
            variant="primary"
            size="sm"
            className="w-full justify-center gap-2"
            onClick={onCreateThread}
            disabled={disabled || activeThread.messages.length === 0}
          >
            <svg
              viewBox="0 0 16 16"
              fill="none"
              stroke="currentColor"
              strokeWidth="2"
              className="h-3.5 w-3.5"
            >
              <path d="M8 3v10M3 8h10" />
            </svg>
            {SIDEBAR_COPY.newChatLabel}
          </Button>
        </div>
 
        <div className="space-y-4">
          <div>
            <div className="mb-2 px-2">
              <Text as="p" variant="eyebrowMuted">
                {SIDEBAR_COPY.currentChatLabel}
              </Text>
            </div>
            <ThreadCard
              thread={activeThread}
              isActive
              disabled={disabled}
              onSelect={() => {}}
              onDelete={() => onDeleteThread(activeThread.id)}
            />
          </div>
 
          {allThreads.length > 1 && (
            <div>
              <div className="mb-2 px-2">
                <Text as="p" variant="eyebrowMuted">
                  {SIDEBAR_COPY.recentChatsLabel}
                </Text>
              </div>
              <div className="space-y-2">
                {allThreads
                  .filter((t) => t.id !== activeThread.id)
                  .map((thread) => (
                    <ThreadCard
                      key={thread.id}
                      thread={thread}
                      disabled={disabled}
                      onSelect={() => onSwitchThread(thread.id)}
                      onDelete={() => onDeleteThread(thread.id)}
                    />
                  ))}
              </div>
            </div>
          )}
        </div>
      </div>
    </aside>
  );
}
 
function ThreadCard({
  thread,
  isActive = false,
  disabled = false,
  onSelect,
  onDelete,
}: {
  thread: ChatThread;
  isActive?: boolean;
  disabled?: boolean;
  onSelect: () => void;
  onDelete: () => void;
}) {
  return (
    <Card
      variant={isActive ? "soft" : "panel"}
      className={cn(
        "group cursor-pointer border-white/10 transition hover:border-white/16 hover:bg-white/9",
        isActive ? "bg-white/10 shadow-[0_8px_26px_rgba(8,12,30,0.22)]" : "bg-white/4",
      )}
      onClick={onSelect}
    >
      <div className="px-4 py-3">
        <div className="flex items-start justify-between gap-3">
          <div className="min-w-0">
            <Text as="p" variant="bodyStrong" className="truncate">
              {thread.title}
            </Text>
            <Text variant="caption" className="mt-1 line-clamp-1">
              {thread.preview}
            </Text>
          </div>
          <Badge
            variant={isActive ? "brand" : "subtle"}
            size="sm"
            className="uppercase tracking-wide"
          >
            {thread.provider}
          </Badge>
        </div>
        <div className="mt-2 flex items-center justify-between gap-2">
          <Text variant="helper" suppressHydrationWarning>
            {formatTimestamp(thread.updatedAt)}
          </Text>
          <button
            type="button"
            className="font-dm-sans text-[10px] uppercase tracking-wider text-white/30 transition hover:text-rose-300 disabled:cursor-not-allowed disabled:opacity-50"
            onClick={(e) => {
              e.stopPropagation();
              onDelete();
            }}
            disabled={disabled}
          >
            {SIDEBAR_COPY.deleteChatLabel}
          </button>
        </div>
      </div>
    </Card>
  );
}