refactor: 调整首页统计与标签洞察展示
This commit is contained in:
+22
-60
@@ -1,5 +1,5 @@
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { getTagCategoryGroups, getTopTags } from '@/hooks/useProjects'
|
||||
import { getTopTags } from '@/hooks/useProjects'
|
||||
import { Prisma } from '@prisma/client'
|
||||
|
||||
const ONE_DAY_MS = 24 * 60 * 60 * 1000
|
||||
@@ -24,28 +24,12 @@ export type HomeProjectSummary = {
|
||||
}>
|
||||
}
|
||||
|
||||
export type HomeCategoryDistribution = {
|
||||
category: string
|
||||
name: string
|
||||
nameEn: string
|
||||
tagCount: number
|
||||
projectAssociationCount: number
|
||||
topTag: {
|
||||
id: string
|
||||
name: string
|
||||
nameEn: string | null
|
||||
slug: string
|
||||
projectCount: number
|
||||
} | null
|
||||
}
|
||||
|
||||
export type HomePageData = {
|
||||
overview: {
|
||||
totalProjects: number
|
||||
activeProjects: number
|
||||
archivedProjects: number
|
||||
totalTags: number
|
||||
newProjects30d: number
|
||||
newProjects7d: number
|
||||
newProjects24h: number
|
||||
}
|
||||
rankings: {
|
||||
latestByWindow: {
|
||||
@@ -63,7 +47,6 @@ export type HomePageData = {
|
||||
slug: string
|
||||
projectCount: number
|
||||
}>
|
||||
categoryDistribution: HomeCategoryDistribution[]
|
||||
}
|
||||
timeline: HomeProjectSummary[]
|
||||
}
|
||||
@@ -162,29 +145,23 @@ export async function getHomePageData(): Promise<HomePageData> {
|
||||
|
||||
const [
|
||||
totalProjects,
|
||||
activeProjects,
|
||||
archivedProjects,
|
||||
totalTags,
|
||||
newProjects30d,
|
||||
newProjects7d,
|
||||
newProjects24h,
|
||||
latest24h,
|
||||
latest7d,
|
||||
latest30d,
|
||||
topStars,
|
||||
timeline,
|
||||
topTags,
|
||||
tagCategoryGroups,
|
||||
] = await Promise.all([
|
||||
safeQuery('countTotalProjects', 0, () => prisma.project.count()),
|
||||
safeQuery('countActiveProjects', 0, () => prisma.project.count({ where: { status: 'ACTIVE' } })),
|
||||
safeQuery('countArchivedProjects', 0, () => prisma.project.count({ where: { status: 'ARCHIVED' } })),
|
||||
safeQuery('countTotalTags', 0, () =>
|
||||
prisma.tag.count({
|
||||
safeQuery('countNewProjects30d', 0, () =>
|
||||
prisma.project.count({
|
||||
where: {
|
||||
category: {
|
||||
not: 'FIXED_PROJECT_TYPE',
|
||||
},
|
||||
projects: {
|
||||
some: {},
|
||||
status: 'ACTIVE',
|
||||
createdAt: {
|
||||
gte: last30Days,
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -199,44 +176,30 @@ export async function getHomePageData(): Promise<HomePageData> {
|
||||
},
|
||||
})
|
||||
),
|
||||
safeQuery('countNewProjects24h', 0, () =>
|
||||
prisma.project.count({
|
||||
where: {
|
||||
status: 'ACTIVE',
|
||||
createdAt: {
|
||||
gte: last24Hours,
|
||||
},
|
||||
},
|
||||
})
|
||||
),
|
||||
getLatestProjects(DEFAULT_RANKING_LIMIT, last24Hours),
|
||||
getLatestProjects(DEFAULT_RANKING_LIMIT, last7Days),
|
||||
getLatestProjects(DEFAULT_RANKING_LIMIT, last30Days),
|
||||
getTopStarsProjects(DEFAULT_RANKING_LIMIT),
|
||||
getLatestProjects(DEFAULT_TIMELINE_LIMIT),
|
||||
getTopTags(DEFAULT_TOP_TAG_LIMIT),
|
||||
getTagCategoryGroups(),
|
||||
])
|
||||
|
||||
const categoryDistribution: HomeCategoryDistribution[] = tagCategoryGroups.map((group) => {
|
||||
const projectAssociationCount = group.tags.reduce((sum, tag) => sum + tag._count.projects, 0)
|
||||
const leadingTag = group.tags[0]
|
||||
|
||||
return {
|
||||
category: group.category,
|
||||
name: group.name,
|
||||
nameEn: group.nameEn,
|
||||
tagCount: group.tags.length,
|
||||
projectAssociationCount,
|
||||
topTag: leadingTag
|
||||
? {
|
||||
id: leadingTag.id,
|
||||
name: leadingTag.name,
|
||||
nameEn: leadingTag.nameEn,
|
||||
slug: leadingTag.slug,
|
||||
projectCount: leadingTag._count.projects,
|
||||
}
|
||||
: null,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
overview: {
|
||||
totalProjects,
|
||||
activeProjects,
|
||||
archivedProjects,
|
||||
totalTags,
|
||||
newProjects30d,
|
||||
newProjects7d,
|
||||
newProjects24h,
|
||||
},
|
||||
rankings: {
|
||||
latestByWindow: {
|
||||
@@ -254,7 +217,6 @@ export async function getHomePageData(): Promise<HomePageData> {
|
||||
slug: tag.slug,
|
||||
projectCount: tag._count.projects,
|
||||
})),
|
||||
categoryDistribution,
|
||||
},
|
||||
timeline,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user