File size: 6,285 Bytes
6a18bec
 
ee09136
 
f0949b9
6a18bec
ee09136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a18bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee09136
 
6a18bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee09136
5b25a69
 
6a18bec
5b25a69
f0949b9
 
 
 
ee09136
 
5b25a69
f0949b9
5b25a69
 
ee09136
f0949b9
 
 
 
 
 
ee09136
 
6a18bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0949b9
6a18bec
 
 
 
ee09136
 
 
 
 
 
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
// web/src/components/sidebar/SavedChatSection.tsx
import React, { useEffect, useState } from "react";
import { Card } from "../ui/card";
import { Button } from "../ui/button";
import { Separator } from "../ui/separator";
import { Bookmark, Trash2, Pencil } from "lucide-react";
import type { SavedChat } from "../../App";

export function SavedChatSection({
  isLoggedIn,
  savedChats,
  onLoadChat,
  onDeleteSavedChat,
  onRenameSavedChat,
}: {
  isLoggedIn: boolean;
  savedChats: SavedChat[];
  onLoadChat: (chat: SavedChat) => void;
  onDeleteSavedChat: (id: string) => void;
  onRenameSavedChat?: (id: string, newTitle: string) => void;
}) {
  const [editingId, setEditingId] = useState<string | null>(null);
  const [draftTitle, setDraftTitle] = useState<string>("");

  // Keep draft in sync if list changes while editing (e.g., load, delete)
  useEffect(() => {
    if (!editingId) return;
    const current = savedChats.find((c) => c.id === editingId);
    if (!current) {
      setEditingId(null);
      setDraftTitle("");
      return;
    }
    // Only update draft if it was empty (avoid overwriting user's typing)
    if (!draftTitle) setDraftTitle(current.title || "");
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [savedChats, editingId]);

  if (!isLoggedIn) return null;

  const beginRename = (chat: SavedChat) => {
    if (!onRenameSavedChat) return;
    setEditingId(chat.id);
    setDraftTitle(chat.title || "");
  };

  const cancelRename = () => {
    setEditingId(null);
    setDraftTitle("");
  };

  const saveRename = () => {
    if (!editingId || !onRenameSavedChat) return;

    const next = draftTitle.trim();
    const current = savedChats.find((c) => c.id === editingId);
    const fallback = current?.title || "";

    // If user clears the title, revert to previous (keeps it clean)
    const finalTitle = next || fallback;

    if (finalTitle && finalTitle !== current?.title) {
      onRenameSavedChat(editingId, finalTitle);
    }

    setEditingId(null);
    setDraftTitle("");
  };

  return (
    // ✅ NO scrolling here. Scrolling is owned by LeftSidebar's scroll container.
    <div className="w-full">
      {/* header */}
      <div className="px-4 py-3">
        <div className="flex items-center gap-2">
          <Bookmark className="h-4 w-4" />
          <div className="font-semibold">Saved Chat</div>
        </div>
      </div>

      <Separator />

      {/* content (normal flow; NO overflow-y-auto) */}
      <div className="px-4 py-4">
        {savedChats.length === 0 ? (
          <Card className="p-8 text-center">
            <div className="text-sm text-muted-foreground">No saved chats yet</div>
            <div className="text-xs text-muted-foreground mt-1">
              Save conversations to view them here
            </div>
          </Card>
        ) : (
          <div className="space-y-2">
            {savedChats.map((chat) => {
              const isEditing = editingId === chat.id;
              const renameEnabled = !!onRenameSavedChat;

              return (
                <Card
                  key={chat.id}
                  className="p-3 group"
                >
                  <div className="flex items-start justify-between gap-2">
                    {/* Left: title + timestamp (click loads, but disabled while editing) */}
                    <div className="text-left flex-1 min-w-0">
                      {!isEditing ? (
                        <button
                          type="button"
                          onClick={() => onLoadChat(chat)}
                          className="text-left w-full"
                        >
                          <div className="text-sm font-medium leading-5 truncate">
                            {chat.title}
                          </div>
                          <div className="text-xs text-muted-foreground mt-1">
                            {chat.timestamp.toLocaleString()}
                          </div>
                        </button>
                      ) : (
                        <div className="w-full">
                          <input
                            autoFocus
                            value={draftTitle}
                            onChange={(e) => setDraftTitle(e.target.value)}
                            onBlur={saveRename}
                            onKeyDown={(e) => {
                              if (e.key === "Enter") saveRename();
                              if (e.key === "Escape") cancelRename();
                            }}
                            className="w-full h-8 px-2 rounded-md border bg-background text-sm outline-none focus:ring-2 focus:ring-ring"
                          />
                          <div className="text-[11px] text-muted-foreground mt-1">
                            Enter to save · Esc to cancel
                          </div>
                        </div>
                      )}
                    </div>

                    {/* Right: actions (hover show) */}
                    <div className="flex items-center gap-1">
                      {/* ✏️ Rename */}
                      {renameEnabled && !isEditing && (
                        <Button
                          variant="ghost"
                          size="icon"
                          className="h-8 w-8 opacity-0 group-hover:opacity-100 transition-opacity"
                          onClick={() => beginRename(chat)}
                          title="Rename"
                        >
                          <Pencil className="h-4 w-4" />
                        </Button>
                      )}

                      {/* 🗑 Delete */}
                      <Button
                        variant="ghost"
                        size="icon"
                        className="h-8 w-8 opacity-0 group-hover:opacity-100 transition-opacity"
                        onClick={() => onDeleteSavedChat(chat.id)}
                        title="Delete"
                      >
                        <Trash2 className="h-4 w-4" />
                      </Button>
                    </div>
                  </div>
                </Card>
              );
            })}
          </div>
        )}
      </div>
    </div>
  );
}