Skip to content

Commit 74e77e1

Browse files
juristrFrozenPandaz
authored andcommitted
fix(nx-dev): update sorting of pinned posts
(cherry picked from commit d347d97)
1 parent dccdb43 commit 74e77e1

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

nx-dev/nx-dev/pages/_app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default function CustomApp({
8787
</Link>
8888
<Component {...pageProps} />
8989
{/* <LiveStreamNotifier /> */}
90-
<WebinarNotifier />
90+
{/* <WebinarNotifier /> */}
9191

9292
{/* All tracking scripts consolidated in GlobalScripts component */}
9393
<GlobalScripts

nx-dev/ui-blog/src/lib/blog-container.tsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Filters } from './filters';
77
import { useSearchParams } from 'next/navigation';
88
import Link from 'next/link';
99
import { ALL_TOPICS } from './topics';
10+
import { sortFirstFivePosts } from './sort-featured-posts';
1011
import {
1112
ComputerDesktopIcon,
1213
BookOpenIcon,
@@ -25,24 +26,6 @@ export interface BlogContainerProps {
2526
tags: string[];
2627
}
2728

28-
// first five blog posts should prioritize pinned posts, then show recent posts
29-
// excluding any posts that have specific slugs we want to deprioritize
30-
export function sortFirstFivePosts(
31-
posts: BlogPostDataEntry[]
32-
): BlogPostDataEntry[] {
33-
// Sort posts: pinned posts first, then by date
34-
const sortedPosts = posts.sort((a, b) => {
35-
// If one is pinned and the other isn't, prioritize the pinned one
36-
if (a.pinned === true && b.pinned !== true) return -1;
37-
if (b.pinned === true && a.pinned !== true) return 1;
38-
39-
// Otherwise, sort by date (newest first)
40-
return new Date(b.date).valueOf() - new Date(a.date).valueOf();
41-
});
42-
43-
return sortedPosts.slice(0, 5);
44-
}
45-
4629
export function BlogContainer({ blogPosts, tags }: BlogContainerProps) {
4730
const searchParams = useSearchParams();
4831
const [filteredList, setFilteredList] = useState(blogPosts);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { BlogPostDataEntry } from '@nx/nx-dev/data-access-documents/node-only';
2+
3+
// first five blog posts should prioritize pinned posts, then show recent posts
4+
// if there are fewer than 5 pinned posts, fill remaining spots with newest posts by date
5+
// then sort the final 5 posts by date so newer posts can appear before older pinned posts
6+
export function sortFirstFivePosts(
7+
posts: BlogPostDataEntry[]
8+
): BlogPostDataEntry[] {
9+
// Separate pinned and non-pinned posts
10+
const pinnedPosts = posts.filter((post) => post.pinned === true);
11+
const nonPinnedPosts = posts.filter((post) => post.pinned !== true);
12+
13+
// Sort both groups by date (newest first)
14+
pinnedPosts.sort(
15+
(a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf()
16+
);
17+
nonPinnedPosts.sort(
18+
(a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf()
19+
);
20+
21+
// Take up to 5 pinned posts, then fill remaining spots with newest non-pinned posts
22+
const selectedPosts = [
23+
...pinnedPosts.slice(0, 5),
24+
...nonPinnedPosts.slice(0, Math.max(0, 5 - pinnedPosts.length)),
25+
];
26+
27+
// Sort the final selection by date (newest first)
28+
// This allows newer non-pinned posts to appear before older pinned posts
29+
return selectedPosts
30+
.sort((a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf())
31+
.slice(0, 5);
32+
}

0 commit comments

Comments
 (0)