Spaces:
Running
Running
File size: 9,594 Bytes
90783c6 8cfacd3 b9f08b9 90783c6 8cfacd3 1b47370 8cfacd3 706c3e3 8cfacd3 1b47370 8cfacd3 90783c6 8cfacd3 1b47370 8cfacd3 706c3e3 8cfacd3 1b47370 706c3e3 90783c6 706c3e3 90783c6 706c3e3 90783c6 8cfacd3 90783c6 8cfacd3 9f5166d 8cfacd3 1b47370 8cfacd3 706c3e3 8cfacd3 1b47370 8cfacd3 1b47370 8cfacd3 1b47370 9f5166d 1b47370 8cfacd3 1b47370 8cfacd3 1b47370 8cfacd3 1b47370 8cfacd3 1b47370 8cfacd3 1b47370 8cfacd3 9f5166d 8cfacd3 1b47370 8cfacd3 1b47370 8cfacd3 1b47370 90783c6 1b47370 8cfacd3 1b47370 8cfacd3 90783c6 8cfacd3 |
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 |
import { useCallback, useState } from 'react';
import {
Box,
List,
ListItem,
IconButton,
Typography,
Button,
CircularProgress,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
} from '@mui/material';
import DeleteIcon from '@mui/icons-material/Delete';
import { useSessionStore } from '@/store/sessionStore';
interface SessionSidebarProps {
onClose?: () => void;
}
export default function SessionSidebar({ onClose }: SessionSidebarProps) {
const {
sessions,
sessionsLoading,
phase,
createSession,
selectSession,
deleteSession,
isSessionSelected,
} = useSessionStore();
// Delete confirmation state
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
const [sessionToDelete, setSessionToDelete] = useState<string | null>(null);
const handleNewSession = useCallback(async () => {
await createSession();
onClose?.();
}, [createSession, onClose]);
const handleSelectSession = useCallback(
async (sessionId: string) => {
await selectSession(sessionId);
onClose?.();
},
[selectSession, onClose]
);
const handleDeleteClick = useCallback(
(sessionId: string, e: React.MouseEvent) => {
e.stopPropagation();
setSessionToDelete(sessionId);
setDeleteConfirmOpen(true);
},
[]
);
const handleConfirmDelete = useCallback(async () => {
if (sessionToDelete) {
await deleteSession(sessionToDelete);
}
setDeleteConfirmOpen(false);
setSessionToDelete(null);
}, [sessionToDelete, deleteSession]);
const handleCancelDelete = useCallback(() => {
setDeleteConfirmOpen(false);
setSessionToDelete(null);
}, []);
const formatTime = (dateString: string) => {
const date = new Date(dateString);
const now = new Date();
const isToday = date.toDateString() === now.toDateString();
if (isToday) {
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
};
// Check if a session is currently loading
const isLoadingSession = (sessionId: string) => {
return phase.status === 'loading' && phase.sessionId === sessionId;
};
return (
<Box className="sidebar" sx={{ height: '100%', display: 'flex', flexDirection: 'column', bgcolor: 'var(--panel)' }}>
{/* Header */}
<Box sx={{
height: '60px',
display: 'flex',
alignItems: 'center',
px: 2,
borderBottom: '1px solid rgba(255,255,255,0.03)'
}}>
<Box className="brand-logo" sx={{ display: 'flex' }}>
<img
src="/hf-log-only-white.png"
alt="HF Agent"
style={{ height: '24px', objectFit: 'contain' }}
/>
</Box>
</Box>
{/* Content */}
<Box sx={{ flex: 1, display: 'flex', flexDirection: 'column', p: 2, overflow: 'hidden' }}>
<Button
fullWidth
className="create-session"
onClick={handleNewSession}
disabled={phase.status === 'loading'}
sx={{
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'flex-start',
gap: '10px',
padding: '10px 14px',
borderRadius: 'var(--radius-md)',
border: '1px solid rgba(255,255,255,0.06)',
bgcolor: 'transparent',
color: 'var(--text)',
fontWeight: 600,
textTransform: 'none',
mb: 3,
'&:hover': {
bgcolor: 'rgba(255,255,255,0.02)',
border: '1px solid rgba(255,255,255,0.1)',
},
'&:disabled': {
opacity: 0.5,
},
'&::before': {
content: '""',
width: '4px',
height: '20px',
background: 'linear-gradient(180deg, var(--accent-yellow), rgba(199,165,0,0.9))',
borderRadius: '4px',
}
}}
>
New Session
</Button>
{/* Session List */}
<Box sx={{ flex: 1, overflow: 'auto', mx: -1, px: 1 }}>
{sessionsLoading && sessions.length === 0 && (
<Box sx={{ display: 'flex', justifyContent: 'center', py: 2 }}>
<CircularProgress size={20} sx={{ color: 'var(--muted-text)' }} />
</Box>
)}
{sessions.length === 0 && !sessionsLoading && (
<Typography variant="body2" sx={{ color: 'var(--muted-text)', textAlign: 'center', py: 2 }}>
No sessions yet
</Typography>
)}
<List disablePadding sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
{sessions.map((session) => {
const isSelected = isSessionSelected(session.id);
const isLoading = isLoadingSession(session.id);
return (
<ListItem
key={session.id}
disablePadding
className="session-item"
onClick={() => !isLoading && handleSelectSession(session.id)}
sx={{
display: 'flex',
alignItems: 'center',
gap: '12px',
padding: '10px',
borderRadius: 'var(--radius-md)',
bgcolor: isSelected ? 'rgba(255,255,255,0.05)' : 'transparent',
cursor: isLoading ? 'default' : 'pointer',
opacity: isLoading ? 0.7 : 1,
transition: 'background 0.18s ease, transform 0.08s ease',
'&:hover': {
bgcolor: isLoading ? undefined : 'rgba(255,255,255,0.02)',
transform: isLoading ? undefined : 'translateY(-1px)',
},
'& .delete-btn': {
opacity: 0,
transition: 'opacity 0.2s',
},
'&:hover .delete-btn': {
opacity: isLoading ? 0 : 1,
}
}}
>
<Box sx={{ flex: 1, overflow: 'hidden' }}>
<Typography variant="body2" sx={{ fontWeight: 500, color: 'var(--text)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
{session.title}
</Typography>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mt: 0.5 }}>
<Typography className="time" variant="caption" sx={{ fontSize: '12px', color: 'var(--muted-text)' }}>
{formatTime(session.updatedAt || session.createdAt)}
</Typography>
{session.messageCount > 0 && (
<Typography variant="caption" sx={{ fontSize: '12px', color: 'var(--muted-text)' }}>
· {session.messageCount} msgs
</Typography>
)}
</Box>
</Box>
{isLoading ? (
<CircularProgress size={16} sx={{ color: 'var(--muted-text)' }} />
) : (
<IconButton
className="delete-btn"
size="small"
onClick={(e) => handleDeleteClick(session.id, e)}
sx={{ color: 'var(--muted-text)', '&:hover': { color: 'var(--accent-red)' } }}
>
<DeleteIcon fontSize="small" />
</IconButton>
)}
</ListItem>
);
})}
</List>
</Box>
</Box>
{/* Footer */}
<Box sx={{ p: 2, borderTop: '1px solid rgba(255,255,255,0.03)' }}>
<Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
<Typography variant="caption" className="small-note" sx={{ fontSize: '12px', color: 'var(--muted-text)' }}>
{sessions.length} session{sessions.length !== 1 ? 's' : ''}
</Typography>
</Box>
</Box>
{/* Delete Confirmation Dialog */}
<Dialog
open={deleteConfirmOpen}
onClose={handleCancelDelete}
PaperProps={{
sx: {
bgcolor: 'var(--panel)',
backgroundImage: 'none',
borderRadius: 'var(--radius-lg)',
border: '1px solid rgba(255,255,255,0.1)',
},
}}
>
<DialogTitle sx={{ color: 'var(--text)' }}>Delete Session?</DialogTitle>
<DialogContent>
<Typography variant="body2" sx={{ color: 'var(--muted-text)' }}>
This action cannot be undone. The session and all its messages will be permanently deleted.
</Typography>
</DialogContent>
<DialogActions sx={{ px: 3, pb: 2 }}>
<Button
onClick={handleCancelDelete}
sx={{
color: 'var(--muted-text)',
'&:hover': { bgcolor: 'rgba(255,255,255,0.05)' },
}}
>
Cancel
</Button>
<Button
onClick={handleConfirmDelete}
variant="contained"
sx={{
bgcolor: 'var(--accent-red)',
color: 'white',
'&:hover': { bgcolor: 'var(--accent-red)', opacity: 0.9 },
}}
>
Delete
</Button>
</DialogActions>
</Dialog>
</Box>
);
}
|