fix(web): resolve community author as GitHub profile when possible

This commit is contained in:
Thomas Camlong
2026-01-08 13:44:25 +01:00
parent 46369283d2
commit 8b4f0eef67
4 changed files with 66 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ import type { Metadata, ResolvingMetadata } from "next"
import { notFound, permanentRedirect } from "next/navigation"
import { IconDetails } from "@/components/icon-details"
import { BASE_URL, WEB_URL } from "@/constants"
import { getAllIcons } from "@/lib/api"
import { getAllIcons, getAuthorData } from "@/lib/api"
import { getCommunityGalleryRecord, getCommunitySubmissionByName, getCommunitySubmissions } from "@/lib/community"
export const dynamicParams = true
@@ -124,14 +124,64 @@ export default async function CommunityIconPage({ params }: { params: Promise<{
const allIcons = await getAllIcons()
const authorData = {
id: 0,
name: iconData.data.update.author.name || "Community",
login: iconData.data.update.author.name || "community",
avatar_url: "",
html_url: "",
const author = iconData.data.update.author as any
const githubId = author?.github_id
const authorMetaLogin = author?.login || author?.name
let authorData:
| {
id: number | string
name?: string
login: string
avatar_url: string
html_url: string
}
| undefined
if (githubId && /^\d+$/.test(String(githubId))) {
authorData = await getAuthorData(String(githubId), { login: authorMetaLogin, name: author?.name })
} else if (authorMetaLogin && authorMetaLogin !== "community" && authorMetaLogin !== "Community") {
// Fallback: resolve by GitHub username if we don't have github_id in the community_gallery view.
// This avoids losing the GitHub link when PB doesn't expose github_id in the view.
try {
const headers: Record<string, string> = {}
if (process.env.GITHUB_TOKEN) {
headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`
}
const res = await fetch(`https://api.github.com/users/${encodeURIComponent(authorMetaLogin)}`, { headers })
if (res.ok) {
const gh = await res.json()
authorData = {
id: gh?.id ?? 0,
name: gh?.name ?? authorMetaLogin,
login: gh?.login ?? authorMetaLogin,
avatar_url: gh?.avatar_url ?? "",
html_url: gh?.html_url ?? "",
}
}
} catch (err) {
console.log("[CommunityPage] GitHub username fallback failed:", { icon, login: authorMetaLogin, err })
}
}
if (!authorData) {
authorData = {
id: 0,
name: author?.name || "Community",
login: authorMetaLogin || "community",
avatar_url: "",
html_url: "",
}
}
console.log("[CommunityPage] resolved authorData:", {
icon,
id: authorData.id,
login: authorData.login,
html_url: authorData.html_url,
avatar_url: authorData.avatar_url,
})
console.log(iconData.data)
const mainIconUrl =
typeof iconData.data.base === "string" && iconData.data.base.startsWith("http")
? iconData.data.base

View File

@@ -101,6 +101,8 @@ function transformGalleryToIcon(item: CommunityGallery): any {
author: {
id: 0,
name: item.created_by || "Community",
login: item.created_by || undefined,
github_id: item.created_by_github_id,
},
},
colors: colors,
@@ -151,6 +153,11 @@ async function fetchCommunitySubmissionByName(name: string): Promise<IconWithNam
const pb = createServerPB()
const record = await pb.collection("community_gallery").getFirstListItem<CommunityGallery>(`name="${name}"`)
console.log("[Community] Record author fields:", {
name,
created_by: record.created_by,
created_by_github_id: record.created_by_github_id,
})
const transformed = transformGalleryToIcon(record)
console.log(`[Community] Fetched ${name}, colors:`, transformed.data.colors)
return transformed

View File

@@ -47,6 +47,7 @@ export interface CommunityGallery {
created_by: string
approved_by?: string
description?: string
created_by_github_id?: string
status: "approved" | "rejected" | "pending" | "added_to_collection"
assets: string[]
admin_comment?: string

View File

@@ -1,5 +1,6 @@
export type IconAuthor = {
id: number | string
github_id?: string
name?: string
login?: string
}