Skip to content

Commit ad0bcba

Browse files
committed
fix: Enhance project sorting in sidebar
- Fixed combined sorting for projects: starred projects first, followed by sorting by last activity date or display name. - Added helper functions to retrieve all sessions for a project and determine the last activity date. - Improved sorting logic to accommodate user-defined display names and recent activity.
1 parent a56e063 commit ad0bcba

1 file changed

Lines changed: 36 additions & 38 deletions

File tree

src/components/Sidebar.jsx

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,48 @@ function Sidebar({
200200
return starredProjects.has(projectName);
201201
};
202202

203-
// Sort projects to show starred ones first
203+
// Helper function to get all sessions for a project (initial + additional)
204+
const getAllSessions = (project) => {
205+
const initialSessions = project.sessions || [];
206+
const additional = additionalSessions[project.name] || [];
207+
return [...initialSessions, ...additional];
208+
};
209+
210+
// Helper function to get the last activity date for a project
211+
const getProjectLastActivity = (project) => {
212+
const allSessions = getAllSessions(project);
213+
if (allSessions.length === 0) {
214+
return new Date(0); // Return epoch date for projects with no sessions
215+
}
216+
217+
// Find the most recent session activity
218+
const mostRecentDate = allSessions.reduce((latest, session) => {
219+
const sessionDate = new Date(session.lastActivity);
220+
return sessionDate > latest ? sessionDate : latest;
221+
}, new Date(0));
222+
223+
return mostRecentDate;
224+
};
225+
226+
// Combined sorting: starred projects first, then by selected order
204227
const sortedProjects = [...projects].sort((a, b) => {
205228
const aStarred = isProjectStarred(a.name);
206229
const bStarred = isProjectStarred(b.name);
207230

231+
// First, sort by starred status
208232
if (aStarred && !bStarred) return -1;
209233
if (!aStarred && bStarred) return 1;
210-
return 0; // Keep original order for same star status
234+
235+
// For projects with same starred status, sort by selected order
236+
if (projectSortOrder === 'date') {
237+
// Sort by most recent activity (descending)
238+
return getProjectLastActivity(b) - getProjectLastActivity(a);
239+
} else {
240+
// Sort by display name (user-defined) or fallback to name (ascending)
241+
const nameA = a.displayName || a.name;
242+
const nameB = b.displayName || b.name;
243+
return nameA.localeCompare(nameB);
244+
}
211245
});
212246

213247
const startEditing = (project) => {
@@ -369,42 +403,6 @@ function Sidebar({
369403
}
370404
};
371405

372-
// Helper function to get all sessions for a project (initial + additional)
373-
const getAllSessions = (project) => {
374-
const initialSessions = project.sessions || [];
375-
const additional = additionalSessions[project.name] || [];
376-
return [...initialSessions, ...additional];
377-
};
378-
379-
// Helper function to get the last activity date for a project
380-
const getProjectLastActivity = (project) => {
381-
const allSessions = getAllSessions(project);
382-
if (allSessions.length === 0) {
383-
return new Date(0); // Return epoch date for projects with no sessions
384-
}
385-
386-
// Find the most recent session activity
387-
const mostRecentDate = allSessions.reduce((latest, session) => {
388-
const sessionDate = new Date(session.lastActivity);
389-
return sessionDate > latest ? sessionDate : latest;
390-
}, new Date(0));
391-
392-
return mostRecentDate;
393-
};
394-
395-
// Sort projects based on selected order
396-
const sortedProjects = [...projects].sort((a, b) => {
397-
if (projectSortOrder === 'date') {
398-
// Sort by most recent activity (descending)
399-
return getProjectLastActivity(b) - getProjectLastActivity(a);
400-
} else {
401-
// Sort by display name (user-defined) or fallback to name (ascending)
402-
const nameA = a.displayName || a.name;
403-
const nameB = b.displayName || b.name;
404-
return nameA.localeCompare(nameB);
405-
}
406-
});
407-
408406
// Filter projects based on search input
409407
const filteredProjects = sortedProjects.filter(project => {
410408
if (!searchFilter.trim()) return true;

0 commit comments

Comments
 (0)