Files
web/apps/scandic-web/components/Blocks/DynamicContent/Stays/Previous/utils/groupStaysByYear.ts
Chuma Mcphoy (We Ahead) 30b9d14fb0 Merged in feat/LOY-424-Sidepeek-Past-Stays (pull request #3270)
feat(LOY-424): Load More Past Stays via Sidepeek

* feat(LOY-424): Load More Past Stays via Sidepeek

* chore(LOY-424): use new section header

* fix(LOY-424): remove uneeded nextCursor check


Approved-by: Emma Zettervall
2025-12-03 15:19:25 +00:00

30 lines
764 B
TypeScript

import { dt } from "@scandic-hotels/common/dt"
import type { Stay } from "@scandic-hotels/trpc/routers/user/output"
export interface StaysByYear {
year: number
stays: Stay[]
}
/**
* Groups stays by year based on checkinDate.
* @returns an array sorted by year in descending order (most recent first).
*/
export function groupStaysByYear(stays: Stay[]): StaysByYear[] {
const groupedMap = new Map<number, Stay[]>()
for (const stay of stays) {
const year = dt(stay.attributes.checkinDate).year()
if (!groupedMap.has(year)) {
groupedMap.set(year, [])
}
groupedMap.get(year)!.push(stay)
}
return Array.from(groupedMap.entries())
.sort(([yearA], [yearB]) => yearB - yearA)
.map(([year, stays]) => ({ year, stays }))
}