export function groupTabsByDate()

in server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb/util.ts [246:300]


export function groupTabsByDate(tabs: Tab[]): ConversationItemGroup[] {
    const now = new Date()
    const today = new Date(now.setHours(0, 0, 0, 0))
    const yesterday = new Date(today)
    yesterday.setDate(yesterday.getDate() - 1)
    const lastWeek = new Date(today)
    lastWeek.setDate(lastWeek.getDate() - 7)
    const lastMonth = new Date(today)
    lastMonth.setMonth(lastMonth.getMonth() - 1)

    // Sort tabs by updatedAt in descending order
    const sortedTabs = [...tabs]
        .map(tab => ({ ...tab, updatedAt: new Date(tab.updatedAt) }))
        .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime())

    // Helper function to convert Tab to DetailedListItem
    const tabToDetailedListItem = (tab: Tab): ConversationItem => ({
        icon: getTabTypeIcon(tab.tabType),
        // Show open tabs as bold (in markdown)
        description: tab.isOpen ? `**${tab.title}**` : tab.title,
        id: tab.historyId,
    })

    const tabGroups = [
        {
            name: 'Today',
            tabs: sortedTabs.filter(tab => tab.updatedAt >= today),
        },
        {
            name: 'Yesterday',
            tabs: sortedTabs.filter(tab => tab.updatedAt >= yesterday && tab.updatedAt < today),
        },
        {
            name: 'Last Week',
            tabs: sortedTabs.filter(tab => tab.updatedAt >= lastWeek && tab.updatedAt < yesterday),
        },
        {
            name: 'Last Month',
            tabs: sortedTabs.filter(tab => tab.updatedAt >= lastMonth && tab.updatedAt < lastWeek),
        },
        {
            name: 'Older',
            tabs: sortedTabs.filter(tab => tab.updatedAt < lastMonth),
        },
    ]

    // Convert to DetailedListItemGroup[] and filter out empty groups
    return tabGroups
        .filter(group => group.tabs.length > 0)
        .map(group => ({
            groupName: group.name,
            icon: 'calendar',
            items: group.tabs.map(tabToDetailedListItem),
        }))
}