Elffuss BraidHub

kikocis/continue public

60acd6c5ca13 · 3.8 KiBRawPermalink
1import { format, isToday, isYesterday, isThisWeek, isThisYear } from "date-fns";
2import { Box, Text, useInput } from "ink";
3import React, { useMemo, useState } from "react";
4
5import { SessionMetadata } from "../session.js";
6
7import { useTerminalSize } from "./hooks/useTerminalSize.js";
8
9interface SessionSelectorProps {
10 sessions: SessionMetadata[];
11 onSelect: (sessionId: string) => void;
12 onExit: () => void;
13}
14
15function formatTimestamp(date: Date): string {
16 if (isToday(date)) {
17 return format(date, "h:mm a");
18 } else if (isYesterday(date)) {
19 return "yesterday";
20 } else if (isThisWeek(date)) {
21 return format(date, "EEEE"); // Day name (e.g., "Monday")
22 } else if (isThisYear(date)) {
23 return format(date, "MMM d"); // e.g., "Jan 15"
24 } else {
25 return format(date, "MMM d, yyyy"); // e.g., "Jan 15, 2023"
26 }
27}
28
29function formatMessage(message: string | undefined): string {
30 if (!message) return "(no messages)";
31 // Ensure we're working with a string, handle edge cases from persisted data
32 const messageStr = typeof message === "string" ? message : String(message);
33 return messageStr.split("\n")[0];
34}
35
36export function SessionSelector({
37 sessions,
38 onSelect,
39 onExit,
40}: SessionSelectorProps) {
41 const [selectedIndex, setSelectedIndex] = useState(0);
42 const { rows: terminalHeight } = useTerminalSize();
43
44 // Calculate how many sessions we can display based on terminal height
45 const displaySessions = useMemo(() => {
46 // Reserve 5 lines for header and instructions, each session takes 3 lines (2 content + 1 spacer)
47 const availableHeight = Math.max(1, terminalHeight - 5);
48 const maxDisplayableSessions = Math.floor(availableHeight / 3);
49
50 return sessions.slice(0, maxDisplayableSessions);
51 }, [sessions, terminalHeight]);
52
53 useInput((input, key) => {
54 if (key.upArrow || input === "k") {
55 setSelectedIndex((prev) =>
56 prev > 0 ? prev - 1 : displaySessions.length - 1,
57 );
58 } else if (key.downArrow || input === "j") {
59 setSelectedIndex((prev) =>
60 prev < displaySessions.length - 1 ? prev + 1 : 0,
61 );
62 } else if (key.return) {
63 if (displaySessions[selectedIndex]) {
64 onSelect(displaySessions[selectedIndex].id);
65 }
66 } else if (key.escape || (key.ctrl && input === "d")) {
67 onExit();
68 }
69 });
70
71 if (sessions.length === 0) {
72 return (
73 <Box flexDirection="column" padding={1}>
74 <Text color="yellow">No previous sessions found.</Text>
75 <Text color="gray">Start a new conversation with: cn</Text>
76 <Text color="gray">Press ESC or Ctrl+D to exit</Text>
77 </Box>
78 );
79 }
80
81 return (
82 <Box flexDirection="column" padding={1}>
83 <Text color="blue" bold>
84 Recent Sessions
85 </Text>
86 <Text color="gray">
87 Use ↑↓ arrows to navigate, Enter to select, ESC or Ctrl+D to exit
88 </Text>
89 <Text> </Text>
90
91 {displaySessions.map((session, index) => {
92 const isSelected = index === selectedIndex;
93 const indicator = isSelected ? "► " : " ";
94 const color = isSelected ? "cyan" : "white";
95
96 return (
97 <Box key={session.id} flexDirection="column">
98 <Box paddingRight={3}>
99 <Text color={color} wrap="truncate-end">
100 {indicator}
101 {formatMessage(session.firstUserMessage)}
102 </Text>
103 </Box>
104 <Box marginLeft={2}>
105 <Text color="gray">
106 {formatTimestamp(session.timestamp)} - {session.messageCount}{" "}
107 {session.messageCount === 1 ? "message" : "messages"}
108 </Text>
109 </Box>
110 {index < displaySessions.length - 1 && (
111 <Text key={`spacer-${session.id}`}> </Text>
112 )}
113 </Box>
114 );
115 })}
116 </Box>
117 );
118}