Elffuss BraidHub

kikocis/continue public

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