-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance/collection alphabetical sorting (#1140)
* refactor: extract getCollectionItems to separate utility file * fix: use getTime() for comparison * improve title sorting with localeCompare * test: update expected number of occurrences in Collection story * add check for both date undefined * fix test: Update expected number of occurrences in Collection story
- Loading branch information
1 parent
b901e38
commit b384d04
Showing
3 changed files
with
116 additions
and
104 deletions.
There are no files selected for viewing
110 changes: 6 additions & 104 deletions
110
packages/components/src/templates/next/layouts/Collection/Collection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/components/src/templates/next/layouts/Collection/utils/getCollectionItems.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { IsomerSiteProps } from "~/engine" | ||
import type { AllCardProps } from "~/interfaces" | ||
import { getParsedDate, getSitemapAsArray } from "~/utils" | ||
|
||
const CATEGORY_OTHERS = "Others" | ||
|
||
export const getCollectionItems = ( | ||
site: IsomerSiteProps, | ||
permalink: string, | ||
): AllCardProps[] => { | ||
let currSitemap = site.siteMap | ||
const permalinkParts = permalink.split("/") | ||
|
||
for (let i = 2; i <= permalinkParts.length; i++) { | ||
const currPermalink = permalinkParts.slice(0, i).join("/") | ||
|
||
if (!currSitemap.children) { | ||
return [] | ||
} | ||
|
||
const child = currSitemap.children.find( | ||
(child) => child.permalink === currPermalink, | ||
) | ||
|
||
if (!child) { | ||
return [] | ||
} | ||
|
||
currSitemap = child | ||
} | ||
|
||
if (!currSitemap.children) { | ||
return [] | ||
} | ||
|
||
const items = currSitemap.children.flatMap((child) => | ||
getSitemapAsArray(child), | ||
) | ||
|
||
const transformedItems = items | ||
.filter( | ||
(item) => | ||
item.layout === "file" || | ||
item.layout === "link" || | ||
item.layout === "article", | ||
) | ||
.map((item) => { | ||
const date = | ||
item.date !== undefined ? getParsedDate(item.date) : undefined | ||
|
||
const baseItem = { | ||
type: "collectionCard" as const, | ||
rawDate: date, | ||
lastUpdated: date?.toISOString(), | ||
category: item.category || CATEGORY_OTHERS, | ||
title: item.title, | ||
description: item.summary, | ||
image: item.image, | ||
site, | ||
tags: item.tags, | ||
} | ||
|
||
if (item.layout === "file") { | ||
return { | ||
...baseItem, | ||
variant: "file", | ||
url: item.ref, | ||
fileDetails: item.fileDetails, | ||
} | ||
} else if (item.layout === "link") { | ||
return { | ||
...baseItem, | ||
variant: "link", | ||
url: item.ref, | ||
} | ||
} | ||
|
||
return { | ||
...baseItem, | ||
variant: "article", | ||
url: item.permalink, | ||
} | ||
}) satisfies AllCardProps[] | ||
|
||
return transformedItems.sort((a, b) => { | ||
// Sort by last updated date, tiebreaker by title | ||
if (a.rawDate && b.rawDate && a.rawDate.getTime() === b.rawDate.getTime()) { | ||
// localeCompare better than > operator as | ||
// it properly handles international and special characters | ||
return a.title.localeCompare(b.title) | ||
} | ||
|
||
// If both items have no dates, sort by title | ||
if (a.rawDate === undefined && b.rawDate === undefined) { | ||
return a.title.localeCompare(b.title) | ||
} | ||
|
||
// Rank items with no dates last | ||
if (a.rawDate === undefined) { | ||
return 1 | ||
} | ||
|
||
if (b.rawDate === undefined) { | ||
return -1 | ||
} | ||
|
||
return a.rawDate.getTime() < b.rawDate.getTime() ? 1 : -1 | ||
}) as AllCardProps[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters