Skip to content

Commit 7bd09eb

Browse files
committed
Fix public team and popular articles
1 parent 551ccff commit 7bd09eb

3 files changed

Lines changed: 33 additions & 22 deletions

File tree

app/team/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ type AuthorType = {
5959

6060
async function getAuthors() {
6161
try {
62-
const authors = await getAllAuthors();
62+
const authors = (await getAllAuthors()).filter(
63+
(author) => author.role !== "moderator" && author.showOnTeam,
64+
);
6365
return authors.map((author) => ({
6466
uid: author.uid,
6567
name: author.name,

components/PopularArticles.tsx

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
onSnapshot,
77
query,
88
where,
9-
orderBy,
109
Timestamp,
1110
} from "firebase/firestore";
1211
import { db } from "@/lib/firebase";
@@ -36,34 +35,40 @@ export default function PopularArticles({
3635
useState<Article[]>(initialArticles);
3736

3837
useEffect(() => {
39-
// Query Firestore for articles that have popularity == true AND publish == true
40-
// Then order by date descending (assuming 'date' is a valid field in the documents)
38+
// Keep the live query on one indexed field, then filter and rank locally.
39+
// This avoids requiring a composite index for the public sidebar.
4140
const articlesQuery = query(
4241
collection(db, "articles"),
43-
where("popularity", "==", true),
4442
where("publish", "==", true),
45-
orderBy("popularityRank", "asc"),
4643
);
4744

4845
const unsubscribe = onSnapshot(
4946
articlesQuery,
5047
(snapshot) => {
51-
const articles: Article[] = snapshot.docs.map((doc) => {
52-
const data = doc.data();
53-
return {
54-
id: doc.id,
55-
title: data.title || "",
56-
slug: data.slug || "",
57-
authorName: data.authorName || "Unknown Team Member",
58-
popularity: data.popularity || false,
59-
publish: data.publish || false,
60-
date:
61-
data.date instanceof Timestamp
62-
? data.date
63-
: (data.date ?? new Date().toISOString()),
64-
popularityRank: data.popularityRank,
65-
} as Article;
66-
});
48+
const articles: Article[] = snapshot.docs
49+
.map((doc) => {
50+
const data = doc.data();
51+
return {
52+
id: doc.id,
53+
title: data.title || "",
54+
slug: data.slug || "",
55+
authorName: data.authorName || "Unknown Team Member",
56+
popularity: data.popularity || false,
57+
publish: data.publish || false,
58+
date:
59+
data.date instanceof Timestamp
60+
? data.date
61+
: (data.date ?? new Date().toISOString()),
62+
popularityRank: data.popularityRank,
63+
} as Article;
64+
})
65+
.filter((article) => article.popularity)
66+
.sort(
67+
(left, right) =>
68+
(left.popularityRank ?? Number.MAX_SAFE_INTEGER) -
69+
(right.popularityRank ?? Number.MAX_SAFE_INTEGER) ||
70+
left.title.localeCompare(right.title),
71+
);
6772

6873
setPopularArticles(articles);
6974
},

lib/content.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export type AuthorRecord = {
9797
body: string;
9898
};
9999
socials: SocialMap;
100+
role: string;
101+
showOnTeam: boolean;
100102
updatedAt?: Date;
101103
};
102104

@@ -395,6 +397,8 @@ function normalizeAuthorDoc(doc: ContentDocument): AuthorRecord {
395397
slug: pickString(data, ["slug"]) || "",
396398
biography: extractBiography(data.biography, name),
397399
socials: normalizeSocials(data.socials),
400+
role: (pickString(data, ["role"]) || "").toLowerCase(),
401+
showOnTeam: data.showOnTeam !== false,
398402
updatedAt: pickDate(data, AUTHOR_UPDATED_FIELDS),
399403
};
400404
}

0 commit comments

Comments
 (0)