Spaces:
Running
Running
File size: 10,956 Bytes
e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 b1d24de e77f678 b1d24de e77f678 b1d24de a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 aaf7d3b a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 e77f678 a56db97 aaf7d3b a56db97 e77f678 a56db97 e77f678 b1d24de e77f678 a56db97 e77f678 29d492e b1d24de e77f678 2826758 e77f678 b1d24de e77f678 a56db97 e77f678 29d492e e77f678 2826758 e77f678 29d492e e77f678 29d492e a56db97 e77f678 29d492e a56db97 e77f678 29d492e a56db97 |
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
import { useCallback, useState } from 'react';
import {
Alert,
Box,
IconButton,
Typography,
CircularProgress,
Divider,
} from '@mui/material';
import AddIcon from '@mui/icons-material/Add';
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
import ChatBubbleOutlineIcon from '@mui/icons-material/ChatBubbleOutline';
import { useSessionStore } from '@/store/sessionStore';
import { useAgentStore } from '@/store/agentStore';
import { apiFetch } from '@/utils/api';
interface SessionSidebarProps {
onClose?: () => void;
}
/** Small coloured dot for connection status */
const StatusDot = ({ connected }: { connected: boolean }) => (
<Box
sx={{
width: 6,
height: 6,
borderRadius: '50%',
bgcolor: connected ? 'var(--accent-green)' : 'var(--accent-red)',
boxShadow: connected ? '0 0 4px rgba(76,175,80,0.4)' : 'none',
flexShrink: 0,
}}
/>
);
export default function SessionSidebar({ onClose }: SessionSidebarProps) {
const { sessions, activeSessionId, createSession, deleteSession, switchSession } =
useSessionStore();
const { isConnected, setPlan, setPanelContent } =
useAgentStore();
const [isCreatingSession, setIsCreatingSession] = useState(false);
const [capacityError, setCapacityError] = useState<string | null>(null);
// ββ Handlers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const handleNewSession = useCallback(async () => {
if (isCreatingSession) return;
setIsCreatingSession(true);
setCapacityError(null);
try {
const response = await apiFetch('/api/session', { method: 'POST' });
if (response.status === 503) {
const data = await response.json();
setCapacityError(data.detail || 'Server is at capacity.');
return;
}
const data = await response.json();
createSession(data.session_id);
setPlan([]);
setPanelContent(null);
onClose?.();
} catch {
setCapacityError('Failed to create session.');
} finally {
setIsCreatingSession(false);
}
}, [isCreatingSession, createSession, setPlan, setPanelContent, onClose]);
const handleDelete = useCallback(
async (sessionId: string, e: React.MouseEvent) => {
e.stopPropagation();
try {
await apiFetch(`/api/session/${sessionId}`, { method: 'DELETE' });
deleteSession(sessionId);
} catch {
// Delete locally even if backend fails (session may already be gone)
deleteSession(sessionId);
}
},
[deleteSession],
);
const handleSelect = useCallback(
(sessionId: string) => {
switchSession(sessionId);
setPlan([]);
setPanelContent(null);
onClose?.();
},
[switchSession, setPlan, setPanelContent, onClose],
);
const formatTime = (d: string) =>
new Date(d).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
// ββ Render ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
return (
<Box
sx={{
height: '100%',
display: 'flex',
flexDirection: 'column',
bgcolor: 'var(--panel)',
}}
>
{/* ββ Header βββββββββββββββββββββββββββββββββββββββββββββββββββ */}
<Box sx={{ px: 1.75, pt: 2, pb: 0 }}>
<Typography
variant="caption"
sx={{
color: 'var(--muted-text)',
fontSize: '0.65rem',
fontWeight: 600,
textTransform: 'uppercase',
letterSpacing: '0.08em',
}}
>
Recent chats
</Typography>
</Box>
{/* ββ Capacity error βββββββββββββββββββββββββββββββββββββββββββ */}
{capacityError && (
<Alert
severity="warning"
variant="outlined"
onClose={() => setCapacityError(null)}
sx={{
m: 1,
fontSize: '0.7rem',
py: 0.25,
'& .MuiAlert-message': { py: 0 },
borderColor: '#FF9D00',
color: 'var(--text)',
}}
>
{capacityError}
</Alert>
)}
{/* ββ Session list βββββββββββββββββββββββββββββββββββββββββββββ */}
<Box
sx={{
flex: 1,
overflow: 'auto',
py: 1,
// Thinner scrollbar
'&::-webkit-scrollbar': { width: 4 },
'&::-webkit-scrollbar-thumb': {
bgcolor: 'var(--scrollbar-thumb)',
borderRadius: 2,
},
}}
>
{sessions.length === 0 ? (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
py: 8,
px: 3,
gap: 1.5,
}}
>
<ChatBubbleOutlineIcon
sx={{ fontSize: 28, color: 'var(--muted-text)', opacity: 0.25 }}
/>
<Typography
variant="caption"
sx={{
color: 'var(--muted-text)',
opacity: 0.5,
textAlign: 'center',
lineHeight: 1.5,
fontSize: '0.72rem',
}}
>
No sessions yet
</Typography>
</Box>
) : (
[...sessions].reverse().map((session, index) => {
const num = sessions.length - index;
const isSelected = session.id === activeSessionId;
return (
<Box
key={session.id}
onClick={() => handleSelect(session.id)}
sx={{
display: 'flex',
alignItems: 'center',
gap: 1,
px: 1.5,
py: 0.875,
mx: 0.75,
borderRadius: '10px',
cursor: 'pointer',
transition: 'background-color 0.12s ease',
bgcolor: isSelected
? 'var(--hover-bg)'
: 'transparent',
'&:hover': {
bgcolor: 'var(--hover-bg)',
},
'& .delete-btn': {
opacity: 0,
transition: 'opacity 0.12s',
},
'&:hover .delete-btn': {
opacity: 1,
},
}}
>
<ChatBubbleOutlineIcon
sx={{
fontSize: 15,
color: isSelected ? 'var(--text)' : 'var(--muted-text)',
opacity: isSelected ? 0.8 : 0.4,
flexShrink: 0,
}}
/>
<Box sx={{ flex: 1, minWidth: 0 }}>
<Typography
variant="body2"
sx={{
fontWeight: isSelected ? 600 : 400,
color: 'var(--text)',
fontSize: '0.84rem',
lineHeight: 1.4,
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{session.title.startsWith('Chat ') ? `Session ${String(num).padStart(2, '0')}` : session.title}
</Typography>
<Typography
variant="caption"
sx={{
color: 'var(--muted-text)',
fontSize: '0.65rem',
lineHeight: 1.2,
}}
>
{formatTime(session.createdAt)}
</Typography>
</Box>
<IconButton
className="delete-btn"
size="small"
onClick={(e) => handleDelete(session.id, e)}
sx={{
color: 'var(--muted-text)',
width: 26,
height: 26,
flexShrink: 0,
'&:hover': { color: 'var(--accent-red)', bgcolor: 'rgba(244,67,54,0.08)' },
}}
>
<DeleteOutlineIcon sx={{ fontSize: 15 }} />
</IconButton>
</Box>
);
})
)}
</Box>
{/* ββ Footer: New Session + status ββββββββββββββββββββββββββββ */}
<Divider sx={{ opacity: 0.5 }} />
<Box
sx={{
px: 1.5,
py: 1.5,
display: 'flex',
flexDirection: 'column',
gap: 1,
flexShrink: 0,
}}
>
<Box
component="button"
onClick={handleNewSession}
disabled={isCreatingSession}
sx={{
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
gap: 0.75,
width: '100%',
px: 1.5,
py: 1.25,
border: 'none',
borderRadius: '10px',
bgcolor: '#FF9D00',
color: '#000',
fontSize: '0.85rem',
fontWeight: 700,
cursor: 'pointer',
transition: 'all 0.12s ease',
'&:hover': {
bgcolor: '#FFB340',
},
'&:disabled': {
opacity: 0.5,
cursor: 'not-allowed',
},
}}
>
{isCreatingSession ? (
<>
<CircularProgress size={12} sx={{ color: '#000' }} />
Creating...
</>
) : (
<>
<AddIcon sx={{ fontSize: 16 }} />
New Session
</>
)}
</Box>
<Box
sx={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 0.5,
}}
>
<StatusDot connected={isConnected} />
<Typography
variant="caption"
sx={{ color: 'var(--muted-text)', fontSize: '0.62rem', letterSpacing: '0.02em' }}
>
{sessions.length} session{sessions.length !== 1 ? 's' : ''} · Backend {isConnected ? 'online' : 'offline'}
</Typography>
</Box>
</Box>
</Box>
);
}
|