File size: 3,186 Bytes
e586655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";
import * as React from "react";
import { Plus, Trash, Pencil } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
  Sidebar,
  SidebarContent,
  SidebarFooter,
  SidebarHeader,
  SidebarMenu,
  SidebarMenuItem,
  SidebarMenuButton,
} from "@/components/ui/sidebar";
import { useSessionContext } from "@/context/SessionContext";

export function AppSidebar() {
  const {
    sessions,
    currentSessionId,
    createNewSession,
    switchSession,
    deleteSession,
    renameSession,
  } = useSessionContext();

  const [editingSessionId, setEditingSessionId] = React.useState<string | null>(
    null
  );
  const [newSessionName, setNewSessionName] = React.useState("");

  const handleRename = (sessionId: string) => {
    if (newSessionName.trim()) {
      renameSession(sessionId, newSessionName.trim());
      setEditingSessionId(null);
      setNewSessionName("");
    }
  };

  return (
    <Sidebar>
      <SidebarHeader>
        <Button onClick={createNewSession} className="w-full">
          <Plus className="mr-2 h-4 w-4" /> New Session
        </Button>
      </SidebarHeader>
      <SidebarContent>
        <SidebarMenu>
          {sessions.map((session) => (
            <SidebarMenuItem key={session.id}>
              {editingSessionId === session.id ? (
                <div className="flex items-center p-2">
                  <Input
                    value={newSessionName}
                    onChange={(e) => setNewSessionName(e.target.value)}
                    onBlur={() => handleRename(session.id)}
                    onKeyPress={(e) =>
                      e.key === "Enter" && handleRename(session.id)
                    }
                    className="mr-2"
                  />
                  <Button size="sm" onClick={() => handleRename(session.id)}>
                    Save
                  </Button>
                </div>
              ) : (
                <SidebarMenuButton
                  onClick={() => switchSession(session.id)}
                  isActive={session.id === currentSessionId}
                >
                  <span className="flex-1 truncate">{session.name}</span>
                  <Button
                    size="icon"
                    variant="ghost"
                    onClick={(e) => {
                      e.stopPropagation();
                      setEditingSessionId(session.id);
                      setNewSessionName(session.name);
                    }}
                  >
                    <Pencil className="h-4 w-4" />
                  </Button>
                  <Button
                    size="icon"
                    variant="ghost"
                    onClick={(e) => {
                      e.stopPropagation();
                      deleteSession(session.id);
                    }}
                  >
                    <Trash className="h-4 w-4" />
                  </Button>
                </SidebarMenuButton>
              )}
            </SidebarMenuItem>
          ))}
        </SidebarMenu>
      </SidebarContent>
      <SidebarFooter></SidebarFooter>
    </Sidebar>
  );
}